Installing::execute()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5.1158

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 3
nop 2
crap 5.1158
1
<?php
2
namespace Samurai\Module\Task;
3
4
use Samurai\Module\Module;
5
use Samurai\Task\ITask;
6
use Samurai\Task\Task;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
11
/**
12
 * Class Installing
13
 * @package Samurai\Module\Task
14
 * @author Raphaël Lefebvre <[email protected]>
15
 */
16
class Installing extends Task
17
{
18
    /**
19
     * @var array
20
     */
21
    private $modules = [
22
        'git' => [
23
            'package' => 'raphhh/samurai-module-git',
24
            'version' => '',
25
        ],
26
        'cleaner' => [
27
            'package' => 'raphhh/samurai-module-cleaner',
28
            'version' => '',
29
        ],
30
    ];
31
32
    /**
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     * @return int|null
36
     */
37 5
    public function execute(InputInterface $input, OutputInterface $output)
38
    {
39 5
        if($input->getArgument('name') && !$input->getArgument('package')) {
40
            throw new \InvalidArgumentException('bootstrap param is mandatory for this action');
41
        }
42
43 5
        if($input->getArgument('name') && $input->getArgument('package')){
44 1
            return $this->installModule($input, $output);
45
        }
46
47 4
        return $this->installModules($input, $output);
48
    }
49
50
    /**
51
     * @param InputInterface $input
52
     * @param OutputInterface $output
53
     * @return int|null
54
     */
55 4
    private function installModules(InputInterface $input, OutputInterface $output)
56
    {
57 4
        $output->writeln('<info>Starting modules installation</info>');
58
59 4
        foreach($this->modules as $name => $moduleData){
60
61 4
            if($this->getService('module_manager')->getByPackage($moduleData['package'])->count()){
62
63 1
                $output->writeln(sprintf('<info>Module "%s" already installed</info>', $moduleData['package']));
64 1
                continue;
65
            }
66
67 3
            if($this->confirmInstall($input, $output, $moduleData['package'])){
68 2
                $this->getService('module_procedure')->setOutput($output);
69 2
                $this->getService('module_procedure')->import($this->buildModule($name, $moduleData));
70 2
            }
71 4
        }
72 4
        return ITask::NO_ERROR_CODE;
73
    }
74
75
    /**
76
     * @param InputInterface $input
77
     * @param OutputInterface $output
78
     * @param string $modulePackage
79
     * @return bool
80
     */
81 3
    private function confirmInstall(InputInterface $input, OutputInterface $output, $modulePackage)
82
    {
83 3
        return $this->getService('helper_set')->get('question')->ask(
84 3
            $input,
85 3
            $output,
86 3
            $this->buildQuestion($modulePackage)
87 3
        );
88
    }
89
90
    /**
91
     * @param string $modulePackage
92
     * @return ConfirmationQuestion
93
     */
94 4
    private function buildQuestion($modulePackage)
95
    {
96 3
        return new ConfirmationQuestion(
97 3
            sprintf(
98 3
                '<question>Do you want to install the module "%s"</question>[y]',
99
                $modulePackage
100 3
            )
101 4
        );
102
    }
103
104
    /**
105
     * @param $name
106
     * @param array $moduleData
107
     * @return Module
108
     */
109 2
    private function buildModule($name, array $moduleData)
110
    {
111 2
        $module = new Module();
112 2
        $module->setName($name);
113 2
        $module->setPackage($moduleData['package']);
114 2
        $module->setVersion($moduleData['version']);
115 2
        $module->setIsEnable(true);
116 2
        return $module;
117
    }
118
119
    /**
120
     * @param $input
121
     * @param $output
122
     * @return bool
123
     */
124 1
    private function installModule($input, $output)
125
    {
126 1
        $task = new Saving($this->getServices());
127 1
        return $task->execute($input, $output);
128
    }
129
}
130