Passed
Push — feature/artisan-command ( f953e8...49b1ef )
by Andrea Marco
02:19
created

StubAssembler::export()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 16
nc 8
nop 3
dl 0
loc 30
ccs 17
cts 17
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A StubAssembler::enumsHaveValues() 0 9 3
1
<?php
2
3
namespace Cerbero\LaravelEnum;
4
5
/**
6
 * The stub assembler.
7
 *
8
 */
9
class StubAssembler
10
{
11
    use ExportsValues;
12
13
    /**
14
     * The stub to assemble.
15
     *
16
     * @var string
17
     */
18
    protected $stub;
19
20
    /**
21
     * The list of enum definitions.
22
     *
23
     * @var array
24
     */
25
    protected $enums;
26
27
    /**
28
     * Set the dependencies.
29
     *
30
     * @param string $stub
31
     * @param array $enums
32
     */
33 12
    public function __construct(string $stub, array $enums)
34
    {
35 12
        $this->stub = $stub;
36 12
        $this->enums = $enums;
37 12
    }
38
39
    /**
40
     * Retrieve the stub
41
     *
42
     * @return string
43
     */
44 12
    public function getStub() : string
45
    {
46 12
        return $this->stub;
47
    }
48
49
    /**
50
     * Replace the PHPDoc method tags for the given stub
51
     *
52
     * @return self
53
     */
54 12
    public function replaceMethodTags() : self
55
    {
56
        $methods = array_map(function (EnumDefinition $enum) {
57 12
            return " * @method static self {$enum->name}()";
58 12
        }, $this->enums);
59
60 12
        $this->stub = str_replace('DummyMethodTags', implode(PHP_EOL, $methods), $this->stub);
61
62 12
        return $this;
63
    }
64
65
    /**
66
     * Replace the constants for the given stub
67
     *
68
     * @return self
69
     */
70 12
    public function replaceConstants() : self
71
    {
72 12
        $padding = 4;
73
74
        $constants = array_map(function (EnumDefinition $enum) use ($padding) {
75 12
            $key = $this->export($enum->key, $padding);
76 12
            return str_repeat(' ', $padding) . "const {$enum->name} = {$key};";
77 12
        }, $this->enums);
78
79 12
        $this->stub = str_replace('DummyConstants', implode(PHP_EOL, $constants), $this->stub);
80
81 12
        return $this;
82
    }
83
84
    /**
85
     * Replace the map for the given stub
86
     *
87
     * @return self
88
     */
89 12
    public function replaceMap() : self
90
    {
91
        // Map enums key and value pairs only if enums have values
92 12
        if ($this->enumsHaveValues()) {
93 3
            $mapStub = file_get_contents(__DIR__ . '/../stubs/map.stub');
94 3
            $this->stub = str_replace('DummyMap', $mapStub, $this->stub);
95 3
            $this->replaceMapPairs();
96
        } else {
97 9
            $this->stub = str_replace('DummyMap', '', $this->stub);
98
        }
99
100 12
        return $this;
101
    }
102
103
    /**
104
     * Determine whether the given enums contain values
105
     *
106
     * @return bool
107
     */
108 12
    private function enumsHaveValues() : bool
109
    {
110 12
        foreach ($this->enums as $enum) {
111 12
            if ($enum->value !== null) {
112 6
                return true;
113
            }
114
        }
115
116 9
        return false;
117
    }
118
119
    /**
120
     * Replace the enums key and value pairs
121
     *
122
     * @return self
123
     */
124 3
    public function replaceMapPairs() : self
125
    {
126 3
        $padding = 12;
127
128
        $pairs = array_map(function (EnumDefinition $enum) use ($padding) {
129 3
            $value = $this->export($enum->value, $padding);
130 3
            return str_repeat(' ', $padding) . "static::{$enum->name} => {$value},";
131 3
        }, $this->enums);
132
133 3
        $this->stub = str_replace('DummyKeyValuePairs', implode(PHP_EOL, $pairs), $this->stub);
134
135 3
        return $this;
136
    }
137
}
138