Cleanly separate the LicenseApi service and the LicenseData value object
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 27 Sep 2023 13:22:33 +0000 (15:22 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 27 Sep 2023 13:40:56 +0000 (15:40 +0200)
wcfsetup/install/files/acp/update_com.woltlab.wcf_6.0_license.php
wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php
wcfsetup/install/files/lib/acp/form/LicenseEditForm.class.php
wcfsetup/install/files/lib/acp/page/LicensePage.class.php
wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php
wcfsetup/install/files/lib/data/package/update/PackageUpdateAction.class.php
wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php
wcfsetup/install/files/lib/system/cronjob/GetUpdateInfoCronjob.class.php
wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php

index 4280047a299d23462b0e2ca5d9741eecef6e41b2..a208a824311e1d251d15101826b612105e61a7dd 100644 (file)
@@ -13,8 +13,9 @@ use wcf\data\package\update\server\PackageUpdateServerEditor;
 use wcf\system\package\license\LicenseApi;
 
 try {
-    $licenseApi = LicenseApi::fetchFromRemote();
-    $licenseApi->updateLicenseFile();
+    $licenseApi = new LicenseApi();
+    $licenseData = $licenseApi->fetchFromRemote();
+    $licenseApi->updateLicenseFile($licenseData);
 
     // If we’re still here it means that the credentials are actually valid. Now
     // we can check if the credentials for both servers are in sync, because
index 41f60590dc06871c6ca209721702d7127b9e63e8..069200c49cf99af9e274a2b79455259cb8cf5b67 100644 (file)
@@ -19,6 +19,7 @@ use wcf\system\form\builder\field\validation\FormFieldValidationError;
 use wcf\system\form\builder\field\validation\FormFieldValidator;
 use wcf\system\package\license\exception\ParsingFailed;
 use wcf\system\package\license\LicenseApi;
+use wcf\system\package\license\LicenseData;
 use wcf\system\request\LinkHandler;
 use wcf\util\HeaderUtil;
 
@@ -39,6 +40,8 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm
 
     private LicenseApi $licenseApi;
 
+    private LicenseData $licenseData;
+
     /**
      * @inheritDoc
      */
@@ -58,6 +61,8 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm
     {
         parent::createForm();
 
+        $this->licenseApi = new LicenseApi();
+
         $this->form->appendChildren([
             $credentialsContainer = FormContainer::create('credentials')
                 ->label('wcf.acp.firstTimeSetup.license.credentials')
@@ -81,7 +86,7 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm
                             \assert($licenseNo instanceof TextFormField);
 
                             try {
-                                $this->licenseApi = LicenseApi::fetchFromRemote([
+                                $this->licenseData = $this->licenseApi->fetchFromRemote([
                                     'username' => $licenseNo->getValue(),
                                     'password' => $serialNo->getValue(),
                                 ]);
@@ -152,11 +157,11 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm
             Option::getOptionByName('first_time_setup_state')->optionID => 1,
         ];
 
-        if (isset($this->licenseApi)) {
-            $this->licenseApi->updateLicenseFile();
+        if (isset($this->licenseData)) {
+            $this->licenseApi->updateLicenseFile($this->licenseData);
 
-            if (isset($this->licenseApi->getData()->license['authCode'])) {
-                $optionData[Option::getOptionByName('package_server_auth_code')->optionID] = $this->licenseApi->getData()->license['authCode'];
+            if (isset($this->licenseData->license['authCode'])) {
+                $optionData[Option::getOptionByName('package_server_auth_code')->optionID] = $this->licenseData->license['authCode'];
             }
         }
 
index 7a932838a97bf6720ea7057254e8bd6bddd815f3..252910d64ba5b386b977749baf4168b1dde4f9c0 100644 (file)
@@ -21,6 +21,7 @@ use wcf\system\form\builder\field\validation\FormFieldValidationError;
 use wcf\system\form\builder\field\validation\FormFieldValidator;
 use wcf\system\package\license\exception\ParsingFailed;
 use wcf\system\package\license\LicenseApi;
+use wcf\system\package\license\LicenseData;
 use wcf\system\request\LinkHandler;
 
 /**
@@ -50,6 +51,8 @@ final class LicenseEditForm extends AbstractFormBuilderForm
 
     private LicenseApi $licenseApi;
 
+    private LicenseData $licenseData;
+
     private string $url;
 
     /**
@@ -72,6 +75,8 @@ final class LicenseEditForm extends AbstractFormBuilderForm
     {
         parent::createForm();
 
+        $this->licenseApi = new LicenseApi();
+
         $licenseNo = '';
         $serialNo = '';
         $authData = PackageUpdateServer::getWoltLabUpdateServer()->getAuthData();
@@ -105,7 +110,7 @@ final class LicenseEditForm extends AbstractFormBuilderForm
                             \assert($licenseNo instanceof TextFormField);
 
                             try {
-                                $this->licenseApi = LicenseApi::fetchFromRemote([
+                                $this->licenseData = $this->licenseApi->fetchFromRemote([
                                     'username' => $licenseNo->getValue(),
                                     'password' => $serialNo->getValue(),
                                 ]);
@@ -201,12 +206,12 @@ final class LicenseEditForm extends AbstractFormBuilderForm
         }
 
         $authCode = '';
-        if (isset($this->licenseApi)) {
-            $this->licenseApi->updateLicenseFile();
+        if (isset($this->licenseData)) {
+            $this->licenseApi->updateLicenseFile($this->licenseData);
 
-            $authCode = $this->licenseApi->getData()->license['authCode'] ?? '';
+            $authCode = $this->licenseData->license['authCode'] ?? '';
         } else {
-            LicenseApi::removeLicenseFile();
+            $this->licenseApi->removeLicenseFile();
         }
 
         $objectAction = new OptionAction(
index c72f1b702493faf507e039e6136401acc7f7eb2d..6ecb670e6e95ee8e66bd57e869fc74d9f16f6a49 100644 (file)
@@ -60,10 +60,9 @@ final class LicensePage extends AbstractPage
 
         (new PackageUpdateAction([], 'refreshDatabase'))->executeAction();
 
-        $licenseApi = LicenseApi::fetchFromRemote();
-        $licenseApi->updateLicenseFile();
-
-        $this->licenseData = $licenseApi->getData();
+        $licenseApi = new LicenseApi();
+        $this->licenseData = $licenseApi->fetchFromRemote();
+        $licenseApi->updateLicenseFile($this->licenseData);
 
         $identifiers = \array_merge(
             \array_keys($this->licenseData->woltlab),
index fb03c436f7c71ffacf8de0ae498e3ec15518bc3a..1f586251faf906ef9d2b9ebe1b23174010b44238 100644 (file)
@@ -65,9 +65,8 @@ return static function (): void {
     });
 
     try {
-        $licenseApi = LicenseApi::readFromFile();
-        if ($licenseApi !== null) {
-            $licenseData = $licenseApi->getData();
+        $licenseData = LicenseApi::readFromFile();
+        if ($licenseData !== null) {
             $brandingFree = $licenseData->woltlab['com.woltlab.brandingFree'] ?? '0.0';
             $expiresAt = $licenseData->license['expiryDates']['com.woltlab.brandingFree'] ?? \TIME_NOW;
             if ($brandingFree !== '0.0' && $expiresAt >= \TIME_NOW) {
index 0b2f0c490ad0120a86fa6b04fb8fb72385c8a7ca..fe35c0388acd9ff986ca4a1e414def62200b28d8 100644 (file)
@@ -613,8 +613,9 @@ class PackageUpdateAction extends AbstractDatabaseObjectAction
 
         // Try to update the cached license data to check for recent purchases.
         if (LicenseApi::hasLicenseCredentials()) {
-            $licenseApi = LicenseApi::fetchFromRemote();
-            $licenseApi->updateLicenseFile();
+            $licenseApi = new LicenseApi();
+            $licenseData = $licenseApi->fetchFromRemote();
+            $licenseApi->updateLicenseFile($licenseData);
         }
 
         $updates = PackageUpdateDispatcher::getInstance()->getAvailableUpdates();
index f4a7c11e2308edf50081790b4440e08d476a4473..0109b60f4c3b1f91cd72912725afa6baeec2015c 100644 (file)
@@ -328,11 +328,11 @@ class BBCodeHandler extends SingletonFactory
      */
     public function getCkeditorLicenseKey(): string
     {
-        $licenseApi = LicenseApi::readFromFile();
-        if ($licenseApi === null) {
+        $licenseData = LicenseApi::readFromFile();
+        if ($licenseData === null) {
             return '';
         }
 
-        return $licenseApi->getData()->license['ckeditorLicenseKey'] ?? '';
+        return $licenseData->license['ckeditorLicenseKey'] ?? '';
     }
 }
index 9d0bde92edf0a4d0dfbb436ffdfd99eb377dfbc7..a0b5bd8a2bbcef5e2e9aba61f108a030a4a79414 100644 (file)
@@ -51,8 +51,9 @@ class GetUpdateInfoCronjob extends AbstractCronjob
     private function refreshLicenseFile(): void
     {
         try {
-            $licenseApi = LicenseApi::fetchFromRemote();
-            $licenseApi->updateLicenseFile();
+            $licenseApi = new LicenseApi();
+            $licenseData = $licenseApi->fetchFromRemote();
+            $licenseApi->updateLicenseFile($licenseData);
         } catch (\Throwable) {
             // This is a “silent” operation that should not interrupt the
             // execution of cronjobs in case of an error.
index 82c9d77b6444cb5291bf8f8866271274aa7b70ab..c51a028307bd37fe236d501850000c9065a8a4cb 100644 (file)
@@ -21,21 +21,9 @@ use wcf\system\package\license\exception\ParsingFailed;
  */
 final class LicenseApi
 {
-    private readonly LicenseData $data;
-
     private const LICENSE_FILE = \WCF_DIR . 'license.php';
 
-    private function __construct(LicenseData $data)
-    {
-        $this->data = $data;
-    }
-
-    public function getData(): LicenseData
-    {
-        return $this->data;
-    }
-
-    public function updateLicenseFile(): void
+    public function updateLicenseFile(LicenseData $data): void
     {
         @\file_put_contents(
             self::LICENSE_FILE,
@@ -46,7 +34,7 @@ final class LicenseApi
                 return unserialize(%s);
                 EOT,
                 \gmdate('r', \TIME_NOW),
-                \var_export(\serialize($this->data), true),
+                \var_export(\serialize($data), true),
             )
         );
     }
@@ -66,7 +54,7 @@ final class LicenseApi
         }
     }
 
-    public static function fetchFromRemote(array $authData = []): LicenseApi
+    public static function fetchFromRemote(array $authData = []): LicenseData
     {
         if ($authData === []) {
             if (!self::hasLicenseCredentials()) {
@@ -91,19 +79,19 @@ final class LicenseApi
 
         $response = HttpFactory::makeClientWithTimeout(5)->send($request);
 
-        return new LicenseApi(self::parseLicenseData($response->getBody()));
+        return self::parseLicenseData($response->getBody());
     }
 
-    public static function readFromFile(): ?LicenseApi
+    public static function readFromFile(): ?LicenseData
     {
         if (!\is_readable(self::LICENSE_FILE)) {
             return null;
         }
 
-        return new LicenseApi(require(self::LICENSE_FILE));
+        return require(self::LICENSE_FILE);
     }
 
-    public static function removeLicenseFile(): void
+    public function removeLicenseFile(): void
     {
         if (!\file_exists(self::LICENSE_FILE)) {
             return;