瀏覽代碼

Move method from model to the Logo service

Bubka 3 年之前
父節點
當前提交
9b634dd55f
共有 2 個文件被更改,包括 37 次插入14 次删除
  1. 5 10
      app/Models/TwoFAccount.php
  2. 32 4
      app/Services/LogoService.php

+ 5 - 10
app/Models/TwoFAccount.php

@@ -320,7 +320,7 @@ class TwoFAccount extends Model implements Sortable
      */
     public function getOTP()
     {
-        Log::info(sprintf('OTP requested for TwoFAccount #%s', $this->id));
+        Log::info(sprintf('OTP requested for TwoFAccount (%s)', $this->id ? 'id:'.$this->id: 'preview'));
 
         // Early exit if the model has an undecipherable secret
         if (strtolower($this->secret) === __('errors.indecipherable')) {
@@ -352,7 +352,7 @@ class TwoFAccount extends Model implements Sortable
 
             }
 
-            Log::info(sprintf('New OTP generated for TwoFAccount #%s', $this->id));
+            Log::info(sprintf('New OTP generated for TwoFAccount (%s)', $this->id ? 'id:'.$this->id: 'preview'));
     
             return $OtpDto;
 
@@ -441,7 +441,7 @@ class TwoFAccount extends Model implements Sortable
             $this->icon = $this->storeImageAsIcon($this->generator->getParameter('image'));
         }
         if (!$this->icon) {
-            $this->icon = $this->defaultLogo();
+            $this->icon = $this->getDefaultIcon();
         }    
 
         Log::info(sprintf('TwoFAccount filled with an URI'));
@@ -584,16 +584,11 @@ class TwoFAccount extends Model implements Sortable
      * 
      * @return string|null The icon
      */
-    private function defaultLogo()
+    private function getDefaultIcon()
     {
         $logoService = App::make(LogoService::class);
-        $logoFilename = $logoService->getLogo($this->service);
 
-        if ($logoFilename) {
-            $newFilename = Str::random(40).'.svg';
-            return Storage::disk('icons')->put($newFilename, Storage::disk('logos')->get($logoFilename)) ? $newFilename : null;
-        }
-        else return null;
+        return $logoService->getIcon($this->service);
     }
 
 

+ 32 - 4
app/Services/LogoService.php

@@ -5,6 +5,7 @@ namespace App\Services;
 use Illuminate\Support\Facades\Http;
 use Illuminate\Support\Facades\Storage;
 use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Str;
 
 class LogoService
 {
@@ -26,22 +27,40 @@ class LogoService
     }
 
 
+    /**
+     * Fetch a logo for the given service and set it as an icon
+     * 
+     * @param string $serviceName Name of the service to fetch a logo for
+     * @return string|null The icon filename or null if no logo has been found
+     */
+    public function getIcon(string $serviceName)
+    {
+        $logoFilename = $this->getLogo($serviceName);
+
+        if ($logoFilename) {
+            $newFilename = Str::random(40).'.svg';
+            return Storage::disk('icons')->put($newFilename, Storage::disk('logos')->get($logoFilename)) ? $newFilename : null;
+        }
+        else return null;
+    }
+
+
     /**
      * Return the logo's filename for a given service
      * 
      * @param string $serviceName Name of the service to fetch a logo for
      * @return string|null The logo filename or null if no logo has been found
      */
-    public function getLogo(string $serviceName) : string
+    public function getLogo(string $serviceName)
     {
-        $domain = $this->tfas->get(strtolower($serviceName));
+        $domain = $this->tfas->get($this->cleanDomain($serviceName));
         $logoFilename = $domain.'.svg';
 
         if ($domain && !Storage::disk('logos')->exists($logoFilename)) {
             $this->fetchLogo($logoFilename);
         }
 
-        return Storage::disk('logos')->exists($logoFilename) ? $logoFilename : '';
+        return Storage::disk('logos')->exists($logoFilename) ? $logoFilename : null;
     }
 
 
@@ -97,7 +116,7 @@ class LogoService
 
 
     /**
-     * Fetch a logo and store it to the disk
+     * Fetch and cache a logo from 2fa.Directory repository
      * 
      * @param string $logoFile Logo filename to fetch
      * @return void
@@ -118,4 +137,13 @@ class LogoService
             Log::error(sprintf('Fetching of logo "%s" failed.', $logoFile));
         }
     }
+
+
+    /**
+     * 
+     */
+    protected function cleanDomain($domain) : string
+    {
+        return strtolower(str_replace(['+'], ['plus'], $domain));
+    }
 }