AssetList::__invoke()   B
last analyzed

Complexity

Conditions 9
Paths 15

Size

Total Lines 48

Duplication

Lines 16
Ratio 33.33 %

Importance

Changes 0
Metric Value
dl 16
loc 48
rs 7.5789
c 0
b 0
f 0
cc 9
nc 15
nop 2
1
<?php
2
3
namespace Faulancer\View\Helper;
4
5
use Faulancer\Exception\ServiceNotFoundException;
6
use Faulancer\Service\Config;
7
use Faulancer\View\AbstractViewHelper;
8
9
/**
10
 * Class AssetList
11
 *
12
 * @category ViewHelper
13
 * @package  Faulancer\View\Helper
14
 * @author   Florian Knapp <[email protected]>
15
 * @license  MIT License
16
 * @link     No link provided
17
 */
18
class AssetList extends AbstractViewHelper
19
{
20
21
    /**
22
     * Render a asset list by type
23
     *
24
     * @param string         $type     The asset type
25
     * @param bool           $optimize If all assets should be concatenated
26
     *                                 within style tag in head
27
     *
28
     * @return string
29
     *
30
     * @throws ServiceNotFoundException
31
     */
32
    public function __invoke($type, $optimize = false)
33
    {
34
        $result  = '';
35
        $pattern = '';
36
37
        switch ($type) {
38
39
        case 'js':
40
            $pattern = '<script src="%s"></script>';
41
            break;
42
43
        case 'css':
44
            $pattern = '<link rel="stylesheet" type="text/css" href="%s">';
45
            break;
46
47
        }
48
49
        /** @var array $files */
50
        $files = $this->view->getVariable('assets' . ucfirst($type));
51
52
        if (empty($files)) {
53
            return '';
54
        }
55
56 View Code Duplication
        if ($type === 'css' && $optimize) {
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...
57
58
            $result  = '<style type="text/css">';
59
            $result .= $this->_collectAssetsContent($files, $type);
60
            $result .= '</style>';
61
            return $result;
62
63
        }
64
65 View Code Duplication
        if ($type === 'js' && $optimize) {
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...
66
67
            $result  = '<script>';
68
            $result .= $this->_collectAssetsContent($files, $type);
69
            $result .= '</script>';
70
            return $result;
71
72
        }
73
74
        foreach ($files AS $file) {
75
            $result .= sprintf($pattern, $file) . "\n";
76
        }
77
78
        return $result;
79
    }
80
81
    /**
82
     * Collect all assets content for concatenation
83
     *
84
     * @param array  $files The asset files
85
     * @param string $type  The assets type
86
     *
87
     * @return string
88
     */
89
    private function _collectAssetsContent(array $files, string $type)
90
    {
91
        /** @var Config $config */
92
        $config  = $this->getServiceLocator()->get(Config::class);
93
        $docRoot = realpath($config->get('projectRoot') . '/public');
94
95
        $content  = '';
96
        $contents = [];
97
98
        foreach ($files as $file) {
99
100
            if (file_exists($docRoot . $file)) {
101
                $contents[] = file_get_contents($docRoot . $file);
102
            }
103
104
        }
105
106
        if ($type === 'css') {
107
108
            $content = str_replace(
109
                ["\n", "\t", "  ", ": ", " {", "{ ", " }", ";}"],
110
                ["", "", "", ":", "{", "{", "}", "}"],
111
                implode('', $contents)
112
            );
113
114
        }
115
116
        return $content;
117
    }
118
119
}