Completed
Pull Request — master (#93)
by Jan Philipp
01:41
created

ScriptFinder::getAllScripts()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.1928
c 0
b 0
f 0
cc 5
nc 5
nop 0
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
            if (!is_dir($path->getPath())) {
48
                throw new ScriptPathNotValidException("The given script path: '{$path->getPath()}' is not a valid directory");
49
            }
50
51
            foreach (scandir($path->getPath(), SCANDIR_SORT_ASCENDING) as $fileName) {
52
                $extension = pathinfo($fileName, PATHINFO_EXTENSION);
53
54
                if (!in_array($extension, self::VALID_EXTENSIONS, true)) {
55
                    continue;
56
                }
57
58
                $description = $this->scriptDescriptionReader->read($path->getPath() . '/' . $fileName);
59
60
                $newScript = new Script($path->getPath(), $fileName, $path->isHidden(), $path->getNamespace(), $description);
61
62
                $scripts[$newScript->getName()] = $newScript;
63
            }
64
        }
65
66
        return $scripts;
67
    }
68
69
    public function getAllVisibleScripts(): array
70
    {
71
        return array_filter($this->getAllScripts(), function (Script $script): bool {
72
            return !$script->isHidden();
73
        });
74
    }
75
76
    /**
77
     * @param string $query
78
     * @return array
79
     */
80
    public function findScriptsByPartialName(string $query): array
81
    {
82
        $scripts = $this->getAllVisibleScripts();
83
84
        return array_filter($scripts, function ($key) use ($query) {
85
            return strpos($key, $query) > -1 || levenshtein($key, $query) < 3;
86
        }, ARRAY_FILTER_USE_KEY);
87
    }
88
89
    /**
90
     * @param string $scriptName
91
     * @return Script
92
     * @throws ScriptNotFoundException
93
     */
94
    public function findScriptByName(string $scriptName): Script
95
    {
96
        foreach ($this->getAllScripts() as $script) {
97
            if ($script->getName() === $scriptName) {
98
                return $script;
99
            }
100
        }
101
102
        throw (new ScriptNotFoundException('Unable to find script named "' . $scriptName . '"'))->setScriptName($scriptName);
103
    }
104
}
105