Completed
Push — dev ( fe3822...8e18b7 )
by Zach
02:29
created

CommandCreator::setNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Console;
4
5
use Yarak\Config\Config;
6
use Yarak\Helpers\Filesystem;
7
use Yarak\Console\Output\Output;
8
use Yarak\Exceptions\WriteError;
9
10
class CommandCreator
11
{
12
    use Filesystem;
13
14
    /**
15
     * Yarak config.
16
     *
17
     * @var Config
18
     */
19
    protected $config;
20
21
    /**
22
     * Output strategy.
23
     *
24
     * @var Output
25
     */
26
    protected $output;
27
28
    /**
29
     * Construct.
30
     *
31
     * @param Config $config
32
     * @param Output $output
33
     */
34
    public function __construct(Config $config, Output $output)
35
    {
36
        $this->config = $config;
37
        $this->output = $output;
38
    }
39
40
    /**
41
     * Create a new command with given name.
42
     *
43
     * @param string $name
44
     *
45
     * @return string
46
     */
47
    public function create($name)
48
    {
49
        if (class_exists($name)) {
50
            throw WriteError::classExists($name);
51
        }
52
53
        $commandsDir = $this->config->getCommandsDirectory();
54
55
        $this->makeDirectoryStructure([$commandsDir]);
56
57
        $this->writeFile(
58
            $path = $commandsDir.$name.'.php',
59
            $this->getStub($name)
60
        );
61
62
        $this->output->writeInfo("Successfully created command {$name}.");
63
64
        return $path;
65
    }
66
67
    /**
68
     * Get the stub file and insert name.
69
     *
70
     * @param string $name
71
     *
72
     * @return string
73
     */
74
    protected function getStub($name)
75
    {
76
        $stub = file_get_contents(__DIR__.'/Stubs/command.stub');
77
78
        $stub = str_replace('CLASSNAME', $name, $stub);
79
80
        return $this->setNamespace($stub, $this->resolveNamespace());
81
    }
82
83
    /**
84
     * Resolve the command namespace.
85
     *
86
     * @return string
87
     */
88
    protected function resolveNamespace()
89
    {
90
        if ($this->config->has(['namespaces', 'commandsNamespace'])) {
91
            return $this->config->get(['namespaces', 'commandsNamespace']);
92
        }
93
        
94
        return $this->guessNamespace($this->config->getCommandsDirectory());
95
    }
96
97
    /**
98
     * Set the namespace in the command stub.
99
     *
100
     * @param string $stub
101
     * @param string $namespace
102
     *
103
     * @return string
104
     */
105
    protected function setNamespace($stub, $namespace = null)
106
    {
107
        if ($namespace) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $namespace of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
108
            return str_replace(
109
                'NAMESPACE',
110
                "\nnamespace {$namespace};\n",
111
                $stub
112
            );
113
        }
114
115
        return str_replace('NAMESPACE', '', $stub);
116
    }
117
}
118