DirectoryCreator::create()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Console;
4
5
use Yarak\Helpers\Creator;
6
use Yarak\Helpers\NamespaceResolver;
7
8
class DirectoryCreator extends Creator
9
{
10
    /**
11
     * Create all directories and files for console.
12
     *
13
     * @param bool $createExample
14
     * @param bool $verbose
15
     */
16
    public function create($createExample = true, $verbose = true)
17
    {
18
        $this->config->validate(['application', 'consoleDir']);
19
20
        $createdDirs = (bool) $this->makeDirectoryStructure([
21
            'console'  => $this->config->getConsoleDirectory(),
22
            'commands' => $this->config->getCommandsDirectory(),
23
        ], $this->output);
24
25
        $createdKernel = $this->createKernel($createExample);
26
27
        $createdExample = $createExample ? $this->createExampleCommand() : false;
28
29
        if ($verbose) {
30
            $this->outputNothingCreated([$createdDirs, $createdKernel, $createdExample]);
31
        }
32
    }
33
34
    /**
35
     * Create the kernel file.
36
     *
37
     * @param bool $createExample
38
     *
39
     * @return bool
40
     */
41 View Code Duplication
    protected function createKernel($createExample)
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...
42
    {
43
        $path = $this->config->getConsoleDirectory('Kernel.php');
44
45
        if (!file_exists($path)) {
46
            $this->writeFile(
47
                $path,
48
                $this->getKernelStub($createExample)
49
            );
50
51
            $this->output->writeInfo('Created kernel file.');
52
53
            return true;
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * Get the kernel stub.
61
     *
62
     * @param bool $createExample [<description>]
63
     *
64
     * @return string
65
     */
66
    protected function getKernelStub($createExample)
67
    {
68
        $stub = file_get_contents(__DIR__.'/Stubs/kernel.stub');
69
70
        $replace = $createExample ? 'ExampleCommand::class' : '//';
71
72
        $stub = str_replace('COMMAND', $replace, $stub);
73
74
        $stub = str_replace(
75
            'USE',
76
            NamespaceResolver::resolve('console', 'Commands'),
77
            $stub
78
        );
79
80
        return $this->setNamespace($stub, NamespaceResolver::resolve('console'));
81
    }
82
83
    /**
84
     * Create an example command.
85
     *
86
     * @return bool
87
     */
88 View Code Duplication
    protected function createExampleCommand()
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...
89
    {
90
        $path = $this->config->getCommandsDirectory('ExampleCommand.php');
91
92
        if (!file_exists($path)) {
93
            $this->writeFile(
94
                $path,
95
                $this->getExampleStub()
96
            );
97
98
            $this->output->writeInfo('Created example command file.');
99
100
            return true;
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * Get the example command stub.
108
     *
109
     * @return string
110
     */
111
    protected function getExampleStub()
112
    {
113
        $stub = file_get_contents(__DIR__.'/Stubs/exampleCommand.stub');
114
115
        return $this->setNamespace($stub, NamespaceResolver::resolve('console', 'Commands'));
116
    }
117
}
118