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\Alias\Task;
3
4
use Samurai\Alias\Alias;
5
use Samurai\Task\ITask;
6
use Samurai\Task\Task;
7
use SimilarText\Finder;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Class Listing
13
 * @package Samurai\Alias\Task
14
 * @author Raphaël Lefebvre <[email protected]>
15
 */
16
class Listing extends Task
17
{
18
    /**
19
     * @param InputInterface $input
20
     * @param OutputInterface $output
21
     * @return int
22
     */
23 8
    public function execute(InputInterface $input, OutputInterface $output)
24
    {
25 8
        if($input->getArgument('name')){
26 2
            return $this->listAlias($input->getArgument('name'), $output);
27
        }
28 6
        return $this->listAliases($input, $output);
29
    }
30
31
    /**
32
     * @param InputInterface $input
33
     * @param OutputInterface $output
34
     * @return int
35
     */
36 6
    private function listAliases(InputInterface $input, OutputInterface $output)
37
    {
38 6
        if ($input->getOption('global')) {
39 2
            $output->writeln($this->mapAliases($this->getService('alias_manager')->getGlobal()));
40 6
        } elseif ($input->getOption('local')) {
41 2
            $output->writeln($this->mapAliases($this->getService('alias_manager')->getLocal()));
42 2
        } else {
43 2
            $output->writeln($this->mapAliases($this->getService('alias_manager')->getAll()));
44
        }
45 6
        return ITask::NO_ERROR_CODE;
46
    }
47
48
    /**
49
     * @param $aliasName
50
     * @param OutputInterface $output
51
     * @return int
52
     */
53 2 View Code Duplication
    private function listAlias($aliasName, OutputInterface $output)
54
    {
55 2
        if(!$this->getService('alias_manager')->has($aliasName)){
56 1
            $textFinder = new Finder($aliasName, array_keys($this->getService('alias_manager')->getAll()));
57 1
            $output->writeln(sprintf(
58 1
                '<error>Alias "%s" not found! Did you mean "%s"?</error>',
59 1
                $aliasName,
60 1
                $textFinder->first()
61 1
            ));
62 1
            return ITask::BLOCKING_ERROR_CODE;
63
        }
64 1
        $output->writeln($this->mapAlias($this->getService('alias_manager')->get($aliasName)));
65 1
        return ITask::NO_ERROR_CODE;
66
    }
67
68
    /**
69
     * @param Alias[] $aliases
70
     * @return string
71
     */
72 6
    private function mapAliases(array $aliases)
73
    {
74 6
        $result = '';
75 6
        foreach($aliases as $name => $alias){
76 6
            $result .= $this->mapAlias($alias) . "\n\n";
77 6
        }
78 6
        return trim($result);
79
    }
80
    
81
    /**
82
     * @param Alias $alias
83
     * @return string
84
     */
85 7 View Code Duplication
    private function mapAlias(Alias $alias)
86
    {
87 7
        $result = '';
88 7
        foreach ($alias->toArray() as $property => $value) {
89 7
            if (is_scalar($value) || is_null($value)) {
90 7
                $result .= "$property: $value\n";
91 7
            }
92 7
        }
93 7
        return trim($result);
94
    }
95
}
96