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 ( 05f22e...bf0dcf )
by Cees-Jan
03:15
created

Filesystem::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
28
    /**
29
     * filesystem constructor.
30
     * @param ReactFilesystem $filesystem
31
     * @param string          $path
32
     */
33 11
    public function __construct(ReactFilesystem $filesystem, string $path)
34
    {
35 11
        $this->filesystem = $filesystem;
36 11
        $this->path = $path;
37 11
    }
38
39
    /**
40
     * @param  string           $key
41
     * @param  null|mixed       $default
42
     * @return PromiseInterface
43
     */
44 5
    public function get($key, $default = null): PromiseInterface
45
    {
46
        return $this->has($key)->then(function (bool $has) use ($key, $default) {
47 5
            if ($has === true) {
48 4
                return $this->getFile($key)->getContents();
49
            }
50
51 1
            return resolve($default);
52 5
        });
53
    }
54
55
    /**
56
     * @param string     $key
57
     * @param mixed      $value
58
     * @param null|mixed $ttl
59
     */
60 5
    public function set($key, $value, $ttl = null): PromiseInterface
61
    {
62 5
        $file = $this->getFile($key);
63 5
        if (\strpos($key, \DIRECTORY_SEPARATOR) === false) {
64 1
            return $this->putContents($file, $value);
65
        }
66
67 4
        $path = \explode(\DIRECTORY_SEPARATOR, $key);
68 4
        \array_pop($path);
69 4
        $path = \implode(\DIRECTORY_SEPARATOR, $path);
70
71 4
        $dir = $this->filesystem->dir($this->path . $path);
72
73
        return $dir->createRecursive()->then(null, function (Throwable $error) {
74 2
            if ($error->getMessage() === 'mkdir(): File exists') {
75 2
                return resolve(true);
76
            }
77
78
            return reject($error);
79
        })->then(function () use ($file, $value): PromiseInterface {
80 4
            return $this->putContents($file, $value);
81 4
        });
82
    }
83
84
    /**
85
     * @param string $key
86
     */
87 4
    public function delete($key): PromiseInterface
88
    {
89
        return $this->has($key)->then(function () use ($key): PromiseInterface {
90 4
            return $this->getFile($key)->remove();
91
        })->then(function () {
92 1
            return resolve(true);
93
        }, function () {
94 3
            return resolve(false);
95 4
        });
96
    }
97
98 2
    public function getMultiple(array $keys, $default = null): PromiseInterface
99
    {
100 2
        $promises = [];
101 2
        foreach ($keys as $key) {
102 2
            $promises[$key] = $this->get($key, $default);
103
        }
104
105 2
        return all($promises);
106
    }
107
108 2
    public function setMultiple(array $values, $ttl = null): PromiseInterface
109
    {
110 2
        $promises = [];
111 2
        foreach ($values as $key => $value) {
112 2
            $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 2
            foreach ($results as $result) {
117 2
                if ($result === false) {
118
                    return resolve(false);
119
                }
120
            }
121
122 2
            return resolve(true);
123 2
        });
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 1
    public function clear(): PromiseInterface
145
    {
146
        return (new Promise(function ($resolve, $reject): void {
147 1
            $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 1
                if ($node instanceof FileInterface === false) {
150 1
                    return;
151
                }
152
153 1
                $node->remove()->then(null, $reject);
154 1
            });
155 1
            $stream->on('error', $reject);
156 1
            $stream->on('close', $resolve);
157
        }))->then(function () {
158 1
            return resolve(true);
159 1
        });
160
    }
161
162 9
    public function has($key): PromiseInterface
163
    {
164
        return $this->getFile($key)->exists()->then(function () {
165 5
            return resolve(true);
166
        }, function () {
167 7
            return resolve(false);
168 9
        });
169
    }
170
171 5
    private function putContents(FileInterface $file, $value): PromiseInterface
172
    {
173
        return $file->putContents($value)->then(function () {
174 5
            return resolve(true);
175
        }, function () {
176
            return resolve(false);
177 5
        });
178
    }
179
180
    /**
181
     * @param $key
182
     * @return FileInterface
183
     */
184 11
    private function getFile($key): FileInterface
185
    {
186 11
        return $this->filesystem->file($this->path . $key);
187
    }
188
}
189