DotcomHelperFunctions.php
5 months ago
DotcomLicenseProvisioner.php
2 years ago
index.php
3 years ago
DotcomLicenseProvisioner.php
135 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\WPCOM; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\ErrorResponse; |
| 9 | use MailPoet\API\JSON\v1\Services; |
| 10 | use MailPoet\API\JSON\v1\Settings; |
| 11 | use MailPoet\Logging\LoggerFactory; |
| 12 | use WP_Error; |
| 13 | |
| 14 | /** |
| 15 | * This class is responsible for receiving and activating the license purchased from WP.com Marketplace. |
| 16 | */ |
| 17 | class DotcomLicenseProvisioner { |
| 18 | const EVENT_TYPE_PROVISION_LICENSE = 'provision_license'; |
| 19 | |
| 20 | /** @var LoggerFactory */ |
| 21 | private $loggerFactory; |
| 22 | |
| 23 | /** @var Settings */ |
| 24 | private $settings; |
| 25 | |
| 26 | /** @var Services */ |
| 27 | private $services; |
| 28 | |
| 29 | /** @var DotcomHelperFunctions */ |
| 30 | private $dotcomHelperFunctions; |
| 31 | |
| 32 | public function __construct( |
| 33 | LoggerFactory $loggerFactory, |
| 34 | Settings $settings, |
| 35 | Services $services, |
| 36 | DotcomHelperFunctions $dotcomHelperFunctions |
| 37 | ) { |
| 38 | $this->loggerFactory = $loggerFactory; |
| 39 | $this->settings = $settings; |
| 40 | $this->services = $services; |
| 41 | $this->dotcomHelperFunctions = $dotcomHelperFunctions; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Activates MSS and adds API key for subscriptions purchased from WP.com Marketplace. |
| 46 | * |
| 47 | * @param bool $result |
| 48 | * @param array $licensePayload |
| 49 | * @param string $eventType |
| 50 | * @return bool|WP_Error |
| 51 | */ |
| 52 | public function provisionLicense(bool $result, array $licensePayload, string $eventType) { |
| 53 | if (!$this->dotcomHelperFunctions->isAtomicPlatform() || $eventType !== self::EVENT_TYPE_PROVISION_LICENSE) { |
| 54 | return $result; |
| 55 | } |
| 56 | |
| 57 | $apiKey = $this->getKeyFromPayload($licensePayload); |
| 58 | if (is_wp_error($apiKey)) { |
| 59 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_PROVISIONING)->error( |
| 60 | 'key was not found in license payload' |
| 61 | ); |
| 62 | return $apiKey; |
| 63 | } |
| 64 | |
| 65 | return $this->activateMSS($apiKey); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns API key from license payload. |
| 70 | * @param array $licensePayload |
| 71 | * @return string|WP_Error |
| 72 | */ |
| 73 | private function getKeyFromPayload(array $licensePayload) { |
| 74 | if (isset($licensePayload['apiKey']) && is_string($licensePayload['apiKey'])) { |
| 75 | return $licensePayload['apiKey']; |
| 76 | } |
| 77 | |
| 78 | return new WP_Error('invalid_license_payload', 'Invalid license payload: Missing API key.'); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Saves the API key and activates MSS. |
| 83 | * @param string $apiKey |
| 84 | * @return true|WP_Error |
| 85 | */ |
| 86 | public function activateMSS(string $apiKey) { |
| 87 | $response = $this->settings->setKeyAndSetupMss($apiKey); |
| 88 | if ($response instanceof ErrorResponse) { |
| 89 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_PROVISIONING)->error( |
| 90 | 'Setting sending method and key failed', |
| 91 | ['$response' => $response] |
| 92 | ); |
| 93 | return new WP_Error('Provisioning failed setting the data', $this->concatMessages($response)); |
| 94 | } |
| 95 | |
| 96 | // This is necessary if the key changed but the sending method was already set to MailPoet |
| 97 | $response = $this->services->refreshMSSKeyStatus(); |
| 98 | if ($response instanceof ErrorResponse) { |
| 99 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_PROVISIONING)->error( |
| 100 | 'Refreshing the key failed', |
| 101 | ['$response' => $response] |
| 102 | ); |
| 103 | return new WP_Error('Provisioning failed activating the data', $this->concatMessages($response)); |
| 104 | } |
| 105 | |
| 106 | $response = $this->services->refreshPremiumKeyStatus(); |
| 107 | if ($response instanceof ErrorResponse) { |
| 108 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_PROVISIONING)->error( |
| 109 | 'Refreshing Premium key failed', |
| 110 | ['$response' => $response] |
| 111 | ); |
| 112 | return new WP_Error('Provisioning failed to verify api key access for MSS/premium', $this->concatMessages($response)); |
| 113 | } |
| 114 | |
| 115 | $this->loggerFactory->getLogger(LoggerFactory::TOPIC_PROVISIONING)->info( |
| 116 | 'License was provisioned' |
| 117 | ); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | private function concatMessages(ErrorResponse $response): string { |
| 122 | $data = $response->getData(); |
| 123 | $result = ''; |
| 124 | |
| 125 | if (empty($data) || !isset($data['errors'])) { |
| 126 | return $result; |
| 127 | } |
| 128 | |
| 129 | foreach ($data['errors'] as $error) { |
| 130 | $result .= $error['message'] . " "; |
| 131 | } |
| 132 | return $result; |
| 133 | } |
| 134 | } |
| 135 |