Listing::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
namespace Samurai\Module\Task;
3
4
use Samurai\Module\Module;
5
use Samurai\Module\Modules;
6
use Samurai\Task\ITask;
7
use Samurai\Task\Task;
8
use SimilarText\Finder;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Class Listing
14
 * @package Samurai\Module\Task
15
 * @author Raphaël Lefebvre <[email protected]>
16
 */
17
class Listing extends Task
18
{
19
    /**
20
     * @param InputInterface $input
21
     * @param OutputInterface $output
22
     * @return int
23
     */
24 4
    public function execute(InputInterface $input, OutputInterface $output)
25
    {
26 4
        if($input->getArgument('name')){
27 2
            return $this->listModule($input->getArgument('name'), $output);
28
        }
29 2
        return $this->listModules($output);
30
    }
31
32
    /**
33
     * @param $moduleName
34
     * @param OutputInterface $output
35
     * @return int
36
     */
37 2 View Code Duplication
    private function listModule($moduleName, OutputInterface $output)
38
    {
39 2
        if(!$this->getService('module_manager')->has($moduleName)){
40 1
            $textFinder = new Finder($moduleName, array_keys($this->getService('module_manager')->getAll()->getArrayCopy()));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 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...
41 1
            $output->writeln(sprintf(
42 1
                '<error>Module "%s" not found! Did you mean "%s"?</error>',
43 1
                $moduleName,
44 1
                $textFinder->first()
45 1
            ));
46 1
            return ITask::BLOCKING_ERROR_CODE;
47
        }
48 1
        $output->writeln($this->mapModule($this->getService('module_manager')->get($moduleName)));
49 1
        return ITask::NO_ERROR_CODE;
50
    }
51
52
    /**
53
     * @param OutputInterface $output
54
     * @return int
55
     */
56 2
    private function listModules(OutputInterface $output)
57
    {
58 2
        $output->writeln(sprintf("<info>%d module(s) set:</info>\n", count($this->getService('module_manager')->getAll())));
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...
59 2
        $output->writeln($this->mapModules($this->getService('module_manager')->getAll()));
60 2
        return ITask::NO_ERROR_CODE;
61
    }
62
63
    /**
64
     * @param Modules $modules
65
     * @return string
66
     */
67 2
    private function mapModules(Modules $modules)
68
    {
69 2
        $result = '';
70 2
        foreach($modules as $name => $module){
71 2
            $result .= $this->mapModule($module) . "\n\n";
72 2
        }
73 2
        return trim($result);
74
    }
75
76
    /**
77
     * @param Module $module
78
     * @return string
79
     */
80 3 View Code Duplication
    private function mapModule(Module $module)
81
    {
82 3
        $result = '';
83 3
        foreach ($module->toArray() as $property => $value) {
84 3
            if (is_scalar($value) || is_null($value)) {
85 3
                $result .= "$property: $value\n";
86 3
            }
87 3
        }
88 3
        return trim($result);
89
    }
90
}
91