Completed
Push — dev ( 6d8737...fe3822 )
by Zach
02:19
created

CommandCreator::getStub()   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 1
dl 0
loc 6
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
    public function create($name)
41
    {
42
        if (class_exists($name)) {
43
            throw WriteError::classExists($name);
44
        }
45
46
        $commandsDir = $this->config->getCommandsDirectory();
47
48
        $this->makeDirectoryStructure([$commandsDir]);
49
50
        $this->writeFile(
51
            $path = $commandsDir.$name.'.php',
52
            $this->getStub($name)
53
        );
54
55
        $this->output->writeInfo("Successfully created command {$name}.");
56
57
        return $path;
58
    }
59
60
    /**
61
     * Get the stub file and insert name.
62
     *
63
     * @param  string $name
64
     *
65
     * @return string
66
     */
67
    protected function getStub($name)
68
    {
69
        $stub = file_get_contents(__DIR__.'/Stubs/command.stub');
70
71
        return str_replace('CLASSNAME', $name, $stub);
72
    }
73
}
74