Saving::buildAlias()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 1
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 Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
11
/**
12
 * Class Saving
13
 * @package Samurai\Alias\Task
14
 * @author Raphaël Lefebvre <[email protected]>
15
 */
16
class Saving extends Task
17
{
18
    /**
19
     * @param InputInterface $input
20
     * @param OutputInterface $output
21
     * @return bool
22
     */
23 4
    public function execute(InputInterface $input, OutputInterface $output)
24
    {
25 4
        $alias = $this->buildAlias($input);
26 4
        if($this->canBeSaved($input, $output, $alias)) {
27 3
            $this->getService('alias_manager')->add($alias);
28 3
        }
29 4
        return ITask::NO_ERROR_CODE;
30
    }
31
32
    /**
33
     * @param InputInterface $input
34
     * @return Alias
35
     */
36 4 View Code Duplication
    private function buildAlias(InputInterface $input)
37
    {
38 4
        $alias = new Alias();
39 4
        $alias->setName($input->getArgument('name'));
40 4
        $alias->setDescription($input->getArgument('description'));
41 4
        $alias->setPackage($input->getArgument('bootstrap'));
42 4
        $alias->setVersion($input->getArgument('version'));
43 4
        $alias->setSource($input->getArgument('source'));
44 4
        return $alias;
45
    }
46
47
    /**
48
     * @param InputInterface $input
49
     * @param OutputInterface $output
50
     * @param Alias $alias
51
     * @return bool
52
     */
53 4
    private function canBeSaved(InputInterface $input, OutputInterface $output, Alias $alias)
54
    {
55 4
        return !$this->getService('alias_manager')->has($alias->getName())
56 4
            || $this->confirmOverride($input, $output, $alias);
57
    }
58
59
    /**
60
     * @param InputInterface $input
61
     * @param OutputInterface $output
62
     * @param Alias $alias
63
     * @return mixed
64
     */
65 2
    private function confirmOverride(InputInterface $input, OutputInterface $output, Alias $alias)
66
    {
67 2
        return $this->getService('helper_set')->get('question')->ask(
68 2
            $input,
69 2
            $output,
70 2
            $this->buildQuestion($alias, $this->getInitialAlias($alias))
71 2
        );
72
    }
73
74
    /**
75
     * @param Alias $newAlias
76
     * @return Alias
77
     */
78 2
    private function getInitialAlias(Alias $newAlias)
79
    {
80 2
        return $this->getService('alias_manager')->get($newAlias->getName());
81
    }
82
83
    /**
84
     * @param Alias $newAlias
85
     * @param Alias $oldAlias
86
     * @return ConfirmationQuestion
87
     */
88 2
    private function buildQuestion(Alias $newAlias, Alias $oldAlias)
89
    {
90 2
        return new ConfirmationQuestion(
91 2
            sprintf(
92 2
                '<question>Do you want to override the bootstrap "%s" with "%s" </question>[y]',
93 2
                trim($oldAlias->getPackage() . ' ' . $oldAlias->getVersion()),
94 2
                trim($newAlias->getPackage() . ' ' . $newAlias->getVersion())
95 2
            )
96 2
        );
97
    }
98
}
99