Completed
Pull Request — master (#41)
by Sven
01:57
created

ScriptFinder::findScriptsByName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
4
namespace Shopware\Psh\Listing;
5
6
use Shopware\Psh\Config\ScriptPath;
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 ScriptPath[]
20
     */
21
    private $scriptPaths;
22
23
    /**
24
     * @var DescriptionReader
25
     */
26
    private $scriptDescriptionReader;
27
28
    /**
29
     * @param ScriptPath[] $scriptPaths
30
     * @param DescriptionReader $scriptDescriptionReader
31
     */
32
    public function __construct(array $scriptPaths, DescriptionReader $scriptDescriptionReader)
33
    {
34
        $this->scriptPaths = $scriptPaths;
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->scriptPaths 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()) as $fileName) {
52
                if (strpos($fileName, '.') === 0) {
53
                    continue;
54
                }
55
56
                $extension = pathinfo($fileName, PATHINFO_EXTENSION);
57
58
                if (!in_array($extension, self::VALID_EXTENSIONS)) {
59
                    continue;
60
                }
61
62
                $description = $this->scriptDescriptionReader->read($path->getPath() . '/' . $fileName);
63
64
                $newScript = new Script($path->getPath(), $fileName, $path->getNamespace(), $description);
65
66
                $scripts[$newScript->getName()] = $newScript;
67
            }
68
        }
69
70
        return $scripts;
71
    }
72
73
    /**
74
     * @param string $query
75
     * @return array
76
     */
77
    public function findScriptsByName(string $query): array
78
    {
79
        $scripts = $this->getAllScripts();
80
81
        return array_filter($scripts, function ($key) use ($query) {
82
            return strpos($key, $query) > -1;
83
        }, ARRAY_FILTER_USE_KEY);
84
    }
85
86
    /**
87
     * @param string $scriptName
88
     * @return Script
89
     * @throws ScriptNotFoundException
90
     */
91
    public function findScriptByName(string $scriptName): Script
92
    {
93
        foreach ($this->getAllScripts() as $script) {
94
            if ($script->getName() === $scriptName) {
95
                return $script;
96
            }
97
        }
98
99
        throw new ScriptNotFoundException('Unable to find script named "' . $scriptName . '"');
100
    }
101
}
102