Completed
Pull Request — master (#87)
by Jan Philipp
03:01
created

ScriptFinder::isValidExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\Listing;
5
6
use Shopware\Psh\Config\ScriptsPath;
7
8
/**
9
 * Load all scripts from all the supplied paths and create an array of scripts
10
 */
11
class ScriptFinder
12
{
13
    const VALID_EXTENSIONS = [
14
        'sh',
15
        'psh'
16
    ];
17
18
    /**
19
     * @var ScriptsPath[]
20
     */
21
    private $scriptsPaths;
22
23
    /**
24
     * @var DescriptionReader
25
     */
26
    private $scriptDescriptionReader;
27
28
    /**
29
     * @param ScriptsPath[] $scriptsPaths
30
     * @param DescriptionReader $scriptDescriptionReader
31
     */
32
    public function __construct(array $scriptsPaths, DescriptionReader $scriptDescriptionReader)
33
    {
34
        $this->scriptsPaths = $scriptsPaths;
35
        $this->scriptDescriptionReader = $scriptDescriptionReader;
36
    }
37
38
    /**
39
     * @return Script[]
40
     * @throws ScriptPathNotValidException
41
     */
42
    public function getAllScripts(): array
43
    {
44
        $scripts = [];
45
46
        foreach ($this->scriptsPaths as $path) {
47
            $this->testPathValidity($path);
48
49
            foreach (scandir($path->getPath(), SCANDIR_SORT_ASCENDING) as $fileName) {
50
                if (!$this->isValidScript($fileName)) {
51
                    continue;
52
                }
53
54
                $description = $this->scriptDescriptionReader->read($path->getPath() . '/' . $fileName);
55
                $newScript = new Script($path->getPath(), $fileName, $path->getNamespace(), $description);
56
57
                $scripts[$newScript->getName()] = $newScript;
58
            }
59
        }
60
61
        return $scripts;
62
    }
63
64
    /**
65
     * @param string $query
66
     * @return array
67
     */
68
    public function findScriptsByPartialName(string $query): array
69
    {
70
        $scripts = $this->getAllScripts();
71
72
        return array_filter($scripts, function ($key) use ($query) {
73
            return strpos($key, $query) > -1 || levenshtein($key, $query) < 3;
74
        }, ARRAY_FILTER_USE_KEY);
75
    }
76
77
    /**
78
     * @param string $scriptName
79
     * @return Script
80
     * @throws ScriptNotFoundException
81
     */
82
    public function findScriptByName(string $scriptName): Script
83
    {
84
        foreach ($this->getAllScripts() as $script) {
85
            if ($script->getName() === $scriptName) {
86
                return $script;
87
            }
88
        }
89
90
        throw (new ScriptNotFoundException('Unable to find script named "' . $scriptName . '"'))->setScriptName($scriptName);
91
    }
92
93
    /**
94
     * @param $fileName
95
     * @return bool
96
     */
97
    private function isValidScript(string $fileName): bool
98
    {
99
        return $this->isVisibleFile($fileName) && $this->isValidExtension($fileName);
100
    }
101
102
    /**
103
     * @param $fileName
104
     * @return bool
105
     */
106
    private function isVisibleFile(string $fileName): bool
107
    {
108
        return strpos($fileName, '.') !== 0;
109
    }
110
111
    /**
112
     * @param string $fileName
113
     * @return bool
114
     */
115
    private function isValidExtension(string $fileName): bool
116
    {
117
        $extension = pathinfo($fileName, PATHINFO_EXTENSION);
118
119
        return in_array($extension, self::VALID_EXTENSIONS, true);
120
    }
121
122
    /**
123
     * @param ScriptsPath $path
124
     */
125
    private function testPathValidity(ScriptsPath $path)
126
    {
127
        if (!$path->isValid()) {
128
            throw new ScriptPathNotValidException("The given script path: '{$path->getPath()}' is not a valid directory");
129
        }
130
    }
131
}
132