Completed
Push — master ( 54521d...b3f719 )
by recca
01:44
created

Code::mergeAttributes()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 8.0036

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 25
cts 26
cp 0.9615
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 26
nc 48
nop 2
crap 8.0036
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
        foreach ($attributes as $key => $value) {
104 4
            if (Str::endsWith($key, 'qualified_class') === false) {
105 4
                continue;
106
            }
107
108 3
            $key = str_replace('qualified_', '', $key);
109
110 3
            if (empty($attributes[$key]) === false) {
111
                continue;
112
            }
113
114 3
            $attributes[$key] = basename(str_replace('//', '/', $value));
115
        }
116
117 4
        if (empty($attributes['namespace']) === false) {
118 4
            $attributes['fully_qualified_class'] = '\\'.$attributes['namespace'].'\\'.$attributes['class'];
119 4
            $attributes['qualified_class'] = $attributes['namespace'].'\\'.$attributes['class'];
120
        }
121
122 4
        foreach ($dependencies as $name => $dependency) {
123 2
            $attributes = array_merge($attributes, $dependency->getAttributes($name));
124
        }
125
126 4
        foreach ($plugins as $pluginClass => $pluginConfig) {
127 2
            $plugin = new $pluginClass();
128 2
            $plugin->setConfig($pluginConfig);
129 2
            $plugin->setAttributes($attributes);
130 2
            $plugin->setFilesystem($this->files);
131 2
            $plugin->setUseSortFixer($this->useSortFixer);
132 2
            $processedAttributes = $plugin->process();
133 2
            if (is_array($processedAttributes) === true) {
134 2
                $attributes = array_merge($attributes, $processedAttributes);
135
            }
136
        }
137
138 4
        return $attributes;
139
    }
140
141 4
    private function getDummyAttributes()
142
    {
143 4
        $dummy = [];
144 4
        foreach ($this->attributes as $key => $value) {
145 4
            $dummy['Dummy'.Str::studly($key)] = $value;
146 4
            $dummy['dummy'.Str::studly($key)] = Str::camel($value);
147
        }
148
149 4
        return $dummy;
150
    }
151
}
152