Completed
Push — master ( 001063...54521d )
by recca
01:40
created

Code::mergeAttributes()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 21
cts 21
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 24
nop 2
crap 6
1
<?php
2
3
namespace Recca0120\Generator;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Illuminate\Filesystem\Filesystem;
8
use Recca0120\Generator\Fixers\UseSortFixer;
9
10
class Code
11
{
12
    private $name;
13
14
    private $className;
15
16
    private $config;
17
18
    private $attributes = [];
19
20
    private $dependencies = [];
21
22
    private $files;
23
24
    private $useSortFixer;
25
26 4
    public function __construct($name, $config, $dependencies = [], Filesystem $files = null, UseSortFixer $useSortFixer = null)
27
    {
28 4
        $this->files = $files ?: new Filesystem;
29 4
        $this->useSortFixer = $useSortFixer ?: new UseSortFixer();
30 4
        $this->useSortFixer->setSortType(UseSortFixer::SORT_TYPE_LENGTH);
31 4
        $this->name = $name;
32 4
        $this->config = $config;
33 4
        $this->dependencies = $dependencies;
34
35 4
        $this->className = $this->name.Arr::get($this->config, 'suffix', '');
36 4
        $this->attributes = $this->mergeAttributes(
37 4
            $dependencies,
38 4
            Arr::get($this->config, 'plugins', [])
39
        );
40 4
    }
41
42
    public function __toString()
43
    {
44
        return $this->render();
45
    }
46
47 2
    public function getAttributes($prefix = null)
48
    {
49 2
        if (empty($prefix) === true) {
50
            return $this->attributes;
51
        }
52
53 2
        $attributes = [];
54 2
        foreach ($this->attributes as $key => $value) {
55 2
            $attributes[str_replace('-', '_', $prefix.'_'.$key)] = $value;
56
        }
57
58 2
        return $attributes;
59
    }
60
61 4
    public function render()
62
    {
63 4
        return $this->format($this->renderStub());
64
    }
65
66 1
    public function store()
67
    {
68 1
        foreach ($this->dependencies as $dependency) {
69 1
            $dependency->store();
70
        }
71
72 1
        $file = Arr::get($this->config, 'path', '').'/'.$this->className.'.'.Arr::get($this->config, 'extension', 'php');
73 1
        $directory = dirname($file);
74
75 1
        if ($this->files->isDirectory($directory) === false) {
76 1
            $this->files->makeDirectory($directory, 0755, true);
77
        }
78
79 1
        return $this->files->exists($file) === false
80 1
            ? $this->files->put($file, $this->render())
81 1
            : false;
82
    }
83
84 4
    private function renderStub()
85
    {
86 4
        return strtr($this->files->get($this->config['stub']), $this->getDummyAttributes());
87
    }
88
89 4
    private function format($content)
90
    {
91 4
        $fixedContent = $this->useSortFixer->fix($content);
92
93 4
        return $fixedContent ? $fixedContent : $content;
94
    }
95
96 4
    private function mergeAttributes($dependencies, $plugins)
97
    {
98 4
        $attributes = array_merge(Arr::get($this->config, 'attributes', []), [
99 4
            'name' => $this->name,
100 4
            'class' => $this->className,
101
        ]);
102
103 4
        if (empty($attributes['extends']) === false) {
104 3
            $attributes['base_extends'] = basename($attributes['extends']);
105
        }
106
107 4
        if (empty($attributes['namespace']) === false) {
108 4
            $attributes['fully_qualified_name'] = '\\'.$attributes['namespace'].'\\'.$attributes['class'];
109 4
            $attributes['qualified_name'] = $attributes['namespace'].'\\'.$attributes['class'];
110
        }
111
112 4
        foreach ($dependencies as $name => $dependency) {
113 2
            $attributes = array_merge($attributes, $dependency->getAttributes($name));
114
        }
115
116 4
        foreach ($plugins as $pluginClass => $pluginConfig) {
117 2
            $plugin = new $pluginClass();
118 2
            $plugin->setConfig($pluginConfig);
119 2
            $plugin->setAttributes($attributes);
120 2
            $plugin->setFilesystem($this->files);
121 2
            $plugin->setUseSortFixer($this->useSortFixer);
122 2
            $processedAttributes = $plugin->process();
123 2
            if (is_array($processedAttributes) === true) {
124 2
                $attributes = array_merge($attributes, $processedAttributes);
125
            }
126
        }
127
128 4
        return $attributes;
129
    }
130
131 4
    private function getDummyAttributes()
132
    {
133 4
        $dummy = [];
134 4
        foreach ($this->attributes as $key => $value) {
135 4
            $dummy['Dummy'.Str::studly($key)] = $value;
136 4
            $dummy['dummy'.Str::studly($key)] = Str::camel($value);
137
        }
138
139 4
        return $dummy;
140
    }
141
}
142