Completed
Push — constructionplanless ( e8d891...d7f83f )
by Dominik
01:36
created

Component::enqueueAssets()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 46

Duplication

Lines 16
Ratio 34.78 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 2
dl 16
loc 46
rs 8.5559
c 0
b 0
f 0
1
<?php
2
3
namespace Flynt\Utils;
4
5
class Component
6
{
7
    public static function enqueueAssets($componentName, array $dependencies = [])
8
    {
9
        // register dependencies
10
        foreach ($dependencies as $dependency) {
11
            // TODO add a warning if the same script is loaded several times (with different names) in multiple components
12
            Asset::register($dependency);
13
        }
14
15
        // collect script dependencies
16
        $scriptDeps = array_reduce($dependencies, function ($list, $dependency) {
17
            if ($dependency['type'] === 'script') {
18
                array_push($list, $dependency['name']);
19
            }
20
            return $list;
21
        }, ['jquery']); // jquery as a default dependency
22
23
        // Enqueue Component Scripts if they exist
24
        $scriptAbsPath = Asset::requirePath("Components/{$componentName}/script.js");
25 View Code Duplication
        if (is_file($scriptAbsPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
            Asset::enqueue([
27
                'type' => 'script',
28
                'name' => "Flynt/Components/{$componentName}",
29
                'path' => "Components/{$componentName}/script.js",
30
                'dependencies' => $scriptDeps
31
            ]);
32
        }
33
34
        // collect style dependencies
35
        $styleDeps = array_reduce($dependencies, function ($list, $dependency) {
36
            if ($dependency['type'] === 'style') {
37
                array_push($list, $dependency['name']);
38
            }
39
            return $list;
40
        }, []);
41
42
        // Enqueue Component Styles if they exist
43
        $styleAbsPath = Asset::requirePath("Components/{$componentName}/style.css");
44 View Code Duplication
        if (is_file($styleAbsPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
            Asset::enqueue([
46
                'type' => 'style',
47
                'name' => "Flynt/Components/{$componentName}",
48
                'path' => "Components/{$componentName}/style.css",
49
                'dependencies' => $styleDeps
50
            ]);
51
        }
52
    }
53
}
54