ClearSimpleCache::__invoke()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
nop 3
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/craft-simplecache/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/craft-simplecache
7
 */
8
9
namespace Flipbox\Relay\Middleware;
10
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
class ClearSimpleCache extends AbstractSimpleCache
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public function __invoke(
24
        RequestInterface $request,
25
        ResponseInterface $response,
26
        callable $next = null
27
    ): ResponseInterface {
28
        parent::__invoke($request, $response);
29
30
        $response = $next($request, $response);
31
32
        // Only cache successful responses
33
        if ($this->isResponseSuccessful($response)) {
34
            $key = $this->getCacheKey($request);
35
36
            if ($this->cache->delete($key)) {
37
                $this->info(
38
                    "Item removed from cache successfully. [key: {key}, type: {type}]",
39
                    [
40
                        'type' => get_class($this->cache),
41
                        'key' => $key
42
                    ]
43
                );
44
            } else {
45
                $this->info(
46
                    "Item not removed from cache. [key: {key}, type: {type}]",
47
                    [
48
                        'type' => get_class($this->cache),
49
                        'key' => $key
50
                    ]
51
                );
52
            }
53
        }
54
        return $response;
55
    }
56
}
57