|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Skaut\Skautis\Wsdl\Decorator\Cache; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
7
|
|
|
use Skaut\Skautis\User; |
|
8
|
|
|
use Skaut\Skautis\Wsdl\Decorator\AbstractDecorator; |
|
9
|
|
|
use Skaut\Skautis\Wsdl\WebServiceInterface; |
|
10
|
|
|
|
|
11
|
|
|
class CacheDecorator extends AbstractDecorator |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var CacheInterface |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $cache; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array<int, string> |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $checkedLoginIds = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
private $ttl; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param WebServiceInterface $webService |
|
30
|
|
|
* @param CacheInterface $cache |
|
31
|
|
|
* @param int $ttlSeconds |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function __construct( |
|
34
|
|
|
WebServiceInterface $webService, |
|
35
|
|
|
CacheInterface $cache, |
|
36
|
|
|
int $ttlSeconds |
|
37
|
|
|
) { |
|
38
|
2 |
|
$this->webService = $webService; |
|
39
|
2 |
|
$this->cache = $cache; |
|
40
|
2 |
|
$this->ttl = $ttlSeconds; |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function call(string $functionName, array $arguments = []) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$callHash = $this->hashCall($functionName, $arguments); |
|
49
|
|
|
|
|
50
|
|
|
// Pozaduj alespon 1 upesny request na server (zadna Exception) - Kontrola prihlaseni |
|
51
|
2 |
|
if (isset($arguments[User::ID_LOGIN]) && !in_array($arguments[User::ID_LOGIN], static::$checkedLoginIds, true)) { |
|
52
|
1 |
|
$response = $this->webService->call($functionName, $arguments); |
|
53
|
1 |
|
$this->cache->set($callHash, $response, $this->ttl); |
|
54
|
1 |
|
static::$checkedLoginIds[] = $arguments[User::ID_LOGIN]; |
|
55
|
|
|
|
|
56
|
1 |
|
return $response; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
$cachedResponse = $this->cache->get($callHash, null); |
|
60
|
2 |
|
if ($cachedResponse !== null) { |
|
61
|
2 |
|
return $cachedResponse; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$response = $this->webService->call($functionName, $arguments); |
|
65
|
1 |
|
$this->cache->set($callHash, $response, $this->ttl); |
|
66
|
|
|
|
|
67
|
1 |
|
return $response; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array<string, mixed> $arguments |
|
72
|
|
|
*/ |
|
73
|
2 |
|
protected function hashCall(string $functionName, array $arguments): string |
|
74
|
|
|
{ |
|
75
|
2 |
|
return $functionName . '?' . http_build_query($arguments); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|