1 | <?php declare(strict_types=1); |
||
16 | final class CacheMiddleware implements MiddlewareInterface |
||
17 | { |
||
18 | const DEFAULT_GLUE = '/'; |
||
19 | |||
20 | /** |
||
21 | * @var CacheInterface[] |
||
22 | */ |
||
23 | private $cache; |
||
24 | |||
25 | /** |
||
26 | * @var string[] |
||
27 | */ |
||
28 | private $key; |
||
29 | |||
30 | /** |
||
31 | * @var RequestInterface[] |
||
32 | */ |
||
33 | private $request; |
||
34 | |||
35 | /** |
||
36 | * @var bool[] |
||
37 | */ |
||
38 | private $store = false; |
||
39 | |||
40 | /** |
||
41 | * @var StrategyInterface[] |
||
42 | */ |
||
43 | private $strategy; |
||
44 | |||
45 | /** |
||
46 | * @param RequestInterface $request |
||
47 | * @param array $options |
||
48 | * @return CancellablePromiseInterface |
||
49 | */ |
||
50 | 13 | public function pre( |
|
51 | RequestInterface $request, |
||
52 | string $transactionId, |
||
53 | array $options = [] |
||
54 | ): CancellablePromiseInterface { |
||
55 | 13 | if (!isset($options[self::class][Options::CACHE]) || !isset($options[self::class][Options::STRATEGY])) { |
|
56 | return resolve($request); |
||
57 | } |
||
58 | 13 | $this->cache[$transactionId] = $options[self::class][Options::CACHE]; |
|
59 | 13 | $this->strategy[$transactionId] = $options[self::class][Options::STRATEGY]; |
|
60 | 13 | if (!($this->cache[$transactionId] instanceof CacheInterface) || |
|
61 | 13 | !($this->strategy[$transactionId] instanceof StrategyInterface) |
|
62 | ) { |
||
63 | return resolve($request); |
||
64 | } |
||
65 | |||
66 | 13 | if ($request->getMethod() !== 'GET') { |
|
67 | 10 | $this->cleanUpTransaction($transactionId); |
|
68 | |||
69 | 10 | return resolve($request); |
|
70 | } |
||
71 | |||
72 | 3 | $this->request[$transactionId] = $request; |
|
73 | 3 | $this->key[$transactionId] = CacheKey::create( |
|
74 | 3 | $this->request[$transactionId]->getUri(), |
|
75 | 3 | $options[self::class][Options::GLUE] ?? self::DEFAULT_GLUE |
|
76 | ); |
||
77 | |||
78 | 3 | return $this->cache[$transactionId]->get( |
|
79 | 3 | $this->key[$transactionId] |
|
80 | )->then(function (string $json) use ($transactionId) { |
||
81 | 1 | $document = Document::createFromString($json); |
|
82 | |||
83 | 1 | if ($document->hasExpired()) { |
|
84 | $this->cache[$transactionId]->remove($this->key[$transactionId]); |
||
|
|||
85 | |||
86 | return resolve($this->request[$transactionId]); |
||
87 | } |
||
88 | |||
89 | 1 | return reject($document->getResponse()); |
|
90 | }, function () use ($transactionId) { |
||
91 | 2 | return resolve($this->request[$transactionId]); |
|
92 | 3 | }); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param ResponseInterface $response |
||
97 | * @param array $options |
||
98 | * @return CancellablePromiseInterface |
||
99 | */ |
||
100 | 11 | public function post( |
|
140 | |||
141 | public function error( |
||
150 | |||
151 | /** |
||
152 | * @param ResponseInterface $response |
||
153 | * @return PromiseInterface |
||
154 | */ |
||
155 | 1 | private function hasBody(ResponseInterface $response): PromiseInterface |
|
163 | |||
164 | 11 | private function cleanUpTransaction(string $transactionId): void |
|
186 | } |
||
187 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.