ToolFactory::importCommand()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 6
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Json\Factory;
4
5
use Zalas\Toolbox\Tool\Collection;
6
use Zalas\Toolbox\Tool\Command;
7
use Zalas\Toolbox\Tool\Command\MultiStepCommand;
8
use Zalas\Toolbox\Tool\Command\TestCommand;
9
use Zalas\Toolbox\Tool\Tool;
10
11
final class ToolFactory
12
{
13 21
    public static function import(array $tool): Tool
14
    {
15 21
        Assert::requireFields(['name', 'summary', 'website', 'command', 'test'], $tool, 'tool');
16
17 16
        return new Tool(
18 16
            $tool['name'],
19 16
            $tool['summary'],
20 16
            $tool['website'],
21 16
            $tool['tags'] ?? [],
22 16
            self::importCommand($tool),
23 16
            new TestCommand($tool['test'], $tool['name'])
24 16
        );
25
    }
26
27 16
    private static function importCommand(array $tool): Command
28
    {
29 16
        $commands = Collection::create([]);
30
31 16
        foreach ($tool['command'] as $type => $command) {
32 15
            $commands = $commands->merge(self::createCommands($type, $command));
33
        }
34
35 15
        if (0 === $commands->count()) {
36 1
            throw new \RuntimeException(\sprintf('No valid command defined for the tool: %s', \json_encode($tool)));
37
        }
38
39 14
        return 1 === $commands->count() ? $commands->toArray()[0] : new MultiStepCommand($commands);
40
    }
41
42 15
    private static function createCommands($type, $command): Collection
43
    {
44 15
        $factories = [
45 15
            'phar-download' => \sprintf('%s::import', PharDownloadCommandFactory::class),
46 15
            'file-download' => \sprintf('%s::import', FileDownloadCommandFactory::class),
47 15
            'box-build' => \sprintf('%s::import', BoxBuildCommandFactory::class),
48 15
            'composer-install' => \sprintf('%s::import', ComposerInstallCommandFactory::class),
49 15
            'phive-install' => \sprintf('%s::import', PhiveInstallCommandFactory::class),
50 15
            'composer-global-install' => \sprintf('%s::import', ComposerGlobalInstallCommandFactory::class),
51 15
            'composer-bin-plugin' => \sprintf('%s::import', ComposerBinPluginCommandFactory::class),
52 15
            'sh' => \sprintf('%s::import', ShCommandFactory::class),
53 15
        ];
54
55 15
        if (!isset($factories[$type])) {
56 1
            throw new \RuntimeException(\sprintf('Unrecognised command: "%s". Supported commands are: "%s".', $type, \implode(', ', \array_keys($factories))));
57
        }
58
59 14
        $command = !\is_numeric(\key($command)) ? [$command] : $command;
60
61 14
        return Collection::create(\array_map(function ($c) use ($type, $factories) {
62 14
            return $factories[$type]($c);
63 14
        }, $command));
64
    }
65
}
66