PluginCommand::execute()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 2
1
<?php
2
3
namespace Padawan\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Padawan\Plugin\Package;
9
10
class PluginCommand extends CliCommand
11
{
12
    protected function configure()
13
    {
14
        $this->setName("plugin")
15
            ->setDescription("Manages plugins for your project")
16
            ->addArgument(
17
                "command_name",
18
                InputArgument::REQUIRED,
19
                "Command to execute. Can be add or remove"
20
            )->addArgument(
21
                "plugin_name",
22
                InputArgument::REQUIRED,
23
                "Plugin to work with"
24
            );
25
    }
26
    protected function execute(InputInterface $input, OutputInterface $output)
27
    {
28
        $commandName = $input->getArgument("command_name");
29
        $pluginName = $input->getArgument("plugin_name");
30
        /** @var \Padawan\Plugin\Package */
31
        $package = $this->getContainer()->get(Package::class);
32
        $plugins = $package->getPluginsList();
33
        if ($commandName === 'add') {
34
            if (array_key_exists($pluginName, $plugins)) {
35
                return;
36
            }
37
            $plugins[] = $pluginName;
38
        } elseif ($commandName === 'remove') {
39
            if (!array_key_exists($pluginName, $plugins)) {
40
                return;
41
            }
42
            unset($plugins[$pluginName]);
43
        }
44
        $package->writePluginsList($plugins);
45
    }
46
}
47