ClearSimpleCache   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 39
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 33 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