Completed
Push — master ( d6febc...001063 )
by recca
01:35
created

Code   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.31%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 0
loc 130
ccs 61
cts 64
cp 0.9531
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A __toString() 0 4 1
A getAttributes() 0 13 3
A render() 0 4 1
A store() 0 17 4
A renderStub() 0 4 1
A format() 0 4 2
B mergeAttributes() 0 34 6
A getDummyAttributes() 0 10 2
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(), Arr::get($this->config, 'sort', true));
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, $useSort = false)
90
    {
91 4
        return $useSort === true ? $this->useSortFixer->fix($content) : $content;
92
    }
93
94 4
    private function mergeAttributes($dependencies, $plugins)
95
    {
96 4
        $attributes = array_merge(Arr::get($this->config, 'attributes', []), [
97 4
            'name' => $this->name,
98 4
            'class' => $this->className,
99
        ]);
100
101 4
        if (empty($attributes['extends']) === false) {
102 3
            $attributes['base_extends'] = basename($attributes['extends']);
103
        }
104
105 4
        if (empty($attributes['namespace']) === false) {
106 4
            $attributes['fully_qualified_name'] = '\\'.$attributes['namespace'].'\\'.$attributes['class'];
107 4
            $attributes['qualified_name'] = $attributes['namespace'].'\\'.$attributes['class'];
108
        }
109
110 4
        foreach ($dependencies as $name => $dependency) {
111 2
            $attributes = array_merge($attributes, $dependency->getAttributes($name));
112
        }
113
114 4
        foreach ($plugins as $pluginClass => $pluginConfig) {
115 2
            $plugin = new $pluginClass();
116 2
            $plugin->setConfig($pluginConfig);
117 2
            $plugin->setAttributes($attributes);
118 2
            $plugin->setFilesystem($this->files);
119 2
            $plugin->setUseSortFixer($this->useSortFixer);
120 2
            $processedAttributes = $plugin->process();
121 2
            if (is_array($processedAttributes) === true) {
122 2
                $attributes = array_merge($attributes, $processedAttributes);
123
            }
124
        }
125
126 4
        return $attributes;
127
    }
128
129 4
    private function getDummyAttributes()
130
    {
131 4
        $dummy = [];
132 4
        foreach ($this->attributes as $key => $value) {
133 4
            $dummy['Dummy'.Str::studly($key)] = $value;
134 4
            $dummy['dummy'.Str::studly($key)] = Str::camel($value);
135
        }
136
137 4
        return $dummy;
138
    }
139
}
140