Completed
Push — dev ( 6bece5...cad8d6 )
by Zach
02:11
created

CommandCreator::resolveCommandNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 10
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
8
class CommandCreator extends Creator
9
{
10
    /**
11
     * Create a new command with given name.
12
     *
13
     * @param string $name
14
     *
15
     * @return string
16
     */
17
    public function create($name)
18
    {
19
        if (class_exists($name)) {
20
            throw WriteError::classExists($name);
21
        }
22
23
        $directoryCreator = new DirectoryCreator($this->config, $this->output);
24
25
        $directoryCreator->create();
26
27
        $commandsDir = $this->config->getCommandsDirectory();
28
29
        $this->writeFile(
30
            $path = $commandsDir.$name.'.php',
31
            $this->getStub($name)
32
        );
33
34
        $this->output->writeInfo("Successfully created command {$name}.");
35
36
        return $path;
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($stub, $this->resolveCommandNamespace());
53
    }
54
55
    /**
56
     * Resolve the command namespace.
57
     *
58
     * @return string
59
     */
60 View Code Duplication
    protected function resolveCommandNamespace()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62
        if ($this->config->has(['namespaces', 'consoleNamespace'])) {
63
            return $this->config->get(
64
                ['namespaces', 'consoleNamespace']
65
            ).'\Commands';
66
        }
67
68
        return $this->guessNamespace($this->config->getCommandsDirectory());
69
    }
70
}
71