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 ( c17903...05f22e )
by Cees-Jan
08:42 queued 10s
created

Filesystem::clear()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 0
cp 0
rs 9.7
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 6
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Cache;
4
5
use React\Cache\CacheInterface;
6
use React\Filesystem\FilesystemInterface as ReactFilesystem;
7
use React\Filesystem\Node\FileInterface;
8
use React\Filesystem\Node\NodeInterface;
9
use function React\Promise\all;
10
use React\Promise\Promise;
11
use React\Promise\PromiseInterface;
12
use function React\Promise\reject;
13
use function React\Promise\resolve;
14
use Throwable;
15
16
final class Filesystem implements CacheInterface
17
{
18
    /**
19
     * @var ReactFilesystem
20
     */
21
    private $filesystem;
22
23
    /**
24
     * @var string
25
     */
26
    private $path;
27 6
28
    /**
29 6
     * filesystem constructor.
30 6
     * @param ReactFilesystem $filesystem
31 6
     * @param string          $path
32
     */
33
    public function __construct(ReactFilesystem $filesystem, string $path)
34
    {
35
        $this->filesystem = $filesystem;
36
        $this->path = $path;
37 2
    }
38
39 2
    /**
40
     * @param  string           $key
41
     * @param  null|mixed       $default
42 1
     * @return PromiseInterface
43 2
     */
44
    public function get($key, $default = null): PromiseInterface
45
    {
46
        return $this->has($key)->then(function (bool $has) use ($key, $default) {
47
            if ($has === true) {
48
                return $this->getFile($key)->getContents();
49
            }
50 2
51
            return resolve($default);
52 2
        });
53 2
    }
54 1
55
    /**
56 1
     * @param string     $key
57
     * @param mixed      $value
58
     * @param null|mixed $ttl
59 1
     */
60 1
    public function set($key, $value, $ttl = null): PromiseInterface
61 1
    {
62
        $file = $this->getFile($key);
63
        if (\strpos($key, \DIRECTORY_SEPARATOR) === false) {
64 1
            return $this->putContents($file, $value);
65 1
        }
66 1
67
        $path = \explode(\DIRECTORY_SEPARATOR, $key);
68
        \array_pop($path);
69
        $path = \implode(\DIRECTORY_SEPARATOR, $path);
70
71 2
        $dir = $this->filesystem->dir($this->path . $path);
72
73 2
        return $dir->createRecursive()->then(null, function (Throwable $error) {
74
            if ($error->getMessage() === 'mkdir(): File exists') {
75 1
                return resolve(true);
76 2
            }
77 2
78
            return reject($error);
79
        })->then(function () use ($file, $value): PromiseInterface {
80
            return $this->putContents($file, $value);
81
        });
82
    }
83 6
84
    /**
85 6
     * @param string $key
86
     */
87
    public function delete($key): PromiseInterface
88
    {
89
        return $this->has($key)->then(function () use ($key): PromiseInterface {
90
            return $this->getFile($key)->remove();
91
        })->then(function () {
92
            return resolve(true);
93
        }, function () {
94
            return resolve(false);
95
        });
96
    }
97
98
    public function getMultiple(array $keys, $default = null): PromiseInterface
99
    {
100
        $promises = [];
101
        foreach ($keys as $key) {
102
            $promises[$key] = $this->get($key, $default);
103
        }
104
105
        return all($promises);
106
    }
107
108
    public function setMultiple(array $values, $ttl = null): PromiseInterface
109
    {
110
        $promises = [];
111
        foreach ($values as $key => $value) {
112
            $promises[$key] = $this->set($key, $value, $ttl);
113
        }
114
115 View Code Duplication
        return all($promises)->then(function ($results) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
            foreach ($results as $result) {
117
                if ($result === false) {
118
                    return resolve(false);
119
                }
120
            }
121
122
            return resolve(true);
123
        });
124
    }
125
126
    public function deleteMultiple(array $keys): PromiseInterface
127
    {
128
        $promises = [];
129
        foreach ($keys as $key) {
130
            $promises[$key] = $this->delete($key);
131
        }
132
133 View Code Duplication
        return all($promises)->then(function ($results) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
            foreach ($results as $result) {
135
                if ($result === false) {
136
                    return resolve(false);
137
                }
138
            }
139
140
            return resolve(true);
141
        });
142
    }
143
144
    public function clear(): PromiseInterface
145
    {
146
        return (new Promise(function ($resolve, $reject): void {
147
            $stream = $this->filesystem->dir($this->path)->lsRecursiveStreaming();
0 ignored issues
show
Bug introduced by
The method lsRecursiveStreaming() does not exist on React\Filesystem\Node\DirectoryInterface. Did you maybe mean lsRecursive()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
148
            $stream->on('data', function (NodeInterface $node) use ($reject): void {
149
                if ($node instanceof FileInterface === false) {
150
                    return;
151
                }
152
153
                $node->remove()->then(null, $reject);
154
            });
155
            $stream->on('error', $reject);
156
            $stream->on('close', $resolve);
157
        }))->then(function () {
158
            return resolve(true);
159
        });
160
    }
161
162
    public function has($key): PromiseInterface
163
    {
164
        return $this->getFile($key)->exists()->then(function () {
165
            return resolve(true);
166
        }, function () {
167
            return resolve(false);
168
        });
169
    }
170
171
    private function putContents(FileInterface $file, $value): PromiseInterface
172
    {
173
        return $file->putContents($value)->then(function () {
174
            return resolve(true);
175
        }, function () {
176
            return resolve(false);
177
        });
178
    }
179
180
    /**
181
     * @param $key
182
     * @return FileInterface
183
     */
184
    private function getFile($key): FileInterface
185
    {
186
        return $this->filesystem->file($this->path . $key);
187
    }
188
}
189