ScriptLoader::support()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\Core\Asset;
4
5
use Leonidas\Contracts\Ui\Asset\InlineScriptCollectionInterface;
6
use Leonidas\Contracts\Ui\Asset\InlineScriptInterface;
7
use Leonidas\Contracts\Ui\Asset\ScriptCollectionInterface;
8
use Leonidas\Contracts\Ui\Asset\ScriptInterface;
9
use Leonidas\Contracts\Ui\Asset\ScriptLoaderInterface;
10
use Leonidas\Contracts\Ui\Asset\ScriptLocalizationCollectionInterface;
11
use Leonidas\Contracts\Ui\Asset\ScriptLocalizationInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
14
class ScriptLoader implements ScriptLoaderInterface
15
{
16
    public function load(ScriptCollectionInterface $scripts, ServerRequestInterface $request)
17
    {
18
        foreach ($scripts->getScripts() as $script) {
19
            if ($script->shouldBeLoaded($request)) {
20
                if ($script->shouldBeEnqueued()) {
21
                    $this->enqueueScript($script);
22
                } else {
23
                    $this->registerScript($script);
24
                }
25
            }
26
        }
27
    }
28
29
    public function support(InlineScriptCollectionInterface $scripts, ServerRequestInterface $request)
30
    {
31
        foreach ($scripts->getScripts() as $script) {
32
            if ($script->shouldBeLoaded($request)) {
33
                $this->addInlineScript($script);
34
            }
35
        }
36
    }
37
38
    public function localize(ScriptLocalizationCollectionInterface $localizations, ServerRequestInterface $request)
39
    {
40
        foreach ($localizations->getLocalizations() as $localization) {
41
            if ($localization->shouldBeLoaded($request)) {
42
                $this->localizeScript($localization);
43
            }
44
        }
45
    }
46
47
    public function activate(string ...$scripts)
48
    {
49
        foreach ($scripts as $script) {
50
            wp_enqueue_script($script);
0 ignored issues
show
Bug introduced by
The function wp_enqueue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

50
            /** @scrutinizer ignore-call */ 
51
            wp_enqueue_script($script);
Loading history...
51
        }
52
    }
53
54
    public function deactivate(string ...$scripts)
55
    {
56
        foreach ($scripts as $script) {
57
            wp_dequeue_script($script);
0 ignored issues
show
Bug introduced by
The function wp_dequeue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

57
            /** @scrutinizer ignore-call */ 
58
            wp_dequeue_script($script);
Loading history...
58
        }
59
    }
60
61
    protected function registerScript(ScriptInterface $script)
62
    {
63
        wp_register_script(
0 ignored issues
show
Bug introduced by
The function wp_register_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

63
        /** @scrutinizer ignore-call */ 
64
        wp_register_script(
Loading history...
64
            $script->getHandle(),
65
            $script->getSrc(),
66
            $script->getDependencies(),
67
            $script->getVersion(),
68
            $script->shouldLoadInFooter()
69
        );
70
71
        // $this->loadScriptAddons($script);
72
    }
73
74
    protected function enqueueScript(ScriptInterface $script)
75
    {
76
        wp_enqueue_script(
0 ignored issues
show
Bug introduced by
The function wp_enqueue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

76
        /** @scrutinizer ignore-call */ 
77
        wp_enqueue_script(
Loading history...
77
            $script->getHandle(),
78
            $script->getSrc(),
79
            $script->getDependencies(),
80
            $script->getVersion(),
81
            $script->shouldLoadInFooter()
82
        );
83
84
        // $this->loadScriptAddons($script);
85
    }
86
87
    protected function addInlineScript(InlineScriptInterface $script)
88
    {
89
        wp_add_inline_script(
0 ignored issues
show
Bug introduced by
The function wp_add_inline_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

89
        /** @scrutinizer ignore-call */ 
90
        wp_add_inline_script(
Loading history...
90
            $script->getHandle(),
91
            $script->getCode(),
92
            $script->getPosition()
93
        );
94
    }
95
96
    protected function localizeScript(ScriptLocalizationInterface $localization)
97
    {
98
        wp_localize_script(
0 ignored issues
show
Bug introduced by
The function wp_localize_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

98
        /** @scrutinizer ignore-call */ 
99
        wp_localize_script(
Loading history...
99
            $localization->getHandle(),
100
            $localization->getVariable(),
101
            $localization->getData()
102
        );
103
    }
104
105
    // protected function loadScriptAddons(ScriptInterface $script)
106
    // {
107
    //     if ($script->hasLocalizations()) {
108
    //         foreach ($script->getLocalizations()->getLocalizations() as $localization) {
109
    //             $this->localizeScript($localization);
110
    //         }
111
    //     }
112
113
    //     if ($script->hasInlineSupport()) {
114
    //         foreach ($script->getInlineSupport()->getScripts() as $inlineScript) {
115
    //             $this->addInlineScript($inlineScript);
116
    //         }
117
    //     }
118
    // }
119
}
120