Passed
Push — master ( 254d09...309d02 )
by Jakub
01:36
created

InstallTools::groupComposerGlobalInstallCommands()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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