Updating::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2.0625
1
<?php
2
namespace Samurai\Module\Task;
3
4
use Samurai\Task\ITask;
5
use Samurai\Task\Task;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class Updating
11
 * @package Samurai\Module\Task
12
 * @author Raphaël Lefebvre <[email protected]>
13
 */
14
class Updating extends Task
15
{
16
17
    /**
18
     * @param InputInterface $input
19
     * @param OutputInterface $output
20
     * @return int|null
21
     */
22 3
    public function execute(InputInterface $input, OutputInterface $output)
23
    {
24 3
        if($input->getArgument('name')) {
25 3
            return $this->updateModule($input, $output);
26
        }
27
        return $this->updateModules($output);
28
    }
29
30
    /**
31
     * @param InputInterface $input
32
     * @param OutputInterface $output
33
     * @return int
34
     */
35 3 View Code Duplication
    private function updateModule(InputInterface $input, OutputInterface $output)
36 3
    {
37 3
        $moduleName = $input->getArgument('name');
38 3
        if (!$this->getService('module_manager')->has($moduleName)) {
39 1
            $output->writeln(sprintf('<error>Error: no module "%s" found</error>', $moduleName));
40 1
            return ITask::BLOCKING_ERROR_CODE;
41
        }
42
43 2
        $this->getService('module_procedure')->setOutput($output);
44 2
        $this->getService('module_procedure')->update($this->getService('module_manager')->get($moduleName));
45 2
        return ITask::NO_ERROR_CODE;
46
    }
47
48
    /**
49
     * @param OutputInterface $output
50
     * @return int
51
     */
52
    private function updateModules(OutputInterface $output)
53
    {
54
        $hasError = false;
55
        $this->getService('module_procedure')->setOutput($output);
56
        foreach($this->getService('module_manager')->getAll() as $module){
57
            if($this->getService('module_procedure')->update($module)){
58
                $hasError = true;
59
            }
60
        }
61
        return $hasError ? ITask::NO_ERROR_CODE : ITask::NON_BLOCKING_ERROR_CODE;
62
    }
63
}
64