Completed
Push — master ( c4c610...7af315 )
by Hannah
11:55
created

AliasCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 2
cbo 7
dl 0
loc 126
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 62 7
C interact() 0 46 8
1
<?php
2
3
namespace Geekish\Crap\Command;
4
5
use Geekish\Crap\CrapException;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
use Symfony\Component\Console\Question\Question;
11
12
/**
13
 * Class AliasCommand
14
 * @package Geekish\Crap\Command
15
 */
16
final class AliasCommand extends BaseCommand
17
{
18
    /**
19
     * @inheritDoc
20
     */
21
    protected function configure()
22
    {
23
        $this->setName("alias");
24
        $this->setDescription("Defines an alias for a package to be used by crap.");
25
        $this->addArgument("alias", InputArgument::REQUIRED, "Package alias");
26
        $this->addArgument("package", InputArgument::REQUIRED, "Package");
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $alias = $input->getArgument("alias");
35
        $package = $input->getArgument("package");
36
37
        if (!$this->helper->validateAlias($alias)) {
38
            throw CrapException::create(
39
                "The alias `%s` is invalid, it should be lowercase, and match: [a-z0-9_.-]+",
40
                $alias
41
            );
42
        }
43
44
        if (!$this->helper->validatePackage($package)) {
45
            throw CrapException::create(
46
                "The package `%s` is invalid, it should match: [a-z0-9_.-]+/[a-z0-9_.-]+",
47
                $input->getArgument("package")
48
            );
49
        }
50
51
        $override = false;
52
53
        if ($this->helper->hasAlias($alias)) {
54
            $current = $this->helper->getAlias($alias);
55
56
            if ($current == $package) {
57
                $output->writeln(sprintf(
58
                    "<comment>Alias `%s` to package `%s` already exists, silly.</comment>",
59
                    $alias,
60
                    $package
61
                ));
62
                return 0;
63
            }
64
65
            $helper = $this->getHelper("question");
66
67
            $output->writeln(sprintf(
68
                "<comment>Alias `%s` exists and is set to `%s`.</comment>",
69
                $alias,
70
                $current
71
            ));
72
73
            $ask = sprintf("Override alias `%s` with `%s`? (y/n) ", $alias, $package);
74
            $question = new ConfirmationQuestion($ask, false);
75
76
            if (!$helper->ask($input, $output, $question)) {
77
                return 0;
78
            }
79
80
            $override = true;
81
        }
82
83
        $this->helper->setAlias($alias, $package);
84
85
        $output->writeln(sprintf(
86
            "<success>Alias `%s` to package `%s` successfully %s.</success>",
87
            $alias,
88
            $package,
89
            $override ? "updated" : "added"
90
        ));
91
92
        return 0;
93
    }
94
95
    protected function interact(InputInterface $input, OutputInterface $output)
96
    {
97
        $args = array_values($input->getArguments());
98
99
        $helper = $this->getHelper("question");
100
101
        if ($args[2] == null && $this->helper->validatePackage($args[1]) === true) {
102
            $package = $args[1];
103
104
            $output->writeln("<comment>You provided the package but no alias!</comment>");
105
106
            $message = sprintf("<question>What do you want to use an an alias for `%s`?</question>", $package);
107
            $question = new Question($message . PHP_EOL, false);
108
109
            $alias = $helper->ask($input, $output, $question);
110
111
            $input->setArgument("alias", $alias);
112
            $input->setArgument("package", $package);
113
        } elseif ($args[2] == null && $this->helper->validateAlias($args[1])) {
114
            $alias = $args[1];
115
116
            $output->writeln("<info>You provided the alias but no package!</info>");
117
118
            $message = sprintf("<question>What package do you want to alias `%s` to?</question>", $alias);
119
            $question = new Question($message . PHP_EOL, false);
120
121
            $package = $helper->ask($input, $output, $question);
122
123
            $input->setArgument("package", $package);
124
        } elseif ($this->helper->validateAlias($args[2]) && $this->helper->validatePackage($args[1])) {
125
            $output->writeln("<info>It looks like you swapped the package and alias.</info>");
126
127
            $message = sprintf(
128
                "<question>Did you mean to alias `%s` to package `%s`?</question> (y/n) ",
129
                $args[2],
130
                $args[1]
131
            );
132
133
            $question = new ConfirmationQuestion($message, false);
134
135
            if ($helper->ask($input, $output, $question)) {
136
                $input->setArgument("alias", $args[2]);
137
                $input->setArgument("package", $args[1]);
138
            }
139
        }
140
    }
141
}
142