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.

TextCacheItemPool::configure()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.0116
1
<?php
2
3
namespace PhpAbac\Cache\Pool;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use Psr\Cache\CacheItemInterface;
7
8
use PhpAbac\Cache\Item\TextCacheItem;
9
10
class TextCacheItemPool implements CacheItemPoolInterface {
11
    /** @var array **/
12
    protected $deferredItems;
13
    /** @var string **/
14
    protected $cacheFolder;
15
16
    /**
17
     * @param array $options
18
     */
19 10
    public function __construct($options = []) {
20 10
        $this->configure($options);
21 10
    }
22
23
    /**
24
     * @param array $options
25
     */
26 10
    protected function configure($options) {
27 10
        $this->cacheFolder =
28 10
            (isset($options['cache_folder']))
29 10
            ? "{$options['cache_folder']}/text"
30 10
            : __DIR__ . '/../../../data/cache/text'
31
        ;
32 10
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function deleteItem($key) {
38 2
        if(is_file("{$this->cacheFolder}/$key.txt")) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
39 2
            unlink("{$this->cacheFolder}/$key.txt");
40 2
        }
41 2
        if(isset($this->deferredItems[$key])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
42
            unset($this->deferredItems[$key]);
43
        }
44 2
        return true;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function deleteItems(array $keys) {
51 1
        foreach($keys as $key) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
52 1
            if(!$this->deleteItem($key)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
53
                return false;
54
            }
55 1
        }
56 1
        return true;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 9
    public function save(CacheItemInterface $item) {
63 9
        $data = "{$item->get()};{$item->getExpirationDate()->format('Y-m-d H:i:s')}";
64
65 9
        file_put_contents("{$this->cacheFolder}/{$item->getKey()}.txt", $data);
66
67 9
        return true;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function saveDeferred(CacheItemInterface $item) {
74 2
        $this->deferredItems[$item->getKey()] = $item;
75
76 2
        return true;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 2
    public function commit() {
83 2
        foreach($this->deferredItems as $key => $item) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
84 2
            $this->save($item);
85 2
            unset($this->deferredItems[$key]);
86 2
        }
87 2
        return true;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 10
    public function hasItem($key) {
94 10
        return is_file("{$this->cacheFolder}/{$key}.txt");
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 6
    public function getItem($key) {
101 6
        $item = new TextCacheItem($key);
102 6
        if(!$this->hasItem($key)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
103 1
            return $item;
104
        }
105 5
        $data = explode(';',file_get_contents("{$this->cacheFolder}/{$key}.txt"));
106
        return $item
107 5
            ->set($data[0])
108 5
            ->expiresAt((new \DateTime($data[1])))
109 5
        ;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 2
    public function getItems(array $keys = array()) {
116 2
        $items = [];
117 2
        foreach($keys as $key) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
118 2
            if($this->hasItem($key)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
119 2
                $items[$key] = $this->getItem($key);
120 2
            }
121 2
        }
122 2
        return $items;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 10
    public function clear() {
129 10
        $items = glob("{$this->cacheFolder}/*.txt"); // get all file names
130 10
        foreach($items as $item){ // iterate files
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
131 8
          if(is_file($item))
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
132 8
            unlink($item); // delete file
133 10
        }
134 10
        $this->deferredItems = [];
135 10
        return true;
136
    }
137
}
138