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.
Completed
Push — master ( 7bf065...13ae28 )
by Axel
02:07
created

TextCacheItemPool   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 22
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 128
ccs 52
cts 54
cp 0.963
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A save() 0 7 1
A saveDeferred() 0 5 1
A hasItem() 0 3 1
A configure() 0 7 2
A deleteItem() 0 9 3
A deleteItems() 0 8 3
A commit() 0 7 2
A getItem() 0 11 2
A getItems() 0 9 3
A clear() 0 9 3
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
            : __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
        }
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 1
                return false;
54
            }
55
        }
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
        }
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
        ;
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
            }
121
        }
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
        }
134 10
        $this->deferredItems = [];
135 10
        return true;
136
    }
137
}
138