Completed
Push — dev ( a8234e...9312cf )
by Zach
04:40
created

CommandCreator::resolveCommandNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Console;
4
5
use Yarak\Helpers\Creator;
6
use Yarak\Exceptions\WriteError;
7
use Yarak\Helpers\NamespaceResolver;
8
9
class CommandCreator extends Creator
10
{
11
    /**
12
     * Create a new command with given name.
13
     *
14
     * @param string $name
15
     *
16
     * @throws WriteError
17
     *
18
     * @return string
19
     */
20
    public function create($name)
21
    {
22
        $path = $this->config->getCommandsDirectory($name.'.php');
23
24
        if (!file_exists($path)) {
25
            $creator = new DirectoryCreator($this->output);
26
27
            $creator->create(false);
28
29
            $this->writeFile($path, $this->getStub($name));
30
31
            $this->output->writeInfo("Created command {$name}.");
32
33
            return $path;
34
        }
35
36
        throw WriteError::commandExists($name);
37
    }
38
39
    /**
40
     * Get the stub file and insert name.
41
     *
42
     * @param string $name
43
     *
44
     * @return string
45
     */
46
    protected function getStub($name)
47
    {
48
        $stub = file_get_contents(__DIR__.'/Stubs/command.stub');
49
50
        $stub = str_replace('CLASSNAME', $name, $stub);
51
52
        return $this->setNamespace(
53
            $stub,
54
            NamespaceResolver::resolve('console', 'Commands')
55
        );
56
    }
57
}
58