Move method from model to the Logo service

This commit is contained in:
Bubka 2022-07-20 13:31:51 +02:00
parent e540e2bb26
commit 9b634dd55f
2 changed files with 37 additions and 14 deletions

View File

@ -320,7 +320,7 @@ public function setCounterAttribute($value)
*/
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 @@ public function getOTP()
}
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 @@ public function fillWithURI(string $uri, bool $isSteamTotp = false)
$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 @@ private function storeImageAsIcon(string $url)
*
* @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);
}

View File

@ -5,6 +5,7 @@
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 @@ public function __construct()
}
/**
* 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 @@ protected function cacheTfaDirectorySource() : void
/**
* 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 @@ protected function fetchLogo(string $logoFile) : void
Log::error(sprintf('Fetching of logo "%s" failed.', $logoFile));
}
}
/**
*
*/
protected function cleanDomain($domain) : string
{
return strtolower(str_replace(['+'], ['plus'], $domain));
}
}