InstallTools::commandFilter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
c 0
b 0
f 0
cc 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\UseCase;
4
5
use Closure;
6
use Zalas\Toolbox\Tool\Collection;
7
use Zalas\Toolbox\Tool\Command;
8
use Zalas\Toolbox\Tool\Command\BoxBuildCommand;
9
use Zalas\Toolbox\Tool\Command\ComposerBinPluginCommand;
10
use Zalas\Toolbox\Tool\Command\ComposerGlobalInstallCommand;
11
use Zalas\Toolbox\Tool\Command\ComposerGlobalMultiInstallCommand;
12
use Zalas\Toolbox\Tool\Command\ComposerInstallCommand;
13
use Zalas\Toolbox\Tool\Command\FileDownloadCommand;
14
use Zalas\Toolbox\Tool\Command\MultiStepCommand;
15
use Zalas\Toolbox\Tool\Command\OptimisedComposerBinPluginCommand;
16
use Zalas\Toolbox\Tool\Command\PharDownloadCommand;
17
use Zalas\Toolbox\Tool\Command\PhiveInstallCommand;
18
use Zalas\Toolbox\Tool\Command\ShCommand;
19
use Zalas\Toolbox\Tool\Filter;
20
use Zalas\Toolbox\Tool\Tool;
21
use Zalas\Toolbox\Tool\Tools;
22
23
class InstallTools
24
{
25
    public const PRE_INSTALLATION_TAG = 'pre-installation';
26
27
    private Tools $tools;
28
29 14
    public function __construct(Tools $tools)
30
    {
31 14
        $this->tools = $tools;
32
    }
33
34 13
    public function __invoke(Filter $filter): Command
35
    {
36 13
        $tools = $this->tools->all($filter);
37 13
        $installationCommands = $this->installationCommands($tools);
38 13
        $commandFilter = $this->commandFilter($this->toolCommands($tools));
39
40 13
        return new MultiStepCommand(
41 13
            $installationCommands
42 13
                ->merge($commandFilter(ShCommand::class))
43 13
                ->merge($commandFilter(FileDownloadCommand::class))
44 13
                ->merge($commandFilter(PharDownloadCommand::class))
45 13
                ->merge($commandFilter(PhiveInstallCommand::class))
46 13
                ->merge($commandFilter(MultiStepCommand::class))
47 13
                ->merge($this->groupComposerGlobalInstallCommands($commandFilter(ComposerGlobalInstallCommand::class)))
48 13
                ->merge($this->groupComposerBinPluginCommands($commandFilter(ComposerBinPluginCommand::class)))
49 13
                ->merge($commandFilter(ComposerInstallCommand::class))
50 13
                ->merge($commandFilter(BoxBuildCommand::class))
51 13
        );
52
    }
53
54 13
    private function commandFilter(Collection $commands): Closure
55
    {
56 13
        return function ($type) use ($commands) {
57 13
            return $commands->filter(function (Command $command) use ($type) {
58 13
                return $command instanceof $type;
59 13
            });
60 13
        };
61
    }
62
63 13
    private function installationCommands(Collection $tools): Collection
64
    {
65 13
        return $tools->filter(function (Tool $tool) {
66 13
            return \in_array(self::PRE_INSTALLATION_TAG, $tool->tags());
67 13
        })->map(function (Tool $tool) {
68 1
            return $tool->command();
69 13
        });
70
    }
71
72 13
    private function toolCommands(Collection $tools): Collection
73
    {
74 13
        return $tools->filter(function (Tool $tool) {
75 13
            return !\in_array(self::PRE_INSTALLATION_TAG, $tool->tags());
76 13
        })->map(function (Tool $tool) {
77 13
            return $tool->command();
78 13
        });
79
    }
80
81 13
    private function groupComposerGlobalInstallCommands(Collection $commands): Collection
82
    {
83 13
        $commands = $commands->empty() ? [] : [new ComposerGlobalMultiInstallCommand($commands)];
84
85 13
        return Collection::create($commands);
86
    }
87
88 13
    private function groupComposerBinPluginCommands(Collection $commands): Collection
89
    {
90 13
        $commands = $commands->empty() ? [] : [new OptimisedComposerBinPluginCommand($commands)];
91
92 13
        return Collection::create($commands);
93
    }
94
}
95