Skip to content
Snippets Groups Projects
Commit cbf4489f authored by daniel_mm02's avatar daniel_mm02 Committed by Alvaro Hurtado
Browse files

Issue #3452045: Move endpoint cache to a new service

parent a69f9169
No related branches found
No related tags found
1 merge request!12Add the interface from IconsCache, move the code from IconifyService to...
Pipeline #192544 passed
......@@ -4,7 +4,7 @@ services:
arguments: [
'@http_client',
'@logger.channel.default',
'@file_system'
'@iconify_icons.icons_cache'
]
iconify_icons.twig.IconifyIcon:
......@@ -13,3 +13,7 @@ services:
- '@iconify_icons.iconify_service'
tags:
- { name: twig.extension }
iconify_icons.icons_cache:
class: Drupal\iconify_icons\IconsCache
arguments: ['@file_system']
......@@ -2,7 +2,6 @@
namespace Drupal\iconify_icons;
use Drupal\Core\File\FileSystemInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use Psr\Log\LoggerInterface;
......@@ -33,11 +32,11 @@ class IconifyService implements IconifyServiceInterface {
protected $logger;
/**
* The fileSystem service.
* The cache service.
*
* @var \Drupal\Core\File\FileSystemInterface
* @var \Drupal\iconify_icons\IconsCacheInterface
*/
protected $fileSystem;
protected $cache;
/**
* IconifyService constructor.
......@@ -46,13 +45,13 @@ class IconifyService implements IconifyServiceInterface {
* The HTTP client.
* @param \Psr\Log\LoggerInterface $logger
* The Logger service.
* @param \Drupal\Core\File\FileSystemInterface $fileSystem
* The FileSystem service.
* @param \Drupal\iconify_icons\IconsCacheInterface $cache
* The cache service.
*/
public function __construct(ClientInterface $http_client, LoggerInterface $logger, FileSystemInterface $fileSystem) {
public function __construct(ClientInterface $http_client, LoggerInterface $logger, IconsCacheInterface $cache) {
$this->httpClient = $http_client;
$this->logger = $logger;
$this->fileSystem = $fileSystem;
$this->cache = $cache;
}
/**
......@@ -121,54 +120,6 @@ class IconifyService implements IconifyServiceInterface {
}
}
/**
* Calculates a file system path to cache an icon based on its parameters.
*
* @param string $collection
* The collection.
* @param string $icon_name
* The icon name.
* @param array $query_options
* Query options.
*
* @return string
* The Path in drupal file system.
*/
protected function iconSettingsToPath(string $collection, string $icon_name, array $query_options) {
return 'public://iconify-icons/' . implode('/', [
$collection,
$icon_name,
$query_options['width'],
$query_options['height'],
$query_options['color'],
$query_options['flip'],
$query_options['rotate'],
]);
}
/**
* Gets the icon from cache and return empty string if it doesn't exist.
*
* @param string $collection
* The collection.
* @param string $icon_name
* The icon name.
* @param array $query_options
* Query options.
*
* @return string
* The svg or '' if it doesn't exist yet.
*/
protected function getIconFromCache(string $collection, string $icon_name, array $query_options): string {
// Translate settings to path.
$path = $this->iconSettingsToPath($collection, $icon_name, $query_options);
if ($this->fileSystem->realpath($path . '/' . $icon_name . '.svg')) {
return file_get_contents($path . '/' . $icon_name . '.svg');
}
return '';
}
/**
* Qualifies the default parameters and sets if any are missing.
*
......@@ -193,8 +144,8 @@ class IconifyService implements IconifyServiceInterface {
*/
public function generateSvg(string $collection, string $icon_name, array $parameters = []): string {
$parameters = $this->setDefaultParameters($parameters);
if ($icon = $this->getIconFromCache($collection, $icon_name, $parameters)) {
return $icon;
if ($this->cache->checkIcon($collection, $icon_name, $parameters)) {
return $this->cache->getIcon($collection, $icon_name, $parameters);
}
try {
$promise = $this->httpClient->requestAsync('GET', sprintf($this::DESIGN_DOWNLOAD_API_ENDPOINT, $collection, $icon_name), [
......@@ -204,13 +155,10 @@ class IconifyService implements IconifyServiceInterface {
// Wait for the asynchronous request to complete.
$response = $promise->wait();
$directory = $this->iconSettingsToPath($collection, $icon_name, $parameters);
$icon = $response->getBody()->getContents();
if ($this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
$filepath = $directory . '/' . $icon_name . '.svg';
$this->fileSystem->saveData($icon, $filepath, FileSystemInterface::EXISTS_REPLACE);
}
$this->cache->setIcon($collection, $icon_name, $icon, $parameters);
return $icon;
}
catch (RequestException $e) {
......
<?php
namespace Drupal\iconify_icons;
use Drupal\Core\File\FileSystemInterface;
/**
* Service description.
*/
class IconsCache implements IconsCacheInterface {
/**
* The file handler.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Constructs an IconsCache object.
*
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file handler.
*/
public function __construct(FileSystemInterface $file_system) {
$this->fileSystem = $file_system;
}
/**
* Calculates a file system path to cache an icon based on its parameters.
*
* @param string $collection
* The collection.
* @param string $icon_name
* The icon name.
* @param array $query_options
* Query options.
*
* @return string
* The Path in drupal file system.
*/
protected function iconSettingsToPath(string $collection, string $icon_name, array $query_options) {
return 'public://iconify-icons/' . implode('/', [
$collection,
$icon_name,
$query_options['width'],
$query_options['height'],
$query_options['color'],
$query_options['flip'],
$query_options['rotate'],
]);
}
/**
* {@inheritdoc}
*/
public function getIcon(string $collection, string $icon_name, array $query_options): string {
// Translate settings to path.
$path = $this->iconSettingsToPath($collection, $icon_name, $query_options);
if ($this->fileSystem->realpath($path . '/' . $icon_name . '.svg')) {
return file_get_contents($path . '/' . $icon_name . '.svg');
}
return '';
}
/**
* {@inheritdoc}
*/
public function setIcon(string $collection, string $icon_name, string $icon, array $parameters): bool {
$directory = $this->iconSettingsToPath($collection, $icon_name, $parameters);
if ($this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
$filepath = $directory . '/' . $icon_name . '.svg';
$this->fileSystem->saveData($icon, $filepath, FileSystemInterface::EXISTS_REPLACE);
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function checkIcon(string $collection, string $icon_name, array $parameters): bool {
// Translate settings to path.
$path = $this->iconSettingsToPath($collection, $icon_name, $parameters);
if ($this->fileSystem->realpath($path . '/' . $icon_name . '.svg')) {
return TRUE;
}
return FALSE;
}
}
<?php
namespace Drupal\iconify_icons;
/**
* Interface for Cache service.
*/
interface IconsCacheInterface {
/**
* Gets the icon from cache and return empty string if it doesn't exist.
*
* @param string $collection
* The collection.
* @param string $icon_name
* The icon name.
* @param array $query_options
* Query options.
*
* @return string
* The svg or '' if it doesn't exist yet.
*/
public function getIcon(string $collection, string $icon_name, array $query_options): string;
/**
* Set the icon in cache.
*
* @param string $collection
* The Path in drupal file system.
* @param string $icon_name
* The icon name.
* @param string $icon
* The svg or '' if it doesn't exist yet.
* @param string $parameters
* Query options.
*
* @return bool
* returns TRUE if the method works correctly, FALSE otherwise.
*/
public function setIcon(string $collection, string $icon_name, string $icon, array $parameters): bool;
/**
* Checks if an icon is in cache.
*
* @param string $collection
* The collection.
* @param string $icon_name
* The icon name.
* @param array $parameters
* Query options.
*
* @return bool
* True if the icon is in cache, false in other case.
*/
public function checkIcon(string $collection, string $icon_name, array $parameters): bool;
}
......@@ -2,8 +2,8 @@
namespace Drupal\Tests\iconify_icons\Unit;
use Drupal\Core\File\FileSystemInterface;
use Drupal\iconify_icons\IconifyService;
use Drupal\iconify_icons\IconsCacheInterface;
use Drupal\Tests\UnitTestCase;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Promise\PromiseInterface;
......@@ -40,11 +40,11 @@ class IconifyServiceTest extends UnitTestCase {
protected $iconifyService;
/**
* The fileSystem service.
* The cache service.
*
* @var \Drupal\Core\File\FileSystemInterface
* @var \Drupal\iconify_icons\IconsCacheInterface
*/
protected $fileSystem;
protected $cache;
/**
* {@inheritdoc}
......@@ -53,8 +53,8 @@ class IconifyServiceTest extends UnitTestCase {
parent::setUp();
$this->httpClient = $this->createMock(ClientInterface::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->fileSystem = $this->createMock(FileSystemInterface::class);
$this->iconifyService = new IconifyService($this->httpClient, $this->logger, $this->fileSystem);
$this->cache = $this->createMock(IconsCacheInterface::class);
$this->iconifyService = new IconifyService($this->httpClient, $this->logger, $this->cache);
}
/**
......
<?php
namespace Drupal\Tests\iconify_icons\Unit;
use Drupal\Core\File\FileSystemInterface;
use Drupal\iconify_icons\IconsCache;
use Drupal\Tests\UnitTestCase;
/**
* Tests for IconsCache Service.
*
* @group iconify_icons
*/
class IconsCacheTest extends UnitTestCase {
/**
* The file handler.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The tested ban iconsCache.
*
* @var \Drupal\iconify_icons\IconsCache
*/
protected $iconsCache;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->fileSystem = $this->createMock(FileSystemInterface::class);
$this->iconsCache = new IconsCache($this->fileSystem);
}
/**
* Tests getIcon method.
*/
public function testGetIcon() {
$collection = 'collection_test';
$icon_name = 'icon_1';
$query_options = [
'width' => '50',
'height' => '50',
'color' => 'red',
'flip' => 'horizontal',
'rotate' => '90',
];
$ok_response = '';
$this->fileSystem->method('realpath')->willReturn(FALSE);
$assert = $this->iconsCache->getIcon($collection, $icon_name, $query_options);
$this->assertEquals($ok_response, $assert);
}
/**
* Tests setIcon method.
*/
public function testSetIcon() {
$collection = 'collection_test';
$directory = 'public://iconify-icons/collection_test/icon_1/50/50/red/horizontal/90';
$icon_name = 'icon_1';
$icon = 'icon';
$parameters = [
'width' => '50',
'height' => '50',
'color' => 'red',
'flip' => 'horizontal',
'rotate' => '90',
];
$this->fileSystem->method('prepareDirectory')->willReturn(TRUE);
$this->fileSystem->method('saveData')->willReturn(TRUE);
$assert = $this->iconsCache->setIcon($collection, $icon_name, $icon, $parameters);
$this->assertTrue($assert);
}
/**
* Tests checkIcon method.
*/
public function testCheckIcon() {
$collection = 'collection_test';
$icon_name = 'icon_1';
$parameters = [
'width' => '50',
'height' => '50',
'color' => 'red',
'flip' => 'horizontal',
'rotate' => '90',
];
$this->fileSystem->method('realpath')->willReturn(TRUE);
$assert = $this->iconsCache->checkIcon($collection, $icon_name, $parameters);
$this->assertTrue($assert);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment