Code::getAttributes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0175
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 5
    public function __construct($name, $config, $dependencies = [], Filesystem $files = null, UseSortFixer $useSortFixer = null)
27
    {
28 5
        $this->files = $files ?: new Filesystem;
29 5
        $this->useSortFixer = $useSortFixer ?: new UseSortFixer();
30 5
        $this->useSortFixer->setSortType(UseSortFixer::SORT_TYPE_LENGTH);
31 5
        $this->name = $name;
32 5
        $this->config = $config;
33 5
        $this->dependencies = $dependencies;
34
35 5
        $this->className = $this->name.Arr::get($this->config, 'suffix', '');
36 5
        $this->attributes = $this->mergeAttributes(
37 5
            $dependencies,
38 5
            Arr::get($this->config, 'plugins', [])
39 5
        );
40 5
    }
41
42
    public function __toString()
43
    {
44
        return $this->render();
45
    }
46
47 3
    public function getAttributes($prefix = null)
48
    {
49 3
        if (empty($prefix) === true) {
50
            return $this->attributes;
51
        }
52
53 3
        $attributes = [];
54 3
        foreach ($this->attributes as $key => $value) {
55 3
            $attributes[str_replace('-', '_', $prefix.'_'.$key)] = $value;
56 3
        }
57
58 3
        return $attributes;
59
    }
60
61 5
    public function render()
62
    {
63 5
        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 1
        }
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 1
        }
78
79 1
        return $this->files->exists($file) === false
80 1
            ? $this->files->put($file, $this->render())
81 1
            : false;
82
    }
83
84 5
    private function renderStub()
85
    {
86 5
        return strtr($this->files->get($this->config['stub']), $this->getDummyAttributes());
87
    }
88
89 5
    private function format($content)
90
    {
91 5
        $fixedContent = $this->useSortFixer->fix($content);
92
93 5
        return $fixedContent ? $fixedContent : $content;
94
    }
95
96 5
    private function mergeAttributes($dependencies, $plugins)
97
    {
98 5
        $attributes = array_merge(Arr::get($this->config, 'attributes', []), [
99 5
            'name' => $this->name,
100 5
            'class' => $this->className,
101 5
        ]);
102
103 5
        foreach ($attributes as $key => $value) {
104 5
            if (Str::endsWith($key, 'qualified_class') === false) {
105 5
                continue;
106
            }
107
108 4
            $key = str_replace('qualified_', '', $key);
109
110 4
            if (empty($attributes[$key]) === false) {
111
                continue;
112
            }
113
114 4
            $attributes[$key] = basename(str_replace('\\', '/', $value));
115 5
        }
116
117 5
        if (empty($attributes['namespace']) === false) {
118 5
            $attributes['fully_qualified_class'] = '\\'.$attributes['namespace'].'\\'.$attributes['class'];
119 5
            $attributes['qualified_class'] = $attributes['namespace'].'\\'.$attributes['class'];
120 5
        }
121
122 5
        foreach ($dependencies as $name => $dependency) {
123 3
            $attributes = array_merge($attributes, $dependency->getAttributes($name));
124 5
        }
125
126 5
        foreach ($plugins as $pluginClass => $pluginConfig) {
127 3
            $plugin = new $pluginClass();
128 3
            $plugin->setConfig($pluginConfig);
129 3
            $plugin->setAttributes($attributes);
130 3
            $plugin->setFilesystem($this->files);
131 3
            $plugin->setUseSortFixer($this->useSortFixer);
132 3
            $processedAttributes = $plugin->process();
133 3
            if (is_array($processedAttributes) === true) {
134
                $attributes = array_merge($attributes, $processedAttributes);
135
            }
136 5
        }
137
138 5
        return $attributes;
139
    }
140
141 5
    private function getDummyAttributes()
142
    {
143 5
        $dummy = [];
144 5
        foreach ($this->attributes as $key => $value) {
145 5
            $dummy['Dummy'.Str::studly($key)] = $value;
146 5
            $dummy['dummy'.Str::studly($key)] = Str::camel($value);
147 5
            $dummy['DummySingular'.Str::studly($key)] = Str::singular($value);
148 5
            $dummy['dummySingular'.Str::studly($key)] = Str::camel(Str::singular($value));
149 5
            $dummy['DummyPlural'.Str::studly($key)] = Str::plural($value);
150 5
            $dummy['dummyPlural'.Str::studly($key)] = Str::camel(Str::plural($value));
151 5
        }
152
153 5
        return $dummy;
154
    }
155
}
156