Completed
Push — master ( 4deee6...b383ba )
by Zach
05:43 queued 03:44
created

DirectoryCreator::getExampleStub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
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\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
        $createdDirs = (bool) count($this->makeDirectoryStructure([
19
            'console'  => $this->config->getConsoleDirectory(),
20
            'commands' => $this->config->getCommandsDirectory(),
21
        ], $this->output));
22
23
        $createdKernel = $this->createKernel($createExample);
24
25
        $createdExample = $createExample ? $this->createExampleCommand() : false;
26
27
        if ($verbose) {
28
            $this->outputNothingCreated([$createdDirs, $createdKernel, $createdExample]);
29
        }
30
    }
31
32
    /**
33
     * Create the kernel file.
34
     *
35
     * @param bool $createExample
36
     *
37
     * @return bool
38
     */
39 View Code Duplication
    protected function createKernel($createExample)
1 ignored issue
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...
40
    {
41
        $path = $this->config->getConsoleDirectory('Kernel.php');
42
43
        if (!file_exists($path)) {
44
            $this->writeFile(
45
                $path,
46
                $this->getKernelStub($createExample)
47
            );
48
49
            $this->output->writeInfo('Created kernel file.');
50
51
            return true;
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * Get the kernel stub.
59
     *
60
     * @param bool $createExample [<description>]
61
     *
62
     * @return string
63
     */
64 View Code Duplication
    protected function getKernelStub($createExample)
65
    {
66
        $stub = file_get_contents(__DIR__.'/Stubs/kernel.stub');
67
68
        $replace = $createExample ? 'ExampleCommand::class' : '//';
69
70
        $stub = str_replace('COMMAND', $replace, $stub);
71
72
        return $this->setNamespace($stub, NamespaceResolver::resolve('console'));
73
    }
74
75
    /**
76
     * Create an example command.
77
     *
78
     * @return bool
79
     */
80 View Code Duplication
    protected function createExampleCommand()
81
    {
82
        $path = $this->config->getCommandsDirectory('ExampleCommand.php');
83
84
        if (!file_exists($path)) {
85
            $this->writeFile(
86
                $path,
87
                $this->getExampleStub()
88
            );
89
90
            $this->output->writeInfo('Created example command file.');
91
92
            return true;
93
        }
94
95
        return false;
96
    }
97
98
    /**
99
     * Get the example command stub.
100
     *
101
     * @return string
102
     */
103
    protected function getExampleStub()
104
    {
105
        $stub = file_get_contents(__DIR__.'/Stubs/exampleCommand.stub');
106
107
        return $this->setNamespace($stub, NamespaceResolver::resolve('console', 'Commands'));
108
    }
109
}
110