Running::runModules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
namespace Samurai\Module\Task;
3
4
use Samurai\Module\Module;
5
use Samurai\Module\Modules;
6
use Samurai\Module\Planner\ModulePlannerBuilder;
7
use Samurai\Module\Planner\ModulesPlannerBuilder;
8
use Samurai\Module\Planner\PlannerAdapter;
9
use Samurai\Task\ITask;
10
use Samurai\Task\Task;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Class Running
16
 * @package Samurai\Module\Task
17
 * @author Raphaël Lefebvre <[email protected]>
18
 */
19
class Running extends Task
20
{
21
    /**
22
     * @param InputInterface $input
23
     * @param OutputInterface $output
24
     * @return int|null
25
     */
26 3
    public function execute(InputInterface $input, OutputInterface $output)
27
    {
28 2
        if($input->getArgument('name')){
29 1
            if(!$this->getService('module_manager')->has($input->getArgument('name'))){
30
                $output->writeln(sprintf('<error>Module "%s" not found!</error>', $input->getArgument('name')));
31
                return ITask::BLOCKING_ERROR_CODE;
32
            }
33 1
            return $this->runModule($input, $output, $this->getService('module_manager')->get($input->getArgument('name')));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
34
        }
35 1
        return $this->runModules($input, $output, $this->getService('module_manager')->getAll());
36 3
    }
37
38
    /**
39
     * @param InputInterface $input
40
     * @param OutputInterface $output
41
     * @param Modules $modules
42
     * @return int|null
43
     */
44 2
    protected function runModules(InputInterface $input, OutputInterface $output, Modules $modules)
45
    {
46 2
        $modules = $this->filter($modules);
47 2
        $output->writeln(sprintf('<info>Running %d module(s)</info>', count($modules)));
48 2
        $planner = new PlannerAdapter(
49 2
            new ModulesPlannerBuilder($this->getServices(), $modules, $this->getService('helper_set')->get('question')),
50 2
            $this->getService('helper_set')->get('question')
51 2
        );
52 2
        return $planner->execute($input, $output);
53
    }
54
55
    /**
56
     * @param InputInterface $input
57
     * @param OutputInterface $output
58
     * @param Module $module
59
     * @return int|null
60
     */
61 1
    private function runModule(InputInterface $input, OutputInterface $output, Module $module)
62
    {
63 1
        $output->writeln('<info>Running the module "'.$module->getName().'"</info>');
64 1
        $planner = new PlannerAdapter(
65 1
            new ModulePlannerBuilder($this->getServices(), $module),
66 1
            $this->getService('helper_set')->get('question')
67 1
        );
68 1
        return $planner->execute($input, $output);
69
    }
70
71
    /**
72
     * @param Modules $modules
73
     * @return Modules
74
     */
75
    private function filter(Modules $modules)
76
    {
77 2
        return new Modules(array_filter($modules->getArrayCopy(), function(Module $module){
78 2
            return $module->isEnable();
79 2
        }));
80
    }
81
}
82