Clean up code
authorjoshuaruesweg <ruesweg@woltlab.com>
Thu, 4 Mar 2021 18:21:45 +0000 (19:21 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 16 Mar 2021 15:19:13 +0000 (16:19 +0100)
wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php
wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrlAction.class.php
wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php
wcfsetup/install/files/lib/system/message/unfurl/exception/DownloadFailed.class.php
wcfsetup/install/files/lib/system/message/unfurl/exception/ParsingFailed.class.php
wcfsetup/install/files/lib/system/message/unfurl/exception/UrlInaccessible.class.php

index 0ee207b4a14f3ae965f532bcdba2aa2e58fceccb..4f3094b5bce6da827e998cecafc9d89d981b49b3 100644 (file)
@@ -92,7 +92,7 @@ class UnfurlUrl extends DatabaseObject
      *
      * @throws \InvalidArgumentException If the given URL is invalid.
      */
-    public static function getByUrl(string $url): self
+    public static function getByUrl(string $url): ?self
     {
         if (!Url::is($url)) {
             throw new \InvalidArgumentException("Given URL is not a valid URL.");
@@ -105,7 +105,7 @@ class UnfurlUrl extends DatabaseObject
         $statement->execute([\sha1($url)]);
         $row = $statement->fetchArray();
         if (!$row) {
-            $row = [];
+            return null;
         }
 
         return new self(null, $row);
index 206fb74d072cbe9542c61720af5bc4e692225d7d..634242519425ce5b9a1fd4c5312a9cf7dc9a3772 100644 (file)
@@ -46,7 +46,7 @@ class UnfurlUrlAction extends AbstractDatabaseObjectAction
     {
         $object = UnfurlUrl::getByUrl($this->parameters['data']['url']);
 
-        if (!$object->urlID) {
+        if (!$object) {
             $returnValues = (new self([], 'create', [
                 'data' => [
                     'url' => $this->parameters['data']['url'],
index 0b382176884de08cb55775638d3a8853a548c36c..883d6d8c5487ee2d4c31dd248400338d3f412623 100644 (file)
@@ -69,52 +69,52 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob
 
             if (empty(StringUtil::trim($unfurlResponse->getTitle()))) {
                 $this->save(UnfurlUrl::STATUS_REJECTED);
-            } else {
-                $title = StringUtil::truncate($unfurlResponse->getTitle(), 255);
-                if ($unfurlResponse->getDescription() !== null) {
-                    $description = StringUtil::truncate($unfurlResponse->getDescription(), 500);
-                } else {
-                    $description = "";
-                }
+                return;
+            }
 
-                if ($unfurlResponse->getImageUrl()) {
-                    try {
-                        $image = $this->downloadImage($unfurlResponse->getImage());
-                        $imageData = \getimagesizefromstring($image);
-                        if ($imageData !== false) {
-                            $imageType = $this->validateImage($imageData);
-                            if (!(MODULE_IMAGE_PROXY || IMAGE_ALLOW_EXTERNAL_SOURCE)) {
-                                $imageHash = $this->saveImage($imageData, $image);
-                            } else {
-                                $imageHash = "";
-                            }
-                        } else {
-                            $imageType = UnfurlUrl::IMAGE_NO_IMAGE;
-                        }
+            $title = StringUtil::truncate($unfurlResponse->getTitle(), 255);
+            $description = "";
+            if ($unfurlResponse->getDescription()) {
+                $description = StringUtil::truncate($unfurlResponse->getDescription());
+            }
 
-                        if ($imageType === UnfurlUrl::IMAGE_NO_IMAGE) {
-                            $imageUrl = $imageHash = "";
+            if ($unfurlResponse->getImageUrl()) {
+                try {
+                    $image = $this->downloadImage($unfurlResponse->getImage());
+                    $imageData = \getimagesizefromstring($image);
+                    if ($imageData !== false) {
+                        $imageType = $this->validateImage($imageData);
+                        if (!(MODULE_IMAGE_PROXY || IMAGE_ALLOW_EXTERNAL_SOURCE)) {
+                            $imageHash = $this->saveImage($imageData, $image);
                         } else {
-                            $imageUrl = $unfurlResponse->getImageUrl();
+                            $imageHash = "";
                         }
-                    } catch (UrlInaccessible | DownloadFailed $e) {
+                    } else {
                         $imageType = UnfurlUrl::IMAGE_NO_IMAGE;
+                    }
+
+                    if ($imageType === UnfurlUrl::IMAGE_NO_IMAGE) {
                         $imageUrl = $imageHash = "";
+                    } else {
+                        $imageUrl = $unfurlResponse->getImageUrl();
                     }
-                } else {
+                } catch (UrlInaccessible | DownloadFailed $e) {
                     $imageType = UnfurlUrl::IMAGE_NO_IMAGE;
                     $imageUrl = $imageHash = "";
                 }
-
-                $this->save(
-                    UnfurlUrl::STATUS_SUCCESSFUL,
-                    $title,
-                    $description,
-                    $imageType,
-                    $imageUrl,
-                    $imageHash
-                );
+            } else {
+                $imageType = UnfurlUrl::IMAGE_NO_IMAGE;
+                $imageUrl = $imageHash = "";
             }
+
+            $this->save(
+                UnfurlUrl::STATUS_SUCCESSFUL,
+                $title,
+                $description,
+                $imageType,
+                $imageUrl,
+                $imageHash
+            );
         } catch (UrlInaccessible | ParsingFailed $e) {
             if (\ENABLE_DEBUG_MODE) {
                 \wcf\functions\exception\logThrowable($e);
index 4d163ee16240210381af8b0e97048229474aea52..34cbf2c5d04107e123d7b300a24bb192027340c6 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace wcf\system\message\unfurl\exception;
 
-use Exception;
-
 /**
  * Denotes a (temporary) download failed. It can be retried later.
  *
@@ -13,6 +11,6 @@ use Exception;
  * @package     WoltLabSuite\Core\System\Message\Unfurl
  * @since       5.4
  */
-class DownloadFailed extends Exception
+class DownloadFailed extends \Exception
 {
 }
index c11296a81dcf8f342dc23768bb1f490784faefa1..b2fe728fffa8a399ef6d9bd1bdd228d909c34418 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace wcf\system\message\unfurl\exception;
 
-use Exception;
-
 /**
  * Denotes a permanent parsing body failed. It should not be retried later.
  *
@@ -13,6 +11,6 @@ use Exception;
  * @package     WoltLabSuite\Core\System\Message\Unfurl
  * @since       5.4
  */
-class ParsingFailed extends Exception
+class ParsingFailed extends \Exception
 {
 }
index 3b0586a52766c09e5be06bcbd70052d52a7c3f13..4d0a8519730f671f66b4c340b2b9f79333f89315 100644 (file)
@@ -2,8 +2,6 @@
 
 namespace wcf\system\message\unfurl\exception;
 
-use Exception;
-
 /**
  * Denotes a permanent failing url, because the url is inaccessible.
  *
@@ -13,6 +11,6 @@ use Exception;
  * @package     WoltLabSuite\Core\System\Message\Unfurl
  * @since       5.4
  */
-class UrlInaccessible extends Exception
+class UrlInaccessible extends \Exception
 {
 }