Completed
Push — master ( 482c5f...79d98c )
by Jan Philipp
13s queued 11s
created

ScriptFinder::isValidScript()   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->isHidden(), $path->getNamespace(), $description);
56
57
                $scripts[$newScript->getName()] = $newScript;
58
            }
59
        }
60
61
        return $scripts;
62
    }
63
64
    public function getAllVisibleScripts(): array
65
    {
66
        return array_filter($this->getAllScripts(), function (Script $script): bool {
67
            return !$script->isHidden();
68
        });
69
    }
70
71
    /**
72
     * @param string $query
73
     * @return array
74
     */
75
    public function findScriptsByPartialName(string $query): array
76
    {
77
        $scripts = $this->getAllVisibleScripts();
78
79
        return array_filter($scripts, function ($key) use ($query) {
80
            return strpos($key, $query) > -1 || levenshtein($key, $query) < 3;
81
        }, ARRAY_FILTER_USE_KEY);
82
    }
83
84
    /**
85
     * @param string $scriptName
86
     * @return Script
87
     * @throws ScriptNotFoundException
88
     */
89
    public function findScriptByName(string $scriptName): Script
90
    {
91
        foreach ($this->getAllScripts() as $script) {
92
            if ($script->getName() === $scriptName) {
93
                return $script;
94
            }
95
        }
96
97
        throw (new ScriptNotFoundException('Unable to find script named "' . $scriptName . '"'))->setScriptName($scriptName);
98
    }
99
100
    /**
101
     * @param $fileName
102
     * @return bool
103
     */
104
    private function isValidScript(string $fileName): bool
105
    {
106
        $extension = pathinfo($fileName, PATHINFO_EXTENSION);
107
108
        return in_array($extension, self::VALID_EXTENSIONS, true);
109
    }
110
111
    /**
112
     * @param ScriptsPath $path
113
     */
114
    private function testPathValidity(ScriptsPath $path)
115
    {
116
        if (!$path->isValid()) {
117
            throw new ScriptPathNotValidException("The given script path: '{$path->getPath()}' is not a valid directory");
118
        }
119
    }
120
}
121