ConfigCommandHandler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 7
c 6
b 1
f 1
lcom 1
cbo 6
dl 0
loc 97
ccs 30
cts 30
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handleReset() 0 6 1
A __construct() 0 4 1
A handleList() 0 23 3
A handleShow() 0 9 1
A handleSet() 0 8 1
1
<?php
2
3
/*
4
 * This file is part of the puli/cli 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\Module\RootModuleFileManager;
17
use Webmozart\Console\Api\Args\Args;
18
use Webmozart\Console\Api\IO\IO;
19
use Webmozart\Console\UI\Component\Table;
20
21
/**
22
 * Handles the "config" command.
23
 *
24
 * @since  1.0
25
 *
26
 * @author Bernhard Schussek <[email protected]>
27
 */
28
class ConfigCommandHandler
29
{
30
    /**
31
     * @var RootModuleFileManager
32
     */
33
    private $manager;
34
35
    /**
36
     * Creates the handler.
37
     *
38
     * @param RootModuleFileManager $manager The root module file manager
39
     */
40 18
    public function __construct(RootModuleFileManager $manager)
41
    {
42 18
        $this->manager = $manager;
43 18
    }
44
45
    /**
46
     * Handles the "config" command.
47
     *
48
     * @param Args $args The console arguments
49
     * @param IO   $io   The I/O
50
     *
51
     * @return int The status code
52
     */
53 6
    public function handleList(Args $args, IO $io)
54
    {
55 6
        $raw = !$args->isOptionSet('parsed');
56 6
        $userValues = $this->manager->getConfigKeys(false, false, $raw);
57 6
        $effectiveValues = $this->manager->getConfigKeys(true, true, $raw);
58
59 6
        $table = new Table(PuliTableStyle::borderless());
60 6
        $table->setHeaderRow(array('Config Key', 'User Value', 'Effective Value'));
61
62 6
        foreach ($effectiveValues as $key => $value) {
63 6
            $table->addRow(array(
64 6
                sprintf('<c1>%s</c1>', $key),
65 6
                array_key_exists($key, $userValues)
66 6
                    ? StringUtil::formatValue($userValues[$key], false)
67 6
                    : '',
68 6
                StringUtil::formatValue($value, false),
69
            ));
70
        }
71
72 6
        $table->render($io);
73
74 6
        return 0;
75
    }
76
77
    /**
78
     * Handles the "config <key>" command.
79
     *
80
     * @param Args $args The console arguments
81
     * @param IO   $io   The I/O
82
     *
83
     * @return int The status code
84
     */
85 6
    public function handleShow(Args $args, IO $io)
86
    {
87 6
        $raw = !$args->isOptionSet('parsed');
88 6
        $value = $this->manager->getConfigKey($args->getArgument('key'), null, true, $raw);
89
90 6
        $io->writeLine(StringUtil::formatValue($value, false));
91
92 6
        return 0;
93
    }
94
95
    /**
96
     * Handles the "config <key> <value>" command.
97
     *
98
     * @param Args $args The console arguments
99
     *
100
     * @return int The status code
101
     */
102 5
    public function handleSet(Args $args)
103
    {
104 5
        $value = StringUtil::parseValue($args->getArgument('value'));
105
106 5
        $this->manager->setConfigKey($args->getArgument('key'), $value);
107
108 5
        return 0;
109
    }
110
111
    /**
112
     * Handles the "config -r <key>" command.
113
     *
114
     * @param Args $args The console arguments
115
     *
116
     * @return int The status code
117
     */
118 1
    public function handleReset(Args $args)
119
    {
120 1
        $this->manager->removeConfigKey($args->getArgument('key'));
121
122 1
        return 0;
123
    }
124
}
125