PhpNamespacePrompt::getValues()   B
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 18
cts 18
cp 1
rs 8.8571
cc 2
eloc 17
nc 1
nop 2
crap 2
1
<?php
2
3
namespace CL\ComposerInit\Prompt;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use Symfony\Component\Console\Helper\DialogHelper;
7
use CL\ComposerInit\GitConfig;
8
use CL\ComposerInit\Inflector;
9
use RuntimeException;
10
11
/**
12
 * @author    Ivan Kerin <[email protected]>
13
 * @copyright (c) 2014 Clippings Ltd.
14
 * @license   http://spdx.org/licenses/BSD-3-Clause
15
 */
16
class PhpNamespacePrompt implements PromptInterface
17
{
18
    /**
19
     * @var GitConfig
20
     */
21
    private $gitConfig;
22
23
    /**
24
     * @var Inflector
25
     */
26
    private $inflector;
27
28 1
    public function __construct(GitConfig $gitConfig, Inflector $inflector)
29
    {
30 1
        $this->gitConfig = $gitConfig;
31 1
        $this->inflector = $inflector;
32 1
    }
33
34
    /**
35
     * @return GitConfig
36
     */
37 1
    public function getGitConfig()
38
    {
39 1
        return $this->gitConfig;
40
    }
41
42
    /**
43
     * @return Inflector
44
     */
45 1
    public function getInflector()
46
    {
47 1
        return $this->inflector;
48
    }
49
50
    /**
51
     * @return array
52
     */
53 2
    public function getDefaults()
54
    {
55 2
        $origin = $this->gitConfig->getOrigin();
56 2
        if (null !== $origin) {
57 1
            list($vendor, $name) = explode('/', $origin);
58
59
            return [
60 1
                $this->inflector->titlecase($vendor).'\\'.$this->inflector->titlecase($name),
61 1
                $this->inflector->initials($vendor).'\\'.$this->inflector->titlecase($name),
62 1
            ];
63
        } else {
64 1
            return [];
65
        }
66
    }
67
68
    /**
69
     * @param  OutputInterface $output
70
     * @param  DialogHelper    $dialog
71
     * @return array
72
     */
73 1
    public function getValues(OutputInterface $output, DialogHelper $dialog)
74
    {
75 1
        $defaults = $this->getDefaults();
76
77 1
        $value = $dialog->askAndValidate(
78 1
            $output,
79 1
            "<info>PHP Namespace</info> ({$defaults[0]}): ",
80 1
            function ($namespace) {
81 1
                if (preg_match("/^([\w\\\\]+)*\w+$/", $namespace)) {
82 1
                    return $namespace;
83
                }
84
85 1
                throw new RuntimeException(sprintf(
86 1
                    '%s is not a valid namespace',
87
                    $namespace
88 1
                ));
89 1
            },
90 1
            false,
91 1
            reset($defaults),
92
            $defaults
93 1
        );
94
95
        return [
96 1
            'php_namespace' => $value,
97 1
            'php_namespace_escaped' => addslashes($value),
98 1
        ];
99
    }
100
}
101
102
103