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

Creator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Helpers;
4
5
use Yarak\Config\Config;
6
use Yarak\Helpers\Filesystem;
7
use Yarak\Console\Output\Output;
8
9
abstract class Creator
10
{
11
    use Filesystem;
12
13
    /**
14
     * Yarak config.
15
     *
16
     * @var Config
17
     */
18
    protected $config;
19
20
    /**
21
     * Output strategy.
22
     *
23
     * @var Output
24
     */
25
    protected $output;
26
27
    /**
28
     * Construct.
29
     *
30
     * @param Config $config
31
     * @param Output $output
32
     */
33
    public function __construct(Config $config, Output $output)
34
    {
35
        $this->config = $config;
36
        $this->output = $output;
37
    }
38
39
    /**
40
     * Guess a namespace based on a path.
41
     *
42
     * @param string $path
43
     *
44
     * @return string|null
45
     */
46
    protected function guessNamespace($path)
47
    {
48
        if (defined('APP_PATH')) {
49
            $appPathArray = explode('/', APP_PATH);
50
51
            $relativePath = array_diff(explode('/', $path), $appPathArray);
52
53
            array_unshift($relativePath, array_pop($appPathArray));
54
55
            $relativePath = array_map('ucfirst', $relativePath);
56
57
            return implode('\\', $relativePath);
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * Set the namespace in the command stub.
65
     *
66
     * @param string $stub
67
     * @param string $namespace
68
     *
69
     * @return string
70
     */
71
    protected function setNamespace($stub, $namespace = null)
72
    {
73
        if ($namespace !== null) {
74
            return str_replace(
75
                'NAMESPACE',
76
                "\nnamespace {$namespace};\n",
77
                $stub
78
            );
79
        }
80
81
        return str_replace('NAMESPACE', '', $stub);
82
    }
83
}
84