Passed
Pull Request — 2.x (#597)
by Antonio Carlos
06:56
created

Block::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 4
b 0
f 0
nc 1
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace A17\Twill\Services\Blocks;
4
5
use Exception;
6
use Illuminate\Support\Str;
7
8
class Block
9
{
10
    const SOURCE_APP = 'app';
11
12
    const SOURCE_TWILL = 'twill';
13
14
    const SOURCE_CUSTOM = 'custom';
15
16
    const TYPE_BLOCK = 'block';
17
18
    const TYPE_REPEATER = 'repeater';
19
20
    /**
21
     * @var string
22
     */
23
    public $title;
24
25
    /**
26
     * @var string
27
     */
28
    public $trigger;
29
30
    /**
31
     * @var string
32
     */
33
    public $source;
34
35
    /**
36
     * @var string
37
     */
38
    public $name;
39
40
    /**
41
     * @var string
42
     */
43
    public $group;
44
45
    /**
46
     * @var string
47
     */
48
    public $type;
49
50
    /**
51
     * @var string
52
     */
53
    public $icon;
54
55
    /**
56
     * @var string
57
     */
58
    public $component;
59
60
    /**
61
     * @var integer
62
     */
63
    public $max = 999;
64
65
    /**
66
     * @var boolean
67
     */
68
    public $isNewFormat;
69
70
    /**
71
     * @var \Symfony\Component\Finder\SplFileInfo
72
     */
73
    public $file;
74
75
    /**
76
     * @var string
77
     */
78
    public $contents;
79
80
    /**
81
     * Block constructor.
82
     * @param $file
83
     * @param $type
84
     * @param $source
85
     * @throws \Exception
86
     */
87 66
    public function __construct($file, $type, $source)
88
    {
89 66
        $this->file = $file;
90
91 66
        $this->type = $type;
92
93 66
        $this->source = $source;
94
95 66
        $this->parse();
96 66
    }
97
98
    /**
99
     * @param $source
100
     * @return $this
101
     */
102 66
    public function setSource($source)
103
    {
104 66
        $this->source = $source;
105
106 66
        return $this;
107
    }
108
109
    /**
110
     * @return \Illuminate\Support\Collection
111
     */
112 7
    public function toList()
113
    {
114 7
        return collect([
115 7
            'title' => $this->title,
116 7
            'trigger' => $this->trigger,
117 7
            'name' => $this->name,
118 7
            'group' => $this->group,
119 7
            'type' => $this->type,
120 7
            'icon' => $this->icon,
121 7
            'source' => $this->source,
122 7
            'new_format' => $this->isNewFormat ? 'yes' : '-',
123 7
            'file' => $this->file->getFilename(),
124 7
            'component' => $this->component,
125
        ]);
126
    }
127
128
    /**
129
     * @param $name
130
     * @return string
131
     */
132 3
    public function makeName($name)
133
    {
134 3
        return Str::kebab($name);
135
    }
136
137
    /**
138
     * @return $this
139
     * @throws \Exception
140
     */
141 66
    public function parse()
142
    {
143 66
        $contents = file_get_contents((string) $this->file->getPathName());
144
145 66
        $this->name = $name = Str::before(
146 66
            $this->file->getFilename(),
147 66
            '.blade.php'
148
        );
149 66
        $this->title = $this->parseProperty('title', $contents, $name);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseProperty('title', $contents, $name) of type array is incompatible with the declared type string of property $title.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
150 66
        $this->trigger = $this->parseProperty('trigger', $contents, $name);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseProperty('trigger', $contents, $name) of type array is incompatible with the declared type string of property $trigger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
151 66
        $this->max = (int) $this->parseProperty('max', $contents, $name, 999);
152 66
        $this->group = $this->parseProperty('group', $contents, $name);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseProperty('group', $contents, $name) of type array is incompatible with the declared type string of property $group.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
153 66
        $this->icon = $this->parseProperty('icon', $contents, $name);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseProperty('icon', $contents, $name) of type array is incompatible with the declared type string of property $icon.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
154 66
        $this->isNewFormat = $this->isNewFormat($contents);
155 66
        $this->contents = $contents;
156 66
        $this->component = "a17-block-{$this->name}";
157
158 66
        return $this;
159
    }
160
161
    /**
162
     * @param $property
163
     * @param $block
164
     * @param $blockName
165
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
166
     * @return array
167
     * @throws \Exception
168
     */
169 66
    public function parseProperty(
170
        $property,
171
        $block,
172
        $blockName,
173
        $default = null
174
    ) {
175 66
        preg_match("/@a17-{$property}\('(.*)'\)/", $block, $matches);
176
177 66
        if (filled($matches)) {
178 66
            return $matches[1];
179
        }
180
181
        if (
182 66
            $value = config(
183 66
                "twill.block_editor.blocks.{$blockName}.{$property}"
184
            )
185
        ) {
186
            return $value;
187
        }
188
189
        if (
190 66
            $value = config(
191 66
                "twill.block_editor.repeaters.{$blockName}.{$property}"
192
            )
193
        ) {
194
            return $value;
195
        }
196
197 66
        if ($property !== 'title') {
198 66
            return $default;
199
        }
200
201
        // Title is mandatory
202
        throw new Exception(
203
            "Property '{$property}' not found on block {$blockName}."
204
        );
205
    }
206
207
    /**
208
     * @param $block
209
     * @return bool
210
     */
211 66
    public function isNewFormat($block)
212
    {
213 66
        preg_match("/@a17-.*\('(.*)'\)/", $block, $matches);
214
215 66
        return filled($matches);
216
    }
217
218
    /**
219
     * @return string
220
     */
221 66
    public function getFileName()
222
    {
223 66
        return $this->file->getFileName();
224
    }
225
226
    /**
227
     * @return string
228
     * @throws \Symfony\Component\Debug\Exception\FatalThrowableError
229
     */
230 4
    public function render()
231
    {
232 4
        return BladeCompiler::render(
233 4
            $this->removeSpecialBladeTags($this->contents),
234
            [
235 4
                'renderForBlocks' => true,
236
            ]
237
        );
238
    }
239
240
    /**
241
     * @param $contents
242
     * @return string
243
     */
244 4
    public function removeSpecialBladeTags($contents)
245
    {
246 4
        return preg_replace("/@a17-.*\('(.*)'\)/", '', $contents);
247
    }
248
}
249