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

FileFinder::find()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 5
nop 2
dl 0
loc 16
rs 9.6111
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 FileFinder implements IFileFinder
10
{
11
    /** @var FilesystemInterface[][][] */
12
    protected $filesystems = [];
13
14
    /**
15
     * @param FilesystemInterface $filesystem
16
     * @param string              $key
17
     * @param int                 $priority
18
     */
19
    public function registerFilesystem(
20
        FilesystemInterface $filesystem,
21
        string $key = IFileFinder::DEFAULT_KEY,
22
        int $priority = -1
23
    ) {
24
        if (empty($this->filesystems[$key][$priority])) {
25
            $this->filesystems[$key][$priority] = [];
26
        }
27
28
        $this->filesystems[$key][$priority][] = $filesystem;
29
30
        krsort($this->filesystems[$key]);
31
    }
32
33
    /**
34
     * @param string $path
35
     * @param string $key
36
     *
37
     * @return string|null
38
     */
39
    public function has(string $path, string $groupName = IFileFinder::DEFAULT_KEY): bool
40
    {
41
        return $this->find($path, $groupName) !== null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->find($path, $groupName) !== null returns the type boolean which is incompatible with the documented return type null|string.
Loading history...
42
    }
43
44
    /**
45
     * @param string $path
46
     * @param string $key
47
     *
48
     * @return string|null
49
     * @throws \League\Flysystem\FileNotFoundException
50
     */
51
    public function read(string $path, string $groupName = IFileFinder::DEFAULT_KEY): ?string
52
    {
53
        $filesystem = $this->find($path, $groupName);
54
        if (!$filesystem) {
55
            return null;
56
        }
57
58
        try {
59
            return (string)$filesystem->read($path);
60
        } catch (\Exception $e) {
61
            return null;
62
        }
63
    }
64
65
    /**
66
     * @param string $path
67
     * @param string $key
68
     *
69
     * @return FilesystemInterface|null
70
     */
71
    protected function find(string $path, string $key): ?FilesystemInterface
72
    {
73
        $possibleKeys = $this->getPossibleKeys($path, $key);
74
        foreach ($possibleKeys as $rootKey) {
75
            foreach ($this->filesystems[$rootKey] as $filesystems) {
76
                foreach ($filesystems as $filesystem) {
77
                    if (!$filesystem->has($path)) {
78
                        continue;
79
                    }
80
81
                    return $filesystem;
82
                }
83
            }
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * @param string $path
91
     * @param string $key
92
     *
93
     * @return string[]
94
     */
95
    protected function getPossibleKeys(string $path, string $key): array
96
    {
97
        if ($key !== static::DEFAULT_KEY && isset($this->filesystems[$key])) {
98
            return [$key];
99
        }
100
101
        $fixedPath = ltrim($path, '/');
102
103
        $keys = [];
104
        foreach (array_keys($this->filesystems) as $rootKey) {
105
            if (substr($fixedPath, 0, strlen($rootKey)) === $rootKey) {
106
                $keys[] = $rootKey;
107
            }
108
        }
109
110
        if (isset($this->filesystems[static::DEFAULT_KEY])) {
111
            $keys[] = static::DEFAULT_KEY;
112
        }
113
114
        return $keys;
115
    }
116
}
117