GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CachingDecorator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A setInnerBus() 0 4 1
A execute() 0 17 3
A createCacheItem() 0 6 1
A getCacheKey() 0 8 3
A getCacheExpiry() 0 8 3
1
<?php
2
3
namespace Chief\Decorators;
4
5
use Chief\Busses\SynchronousCommandBus;
6
use Chief\CacheableCommand;
7
use Chief\Command;
8
use Chief\CommandBus;
9
use Chief\Decorator;
10
use Chief\HasCacheOptions;
11
use Psr\Cache\CacheItemInterface;
12
use Psr\Cache\CacheItemPoolInterface;
13
14
class CachingDecorator implements Decorator
15
{
16
    /**
17
     * @var CommandBus
18
     */
19
    private $innerBus;
20
    /**
21
     * @var CacheItemPoolInterface
22
     */
23
    private $cache;
24
    /**
25
     * @var int
26
     */
27
    private $expiresAfter;
28
29
    /**
30
     * CachingDecorator constructor.
31
     * @param CacheItemPoolInterface $cache
32
     * @param int $expiresAfter
33
     * @param CommandBus $innerCommandBus
34
     */
35
    public function __construct(CacheItemPoolInterface $cache, $expiresAfter = 3600, CommandBus $innerCommandBus = null)
36
    {
37
        $this->cache = $cache;
38
        $this->expiresAfter = $expiresAfter;
39
        $this->setInnerBus($innerCommandBus ?: new SynchronousCommandBus);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function setInnerBus(CommandBus $bus)
46
    {
47
        $this->innerBus = $bus;
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function execute(Command $command)
54
    {
55
        if (!$command instanceof CacheableCommand) {
56
            return $this->innerBus->execute($command);
57
        }
58
59
        $cached = $this->cache->getItem($this->getCacheKey($command));
60
        if ($cached->isHit()) {
61
            return $cached->get();
62
        }
63
64
        $value = $this->innerBus->execute($command);
65
66
        $this->cache->save($this->createCacheItem($command, $value));
67
68
        return $value;
69
    }
70
71
    /**
72
     * Create a new cache item to be persisted.
73
     *
74
     * @param CacheableCommand $command
75
     * @param mixed $value
76
     * @return CacheItemInterface
77
     */
78
    private function createCacheItem(CacheableCommand $command, $value)
79
    {
80
        return $this->cache->getItem($this->getCacheKey($command))
81
            ->expiresAfter($this->getCacheExpiry($command))
82
            ->set($value);
83
    }
84
85
    /**
86
     * Create the key to be used when saving this item to the cache pool.
87
     *
88
     * The cache item key is taken as a the (string) serialized command, to ensure the return value is unique
89
     * depending on the command properties; that serialized string is then md5'd to ensure it doesn't
90
     * overflow any string length limits the implementing CacheItemPoolInterface library has.
91
     *
92
     * @param CacheableCommand $command
93
     * @return string
94
     */
95
    private function getCacheKey(CacheableCommand $command)
96
    {
97
        if ($command instanceof HasCacheOptions && $command->getCacheKey()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $command->getCacheKey() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
98
            return $command->getCacheKey();
99
        }
100
101
        return md5(serialize($command));
102
    }
103
104
    /**
105
     * Determine when this CachableCommand should expire, in terms of seconds from now.
106
     *
107
     * @param CacheableCommand $command
108
     * @return int
109
     */
110
    private function getCacheExpiry(CacheableCommand $command)
111
    {
112
        if ($command instanceof HasCacheOptions && $command->getCacheExpiry() > 0) {
113
            return $command->getCacheExpiry();
114
        }
115
116
        return $this->expiresAfter;
117
    }
118
}
119