Test Failed
Push — master ( 50dd93...f69d16 )
by Chris
12:03
created

ProvisionsAssetsTrait::styles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leonidas\Framework\Modules\Traits;
4
5
use Leonidas\Contracts\Ui\Asset\InlineScriptCollectionInterface;
6
use Leonidas\Contracts\Ui\Asset\InlineStyleCollectionInterface;
7
use Leonidas\Contracts\Ui\Asset\ScriptCollectionInterface;
8
use Leonidas\Contracts\Ui\Asset\ScriptLoaderInterface;
9
use Leonidas\Contracts\Ui\Asset\ScriptLocalizationCollectionInterface;
10
use Leonidas\Contracts\Ui\Asset\ScriptPrinterInterface;
11
use Leonidas\Contracts\Ui\Asset\StyleCollectionInterface;
12
use Leonidas\Contracts\Ui\Asset\StyleLoaderInterface;
13
use Leonidas\Contracts\Ui\Asset\StylePrinterInterface;
14
use Leonidas\Library\Core\Asset\ScriptLoader;
15
use Leonidas\Library\Core\Asset\ScriptPrinter;
16
use Leonidas\Library\Core\Asset\StyleLoader;
17
use Leonidas\Library\Core\Asset\StylePrinter;
18
19
trait ProvisionsAssetsTrait
20
{
21
    use AbstractModuleTraitTrait;
22
    use MustBeInitiatedTrait;
23
24
    protected ScriptLoaderInterface $scriptLoader;
25
26
    protected ScriptPrinterInterface $scriptPrinter;
27
28
    protected StyleLoaderInterface $styleLoader;
29
30
    protected StylePrinterInterface $stylePrinter;
31
32
    protected ?ScriptCollectionInterface $scripts = null;
33
34
    protected ?StyleCollectionInterface $styles = null;
35
36
    protected ?InlineScriptCollectionInterface $inlineScripts = null;
37
38
    protected ?InlineStyleCollectionInterface $inlineStyles = null;
39
40
    protected ?ScriptLocalizationCollectionInterface $scriptLocalizations = null;
41
42
    protected function getScriptLoader(): ScriptLoaderInterface
43
    {
44
        return $this->scriptLoader;
45
    }
46
47
    protected function getScriptPrinter(): ScriptPrinterInterface
48
    {
49
        return $this->scriptPrinter;
50
    }
51
52
    protected function getStyleLoader(): StyleLoaderInterface
53
    {
54
        return $this->styleLoader;
55
    }
56
57
    protected function getStylePrinter(): StylePrinterInterface
58
    {
59
        return $this->stylePrinter;
60
    }
61
62
    protected function getScripts(): ?ScriptCollectionInterface
63
    {
64
        return $this->scripts;
65
    }
66
67
    protected function getStyles(): ?StyleCollectionInterface
68
    {
69
        return $this->styles;
70
    }
71
72
    protected function getInlineScripts(): ?InlineScriptCollectionInterface
73
    {
74
        return $this->inlineScripts;
75
    }
76
77
    protected function getInlineStyles(): ?InlineStyleCollectionInterface
78
    {
79
        return $this->inlineStyles;
80
    }
81
82
    protected function getScriptLocalizations(): ?ScriptLocalizationCollectionInterface
83
    {
84
        return $this->scriptLocalizations;
85
    }
86
87
    protected function init()
88
    {
89
        $this->scripts = $this->scripts();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->scripts is correct as $this->scripts() targeting Leonidas\Framework\Modul...sAssetsTrait::scripts() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
90
        $this->inlineScripts = $this->inlineScripts();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->inlineScripts is correct as $this->inlineScripts() targeting Leonidas\Framework\Modul...sTrait::inlineScripts() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
91
        $this->scriptLocalizations = $this->scriptLocalizations();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->scriptLocalizations is correct as $this->scriptLocalizations() targeting Leonidas\Framework\Modul...::scriptLocalizations() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
93
        $this->styles = $this->styles();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->styles is correct as $this->styles() targeting Leonidas\Framework\Modul...nsAssetsTrait::styles() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
94
        $this->inlineStyles = $this->inlineStyles();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $this->inlineStyles is correct as $this->inlineStyles() targeting Leonidas\Framework\Modul...tsTrait::inlineStyles() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
95
96
        $this->scriptLoader = $this->scriptLoader();
97
        $this->styleLoader = $this->styleLoader();
98
99
        $this->scriptPrinter = $this->scriptPrinter();
100
        $this->stylePrinter = $this->stylePrinter();
101
102
        $this->isInitiated = true;
103
    }
104
105
    protected function hasScripts(): bool
106
    {
107
        return ($scripts = $this->getScripts())
108
            && !empty($scripts->getScripts());
109
    }
110
111
    public function hasStyles(): bool
112
    {
113
        return ($styles = $this->getStyles())
114
            && !empty($styles->getStyles());
115
    }
116
117
    protected function hasInlineScripts(): bool
118
    {
119
        return ($inlineScripts = $this->getInlineScripts())
120
            && !empty($inlineScripts->getScripts());
121
    }
122
123
    public function hasInlineStyles(): bool
124
    {
125
        return ($inlineStyles = $this->getInlineStyles())
126
            && !empty($inlineStyles->getStyles());
127
    }
128
129
    public function hasScriptLocalizations(): bool
130
    {
131
        return ($scriptLocalizations = $this->getScriptLocalizations())
132
            && !empty($scriptLocalizations->getLocalizations());
133
    }
134
135
    protected function provisionAssets(?string $hookSuffix = null): void
136
    {
137
        $this->maybeInit();
138
139
        $request = $this->getServerRequest();
140
141
        $scriptLoader = $this->getScriptLoader();
142
        $styleLoader = $this->getStyleLoader();
143
144
        if ($hookSuffix) {
145
            $request = $request->withAttribute('hook_suffix', $hookSuffix);
146
        }
147
148
        if ($this->hasScripts()) {
149
            $scriptLoader->load($this->getScripts(), $request);
150
        }
151
152
        if ($this->hasStyles()) {
153
            $styleLoader->load($this->getStyles(), $request);
154
        }
155
156
        if ($this->hasInlineScripts()) {
157
            $scriptLoader->support($this->getInlineScripts(), $request);
158
        }
159
160
        if ($this->hasInlineStyles()) {
161
            $styleLoader->support($this->getInlineStyles(), $request);
162
        }
163
164
        if ($this->hasScriptLocalizations()) {
165
            $scriptLoader->localize($this->getScriptLocalizations(), $request);
166
        }
167
168
        $scriptLoader->activate(...$this->activateScripts());
169
        $scriptLoader->deactivate(...$this->deactivateScripts());
170
171
        $styleLoader->activate(...$this->activateStyles());
172
        $styleLoader->deactivate(...$this->deactivateStyles());
173
    }
174
175
    protected function filterScriptLoaderTag(string $tag, string $handle, string $src): string
0 ignored issues
show
Unused Code introduced by
The parameter $src is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

175
    protected function filterScriptLoaderTag(string $tag, string $handle, /** @scrutinizer ignore-unused */ string $src): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
176
    {
177
        if (!$this->isInitiated() || !$this->hasScripts() || !$this->getScripts()->hasScript($handle)) {
178
            return $tag;
179
        }
180
181
        return $this->getScriptPrinter()
182
            ->merge($tag, $this->getScripts()->getScript($handle));
183
    }
184
185
    protected function filterStyleLoaderTag(string $tag, string $handle, string $href, string $media): string
0 ignored issues
show
Unused Code introduced by
The parameter $href is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

185
    protected function filterStyleLoaderTag(string $tag, string $handle, /** @scrutinizer ignore-unused */ string $href, string $media): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $media is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

185
    protected function filterStyleLoaderTag(string $tag, string $handle, string $href, /** @scrutinizer ignore-unused */ string $media): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
        if (!$this->isInitiated() || !$this->hasStyles() || !$this->getStyles()->hasStyle($handle)) {
188
            return $tag;
189
        }
190
191
        return $this->getStylePrinter()
192
            ->merge($tag, $this->getStyles()->getStyle($handle));
193
    }
194
195
    protected function asset(?string $asset = null): string
196
    {
197
        return $this->extension->url(
198
            $this->configCascade($this->assetsConfigCascade()) . $asset
0 ignored issues
show
Bug introduced by
It seems like configCascade() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
            $this->/** @scrutinizer ignore-call */ 
199
                   configCascade($this->assetsConfigCascade()) . $asset
Loading history...
199
        );
200
    }
201
202
    protected function assetsConfigCascade(): array
203
    {
204
        return [
205
            'view.assets.path', 'view.assets', 'theme.assets',
206
        ];
207
    }
208
209
    protected function version(?string $version = null): string
210
    {
211
        if ($this->extension->isInDev()) {
212
            return (string) time();
213
        }
214
215
        return $version ?: $this->extension->getVersion();
216
    }
217
218
    protected function scriptLoader(): ScriptLoaderInterface
219
    {
220
        return new ScriptLoader();
221
    }
222
223
    protected function scriptPrinter(): ScriptPrinterInterface
224
    {
225
        return new ScriptPrinter();
226
    }
227
228
    protected function styleLoader(): StyleLoaderInterface
229
    {
230
        return new StyleLoader();
231
    }
232
233
    protected function stylePrinter(): StylePrinterInterface
234
    {
235
        return new StylePrinter();
236
    }
237
238
    protected function scripts(): ?ScriptCollectionInterface
239
    {
240
        return null;
241
    }
242
243
    protected function styles(): ?StyleCollectionInterface
244
    {
245
        return null;
246
    }
247
248
    protected function inlineScripts(): ?InlineScriptCollectionInterface
249
    {
250
        return null;
251
    }
252
253
    protected function inlineStyles(): ?InlineStyleCollectionInterface
254
    {
255
        return null;
256
    }
257
258
    protected function scriptLocalizations(): ?ScriptLocalizationCollectionInterface
259
    {
260
        return null;
261
    }
262
263
    protected function activateScripts(): array
264
    {
265
        return [];
266
    }
267
268
    protected function deactivateScripts(): array
269
    {
270
        return [];
271
    }
272
273
    protected function activateStyles(): array
274
    {
275
        return [];
276
    }
277
278
    protected function deactivateStyles(): array
279
    {
280
        return [];
281
    }
282
}
283