Passed
Push — master ( 70f9f1...079f74 )
by Peter
02:21
created

CacheManager::flush()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Assets;
6
7
use League\Flysystem\FilesystemInterface;
8
9
class CacheManager implements ICacheManager
10
{
11
    const ERROR_FILESYSTEM_NOT_FOUND = 'filesystem not found';
12
13
    /** @var FilesystemInterface[] */
14
    protected $filesystems = [];
15
16
    /** @var callable[] */
17
    protected $checkers = [];
18
19
    /**
20
     * @param string $path
21
     *
22
     * @return FilesystemInterface
23
     */
24
    protected function getFilesystem(string $path): FilesystemInterface
25
    {
26
        foreach ($this->filesystems as $priority => $filesystem) {
27
            if (empty($this->checkers[$priority])) {
28
                return $filesystem;
29
            }
30
31
            if (call_user_func($this->checkers[$priority], $path)) {
32
                return $filesystem;
33
            }
34
        }
35
36
        throw new \InvalidArgumentException(static::ERROR_FILESYSTEM_NOT_FOUND);
37
    }
38
39
    /**
40
     * @param FilesystemInterface $filesystem
41
     * @param callable|null       $checker
42
     * @param int|null            $priority
43
     */
44
    public function registerFilesystem(FilesystemInterface $filesystem, callable $checker = null, ?int $priority = null)
45
    {
46
        $priority = $priority === null ? count($this->filesystems) * -1 : $priority;
47
48
        $this->filesystems[$priority] = $filesystem;
49
        $this->checkers[$priority]    = $checker;
50
51
        krsort($this->filesystems);
52
        krsort($this->checkers);
53
    }
54
55
    /**
56
     * @param string $path
57
     * @param string $groupName
58
     *
59
     * @return bool
60
     */
61
    public function has(string $path): bool
62
    {
63
        $fs = $this->getFilesystem($path);
64
65
        return $fs->has($path);
66
    }
67
68
    /**
69
     * @param string $path
70
     *
71
     * @return string|null
72
     * @throws \League\Flysystem\FileNotFoundException
73
     */
74
    public function read(string $path): ?string
75
    {
76
        $fs = $this->getFilesystem($path);
77
78
        if (!$fs->has($path)) {
79
            return null;
80
        }
81
82
        return $fs->read($path);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fs->read($path) could return the type false which is incompatible with the type-hinted return null|string. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
85
    /**
86
     * @param string $path
87
     * @param string $content
88
     *
89
     * @return bool
90
     * @throws \League\Flysystem\FileExistsException
91
     */
92
    public function write(string $path, string $content): bool
93
    {
94
        $fs = $this->getFilesystem($path);
95
96
        $result = $fs->write($path, $content);
97
        if (!$result) {
98
            throw new \RuntimeException();
99
        }
100
101
        return true;
102
    }
103
104
    /**
105
     * @param string $path
106
     *
107
     * @return string
108
     * @throws \League\Flysystem\FileNotFoundException
109
     */
110
    public function getWebPath(string $path): string
111
    {
112
        $fs = $this->getFilesystem($path);
113
114
        $timestamp = (string)$fs->getTimestamp($path);
115
116
        return sprintf('%s?%s', $path, substr(md5($timestamp), 0, 5));
117
    }
118
119
    /**
120
     * @throws \League\Flysystem\FileNotFoundException
121
     */
122
    public function flush()
123
    {
124
        foreach ($this->filesystems as $filesystem) {
125
            $objects = $filesystem->listContents('/', false);
126
127
            foreach ($objects as $object) {
128
                $filesystem->delete($object['path']);
129
            }
130
        }
131
    }
132
}
133