]> _ Git - psq.git/commitdiff
job generatelink
authorLouis Jeckel <louis.jeckel@outlook.com>
Thu, 3 Dec 2020 12:30:00 +0000 (13:30 +0100)
committerLouis Jeckel <louis.jeckel@outlook.com>
Thu, 3 Dec 2020 12:30:00 +0000 (13:30 +0100)
app/Jobs/GetTitleForLink.php [new file with mode: 0644]
app/TrackedLink.php

diff --git a/app/Jobs/GetTitleForLink.php b/app/Jobs/GetTitleForLink.php
new file mode 100644 (file)
index 0000000..436925c
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Invokables\GetPageTitleFromUrl;
+use App\TrackedLink;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class GetTitleForLink implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    /**
+     * @var TrackedLink
+     */
+    private TrackedLink $link;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct(TrackedLink $link)
+    {
+        $this->link = $link;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+
+        $title = (new GetPageTitleFromUrl)($this->link->target);
+
+        $this->link->update(['title' => $title]);
+    }
+}
index 5bf7368c456479779df7d4dc64442ba466c93ac8..2232a9c42f5dab0dad665a2fdd39f8498e26d797 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace App;
 
+use App\Jobs\GetTitleForLink;
 use Closure;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Str;
@@ -42,15 +43,16 @@ class TrackedLink extends Model
      */
     public static function shorten($url, PdfFile $file): TrackedLink
     {
-        /** @var TrackedLink $file */
+        /** @var TrackedLink $link */
         $link = self::query()->firstOrCreate([
             'target' => $url,
             'file_id' => $file->id,
         ], [
             'slug' => Str::random(8),
             'clicks' => 0,
-            'title' => (new Invokables\GetPageTitleFromUrl)($url)
+            'title' => $url
         ]);
+        dispatch(new GetTitleForLink($link));
         return $link;
 
     }