-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLUG-127: add psr16 cache adapter for truelayer client (#36)
- Loading branch information
1 parent
51c447a
commit 6a2ea8e
Showing
11 changed files
with
237 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace TrueLayer\Connect\Model\Cache; | ||
|
||
use Magento\Framework\App\Cache\Type\FrontendPool; | ||
use Magento\Framework\Cache\Frontend\Decorator\TagScope; | ||
|
||
class CacheType extends TagScope | ||
{ | ||
/** | ||
* Cache type code unique among all cache types | ||
* @var string | ||
*/ | ||
public const TYPE_IDENTIFIER = 'truelayer'; | ||
|
||
/** | ||
* The tag name that limits the cache cleaning scope within a particular tag | ||
* @var string | ||
*/ | ||
public const CACHE_TAG = 'TRUELAYER'; | ||
|
||
public function __construct(FrontendPool $cacheFrontendPool) | ||
{ | ||
parent::__construct( | ||
$cacheFrontendPool->get(self::TYPE_IDENTIFIER), | ||
self::CACHE_TAG | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace TrueLayer\Connect\Service\Cache; | ||
|
||
class InvalidArgumentException extends \InvalidArgumentException implements \Psr\Cache\InvalidArgumentException | ||
{ | ||
public function __construct(string $message) | ||
{ | ||
parent::__construct($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace TrueLayer\Connect\Service\Cache; | ||
|
||
use Psr\SimpleCache\CacheInterface; | ||
use TrueLayer\Connect\Model\Cache\CacheType; | ||
use TrueLayer\Connect\Service\Cache\InvalidArgumentException; | ||
|
||
class Psr16CacheAdapter implements CacheInterface | ||
{ | ||
private CacheType $cacheFrontend; | ||
|
||
public function __construct(CacheType $cacheFrontend) | ||
{ | ||
$this->cacheFrontend = $cacheFrontend; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(string $key, mixed $default = null): mixed | ||
{ | ||
$item = $this->cacheFrontend->load($key); | ||
|
||
if ($item === false) { | ||
return $default; | ||
} | ||
|
||
return unserialize($item); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set(string $key, $value, $ttl = null): bool | ||
{ | ||
$value = serialize($value); | ||
return $this->cacheFrontend->save( | ||
$value, | ||
$key, | ||
[CacheType::CACHE_TAG], | ||
$ttl | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete($key): bool | ||
{ | ||
return $this->cacheFrontend->remove($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear(): bool | ||
{ | ||
return $this->cacheFrontend->clean(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMultiple(iterable $keys, mixed $default = null): iterable | ||
{ | ||
if ($keys instanceof \Traversable) { | ||
$keys = iterator_to_array($keys, false); | ||
} | ||
$values = []; | ||
foreach ($keys as $key) { | ||
$values[$key] = $this->get($key, $default); | ||
} | ||
return $values; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @param iterable<int|string, mixed> $values | ||
*/ | ||
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool | ||
{ | ||
$stringKeyedValues = []; | ||
foreach ($values as $key => $value) { | ||
if (is_int($key)) { | ||
$key = (string) $key; | ||
} | ||
|
||
if (!is_string($key)) { | ||
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', gettype($key))); | ||
} | ||
|
||
$stringKeyedValues[$key] = $value; | ||
} | ||
$success = true; | ||
foreach ($stringKeyedValues as $key => $value) { | ||
$success = $this->set($key, $value, $ttl) && $success; | ||
} | ||
|
||
return $success; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteMultiple(iterable $keys): bool | ||
{ | ||
if ($keys instanceof \Traversable) { | ||
$keys = iterator_to_array($keys, false); | ||
} | ||
$success = true; | ||
foreach ($keys as $key) { | ||
$success = $this->delete($key) && $success; | ||
} | ||
return $success; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has(string $key): bool | ||
{ | ||
return $this->cacheFrontend->test($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace TrueLayer\Connect\Setup; | ||
|
||
use Magento\Framework\Setup\InstallSchemaInterface; | ||
use Magento\Framework\Setup\ModuleContextInterface; | ||
use Magento\Framework\Setup\SchemaSetupInterface; | ||
use Magento\Config\Model\ResourceModel\Config; | ||
use TrueLayer\Connect\Api\Config\RepositoryInterface; | ||
use TrueLayer\Connect\Api\Config\System\ConnectionInterface; | ||
use Magento\Framework\Encryption\EncryptorInterface; | ||
|
||
class Recurring implements InstallSchemaInterface | ||
{ | ||
public function __construct(private RepositoryInterface $configRepository, private EncryptorInterface $encryptor, private Config $resourceConfig) | ||
{ | ||
|
||
} | ||
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) | ||
{ | ||
$setup->startSetup(); | ||
|
||
$path = ConnectionInterface::XML_PATH_CACHE_ENCRYPTION_KEY; | ||
$value = bin2hex(openssl_random_pseudo_bytes(32)); | ||
$value = $this->encryptor->encrypt($value); | ||
$this->resourceConfig->saveConfig($path, $value, 'default', 0); | ||
|
||
$setup->endSetup(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd"> | ||
<type name="truelayer" translate="label,description" instance="TrueLayer\Connect\Model\Cache\CacheType"> | ||
<label>Truelayer</label> | ||
<description>Cache used by TrueLayer Plugin</description> | ||
</type> | ||
</config> |