MoveJsToFooter::execute()   B
last analyzed

Complexity

Conditions 10
Paths 32

Size

Total Lines 66
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 41
c 1
b 0
f 0
nc 32
nop 1
dl 0
loc 66
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright (c) 2019. Volodymyr Hryvinskyi.  All rights reserved.
4
 * @author: <mailto:[email protected]>
5
 * @github: <https://github.com/hryvinskyi>
6
 */
7
8
declare(strict_types=1);
9
10
namespace Hryvinskyi\DeferJs\Model;
11
12
use Hryvinskyi\Base\Helper\ArrayHelper;
13
use Hryvinskyi\Base\Helper\Json;
14
use Hryvinskyi\DeferJs\Helper\Config;
15
use Hryvinskyi\DeferJs\Model\Minify\MinifyJsInterface;
16
use Hryvinskyi\DeferJs\Model\PassesValidator\ValidateSkipper;
17
use Magento\Framework\App\Response\Http;
18
19
/**
20
 * Class MoveJsToFooter
21
 */
22
class MoveJsToFooter implements MoveJsToFooterInterface
23
{
24
    /**
25
     * @var Config
26
     */
27
    private $config;
28
29
    /**
30
     * @var MinifyJsInterface
31
     */
32
    private $minifyJs;
33
34
    /**
35
     * @var ValidateSkipper
36
     */
37
    private $validateSkipper;
38
39
    /**
40
     * MoveJsToFooter constructor.
41
     *
42
     * @param Config $config
43
     * @param MinifyJsInterface $minifyJs
44
     * @param ValidateSkipper $validateSkipper
45
     */
46
    public function __construct(
47
        Config $config,
48
        MinifyJsInterface $minifyJs,
49
        ValidateSkipper $validateSkipper
50
    ) {
51
        $this->config = $config;
52
        $this->minifyJs = $minifyJs;
53
        $this->validateSkipper = $validateSkipper;
54
    }
55
56
    /**
57
     * @param Http $http
58
     *
59
     * @return void
60
     */
61
    public function execute(Http $http)
62
    {
63
        $scripts = [];
64
        $html = $http->getBody();
65
66
        $jsons = [];
67
        $scriptStart = '<script';
68
        $scriptEnd = '</script>';
69
70
        $start = 0;
71
        $i = 0;
72
        while (($start = stripos($html, $scriptStart, $start)) !== false) {
73
            $end = stripos($html, $scriptEnd, $start);
74
75
            if ($end === false) {
76
                break;
77
            }
78
79
            $len = $end + strlen($scriptEnd) - $start;
80
            $script = substr($html, $start, $len);
81
82
            if (
83
                $this->config->isOptimizeXMagentoInitScripts() &&
84
                strpos($script, 'text/x-magento-init') !== false
85
            ) {
86
                $jsons[] = strip_tags($script);
87
                $html = str_replace($script, '', $html);
88
                continue;
89
            }
90
91
            if ($this->validateSkipper->execute($script, $http)) {
92
                $start++;
93
                continue;
94
            }
95
96
            $html = str_replace($script, '', $html);
97
            $scripts[] = $script;
98
99
            $i++;
100
        }
101
102
        if ($this->config->isMinifyBodyScript()) {
103
            $scripts = $this->minifyJs->execute($scripts);
104
        }
105
106
        $merged = [];
107
        foreach ($jsons as $json) {
108
            $json = Json::decode($json);
109
            $merged = ArrayHelper::merge($merged, $json);
110
        }
111
112
        if (count($merged) > 0) {
113
            $merged = '<script type=text/x-magento-init>' . Json::encode($merged) . '</script>';
114
        } else {
115
            $merged = '';
116
        }
117
118
        $scripts = implode('', $scripts);
119
120
        if ($endBody = stripos($html, '</body>')) {
121
            $html = substr($html, 0, $endBody) . $merged . $scripts . substr($html, $endBody);
122
        } else {
123
            $html .= $merged . $scripts;
124
        }
125
126
        $http->setBody($html);
127
    }
128
}