Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/DataProvider/CachedDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);


namespace src\DataProvider;

use Psr\Cache\CacheItemPoolInterface;

class CachedDataProvider implements DataProviderInterface
{
/**
* @var DataProviderInterface
*/
private $dataProvider;

/**
* @var CacheItemPoolInterface
*/
private $cachePool;
/**
* @var string
*/
private $cacheExpiryModifier;

/**
* CachedDataProvider constructor.
*
* @param DataProviderInterface $dataProvider
* @param CacheItemPoolInterface $cachePool
* @param string $cacheExpiryModifier
*/
public function __construct(DataProviderInterface $dataProvider, CacheItemPoolInterface $cachePool, string $cacheExpiryModifier = '+1 day')
{
$this->dataProvider = $dataProvider;
$this->cachePool = $cachePool;
$this->cacheExpiryModifier = $cacheExpiryModifier;
}

/**
* {@inheritdoc}
*/
public function get(array $request): array
{
$cacheKey = $this->getCacheKey($request);
$cacheItem = $this->cache->getItem($cacheKey);
if ($cacheItem->isHit()) {
return $cacheItem->get();
}

$result = $this->dataProvider->get($request);

$cacheItem
->set($result)
->expiresAt(
(new \DateTime())->modify($this->cacheExpiryModifier)
);

return $result;
}

/**
* @param array $request
*
* @return string
*/
private function getCacheKey(array $request): string
{
$request = $this->sortArrayByKeys($request);

return \json_encode($request);
}

/**
* @param array $arr
*
* @return array
*/
private function sortArrayByKeys(array $arr): array
{
\ksort($arr);
foreach ($arr as $key => $item) {
if (\is_array($item)) {
$arr[$key] = $this->sortArrayByKeys($item);
}
}

return $arr;
}
}
16 changes: 16 additions & 0 deletions src/DataProvider/DataProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);


namespace src\DataProvider;


interface DataProviderInterface
{
/**
* @param array $request
*
* @return array
*/
public function get(array $request): array;
}
34 changes: 34 additions & 0 deletions src/DataProvider/RemoteServerDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);


namespace src\DataProvider;


use src\RemoteServer\DoRequestInterface;

class RemoteServerDataProvider implements DataProviderInterface
{
/**
* @var DoRequestInterface
*/
private $server;

/**
* RemoteServerDataProvider constructor.
*
* @param DoRequestInterface $server
*/
public function __construct(DoRequestInterface $server)
{
$this->server = $server;
}

/**
* {@inheritdoc}
*/
public function get(array $request): array
{
return $this->server->doRequest($request);
}
}
16 changes: 16 additions & 0 deletions src/RemoteServer/DoRequestInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);


namespace src\RemoteServer;


interface DoRequestInterface
{
/**
* @param array $request
*
* @return array
*/
public function doRequest(array $request): array;
}
65 changes: 65 additions & 0 deletions src/RemoteServer/RemoteServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);


namespace src\RemoteServer;

use Psr\Log\LoggerInterface;

class RemoteServer implements DoRequestInterface
{
/**
* @var string
*/
private $host;

/**
* @var string
*/
private $user;

/**
* @var string
*/
private $pwd;

/**
* @var null|LoggerInterface
*/
private $logger;

/**
* RemoteServer constructor.
*
* @param string $host
* @param string $user
* @param string $pwd
* @param LoggerInterface|null $logger
*/
public function __construct(string $host, string $user, string $pwd, LoggerInterface $logger = null)
{
$this->host = $host;
$this->user = $user;
$this->pwd = $pwd;
$this->logger = $logger;
}

/**
* {@inheritdoc}
*/
public function doRequest(array $request): array
{
try {
// TODO: Implement doRequest() method.
} catch (\Throwable $e) {
if (null !== $this->logger) {
$msg = \sprintf('Error occurs while request to remote server with message "%s"', $e->getMessage());
$this->logger->error($msg, [
'remoteServerHost' => $this->host,
'remoteServerUser' => $this->user,
]);
}
throw $e;
}
}
}