-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeReview.php
More file actions
103 lines (84 loc) · 2.24 KB
/
codeReview.php
File metadata and controls
103 lines (84 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace src\Integration;
class DataProvider
{
private $host;
private $user;
private $password;
/**
* @param $host
* @param $user
* @param $password
*/
public function __construct($host, $user, $password)
{
$this->host = $host;
$this->user = $user;
$this->password = $password;
}
/**
* @param array $request
*
* @return array
*/
public function get(array $request)
{
// returns a response from external service
}
}
namespace src\Decorator;
use DateTime;
use Exception;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use src\Integration\DataProvider;
class DecoratorManager extends DataProvider
{
public $cache;
public $logger;
/**
* @param string $host
* @param string $user
* @param string $password
* @param CacheItemPoolInterface $cache
*/
public function __construct($host, $user, $password, CacheItemPoolInterface $cache)
{
parent::__construct($host, $user, $password);
$this->cache = $cache;
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
public function getResponse(array $input)
{
try {
//$cacheKey = $this->getCacheKey($input); // Выполняется что-то не то
//$cacheItem = $this->cache->getItem($cacheKey); // результат фукнции getCacheKey не будет равен ключу
$cacheItem = $this->cache->get($input); // Предлагаю сделать вот так
if ($cacheItem->isHit()) {
return $cacheItem->get();
}
$result = parent::get($input);
$cacheItem
->set($result)
->expiresAt(
(new DateTime())->modify('+1 day')
);
return $result;
} catch (Exception $e) {
$this->logger->critical('Error');
}
return [];
}
// Функцию можно выключить вообще
/*public function getCacheKey(array $input)
{
return json_encode($input);
}*/
}
?>