Completed
Pull Request — master (#27)
by Hari
09:21
created

InstallerCommandHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the puli/asset-plugin package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Cli\Handler;
13
14
use Puli\Cli\Style\PuliTableStyle;
15
use Puli\Cli\Util\StringUtil;
16
use Puli\Manager\Api\Installer\InstallerDescriptor;
17
use Puli\Manager\Api\Installer\InstallerManager;
18
use Puli\Manager\Api\Installer\InstallerParameter;
19
use RuntimeException;
20
use Webmozart\Console\Api\Args\Args;
21
use Webmozart\Console\Api\IO\IO;
22
use Webmozart\Console\UI\Component\Table;
23
24
/**
25
 * @since  1.0
26
 *
27
 * @author Bernhard Schussek <[email protected]>
28
 */
29
class InstallerCommandHandler
30
{
31
    /**
32
     * @var InstallerManager
33
     */
34
    private $installerManager;
35
36
    public function __construct(InstallerManager $installerManager)
37
    {
38
        $this->installerManager = $installerManager;
39
    }
40
41
    public function handleList(Args $args, IO $io)
42
    {
43
        $table = new Table(PuliTableStyle::borderless());
44
45
        $table->setHeaderRow(array('Name', 'Class', 'Description'));
46
47
        foreach ($this->installerManager->getInstallerDescriptors() as $descriptor) {
48
            $className = $descriptor->getClassName();
49
50
            if (!$args->isOptionSet('long')) {
51
                $className = StringUtil::getShortClassName($className);
52
            }
53
54
            $parameters = array();
55
56
            foreach ($descriptor->getParameters() as $parameterName => $parameter) {
57
                if (!$parameter->isRequired()) {
58
                    $parameterName .= '='.StringUtil::formatValue($parameter->getDefaultValue());
59
                }
60
61
                $parameters[] = $parameterName;
62
            }
63
64
            $description = $descriptor->getDescription();
65
66
            if (!empty($parameters)) {
67
                // non-breaking space
68
                $description .= ' <c1>('.implode(",\xc2\xa0", $parameters).')</c1>';
69
            }
70
71
            $table->addRow(array(
72
                '<u>'.$descriptor->getName().'</u>',
73
                '<c1>'.$className.'</c1>',
74
                $description,
75
            ));
76
        }
77
78
        $table->render($io);
79
80
        return 0;
81
    }
82
83
    public function handleAdd(Args $args)
84
    {
85
        $descriptions = $args->getOption('description');
86
        $parameters = array();
87
88
        // The first description is for the installer
89
        $description = $descriptions ? array_shift($descriptions) : null;
90
91 View Code Duplication
        foreach ($args->getOption('param') as $parameter) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
            // Subsequent descriptions are for the parameters
93
            $paramDescription = $descriptions ? array_shift($descriptions) : null;
94
95
            // Optional parameter with default value
96
            if (false !== ($pos = strpos($parameter, '='))) {
97
                $parameters[] = new InstallerParameter(
98
                    substr($parameter, 0, $pos),
99
                    InstallerParameter::OPTIONAL,
100
                    StringUtil::parseValue(substr($parameter, $pos + 1)),
101
                    $paramDescription
102
                );
103
104
                continue;
105
            }
106
107
            // Required parameter
108
            $parameters[] = new InstallerParameter(
109
                $parameter,
110
                InstallerParameter::REQUIRED,
111
                null,
112
                $paramDescription
113
            );
114
        }
115
116
        $this->installerManager->addRootInstallerDescriptor(new InstallerDescriptor(
117
            $args->getArgument('name'),
118
            $args->getArgument('class'),
119
            $description,
120
            $parameters
121
        ));
122
123
        return 0;
124
    }
125
126
    public function handleDelete(Args $args)
127
    {
128
        $installerName = $args->getArgument('name');
129
130
        if (!$this->installerManager->hasInstallerDescriptor($installerName)) {
131
            throw new RuntimeException(sprintf(
132
                'The installer "%s" does not exist.',
133
                $installerName
134
            ));
135
        }
136
137
        $this->installerManager->removeRootInstallerDescriptor($installerName);
138
139
        return 0;
140
    }
141
}
142