EnvironmentModule.php
1 week ago
WpEnvironment.php
1 week ago
WpEnvironmentFactory.php
1 week ago
WpEnvironmentFactoryInterface.php
1 week ago
WpEnvironmentInterface.php
1 week ago
WpEnvironmentFactory.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace Syde\Vendor\Worldline\Inpsyde\WorldlineForWoocommerce\Environment; |
| 5 | |
| 6 | use Syde\Vendor\Worldline\Dhii\Package\Version\StringVersionFactoryInterface; |
| 7 | use Exception; |
| 8 | use WooCommerce; |
| 9 | use function do_action; |
| 10 | use const WC_VERSION; |
| 11 | /** |
| 12 | * Service creating objects representing WordPress environment. |
| 13 | */ |
| 14 | class WpEnvironmentFactory implements WpEnvironmentFactoryInterface |
| 15 | { |
| 16 | protected StringVersionFactoryInterface $versionFactory; |
| 17 | protected string $eventNameEnvironmentValidationFailed; |
| 18 | /** |
| 19 | * @param StringVersionFactoryInterface $versionFactory |
| 20 | * @param string $eventNameEnvironmentValidationFailed |
| 21 | */ |
| 22 | public function __construct(StringVersionFactoryInterface $versionFactory, string $eventNameEnvironmentValidationFailed) |
| 23 | { |
| 24 | $this->versionFactory = $versionFactory; |
| 25 | $this->eventNameEnvironmentValidationFailed = $eventNameEnvironmentValidationFailed; |
| 26 | } |
| 27 | /** |
| 28 | * @inheritDoc |
| 29 | */ |
| 30 | public function createFromGlobals() : WpEnvironmentInterface |
| 31 | { |
| 32 | return new WpEnvironment($this->phpVersion(), $this->wpVersion(), $this->wcVersion(), $this->isWcActive()); |
| 33 | } |
| 34 | /** |
| 35 | * Get current PHP version. |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | protected function phpVersion() : string |
| 40 | { |
| 41 | try { |
| 42 | return (string) $this->versionFactory->createVersionFromString((string) \phpversion()); |
| 43 | } catch (Exception $exception) { |
| 44 | do_action($this->eventNameEnvironmentValidationFailed, ['reason' => 'couldn\'t get PHP version', 'details' => $exception->getMessage()]); |
| 45 | return ''; |
| 46 | } |
| 47 | } |
| 48 | /** |
| 49 | * Get current WP version. |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | protected function wpVersion() : string |
| 54 | { |
| 55 | global $wp_version; |
| 56 | try { |
| 57 | return (string) $this->versionFactory->createVersionFromString((string) $wp_version); |
| 58 | } catch (Exception $exception) { |
| 59 | do_action($this->eventNameEnvironmentValidationFailed, ['reason' => 'couldn\'t get WordPress version', 'details' => $exception->getMessage()]); |
| 60 | return ''; |
| 61 | } |
| 62 | } |
| 63 | /** |
| 64 | * Get current WC version. |
| 65 | * |
| 66 | * @return string |
| 67 | */ |
| 68 | protected function wcVersion() : string |
| 69 | { |
| 70 | if (!\defined('WC_VERSION')) { |
| 71 | return ''; |
| 72 | } |
| 73 | try { |
| 74 | return (string) $this->versionFactory->createVersionFromString(WC_VERSION); |
| 75 | } catch (Exception $exception) { |
| 76 | do_action($this->eventNameEnvironmentValidationFailed, ['reason' => 'couldn\'t get WooCommerce version', 'details' => $exception->getMessage()]); |
| 77 | return ''; |
| 78 | } |
| 79 | } |
| 80 | /** |
| 81 | * Check whether WC active. |
| 82 | * |
| 83 | * @return bool |
| 84 | */ |
| 85 | protected function isWcActive() : bool |
| 86 | { |
| 87 | return \class_exists(WooCommerce::class); |
| 88 | } |
| 89 | } |
| 90 |