Completed
Push — master ( ab2f9c...c343b6 )
by David
01:57
created

TagManagerVariables::fillValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
/**
3
 * Tag Manager
4
 * Copyright (c) Webmatch GmbH
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 */
16
17
namespace WbmTagManager\Services;
18
19
use Shopware\Components\Model\ModelRepository;
20
21
/**
22
 * Class TagManagerVariables
23
 */
24
class TagManagerVariables implements TagManagerVariablesInterface
25
{
26
    /**
27
     * @var \WbmTagManager\Models\Repository
28
     */
29
    private $propertyRepository;
30
31
    /**
32
     * @var \Enlight_Template_Manager
33
     */
34
    private $template;
35
36
    /**
37
     * @var array
38
     */
39
    private $viewVariables = [];
40
41
    /**
42
     * @var mixed
43
     */
44
    private $variables = null;
45
46
    /**
47
     * TagManagerVariables constructor.
48
     *
49
     * @param ModelRepository           $propertyRepository
50
     * @param \Enlight_Template_Manager $template
51
     */
52
    public function __construct(
53
        ModelRepository $propertyRepository,
54
        \Enlight_Template_Manager $template
55
    ) {
56
        $this->propertyRepository = $propertyRepository;
0 ignored issues
show
Documentation Bug introduced by
$propertyRepository is of type object<Shopware\Components\Model\ModelRepository>, but the property $propertyRepository was declared to be of type object<WbmTagManager\Models\Repository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
57
        $this->template = $template;
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function getViewVariables()
64
    {
65
        return $this->viewVariables;
66
    }
67
68
    /**
69
     * @param array $viewVariables
70
     */
71
    public function setViewVariables($viewVariables)
72
    {
73
        $this->viewVariables = $viewVariables;
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79
    public function getVariables()
80
    {
81
        return $this->variables;
82
    }
83
84
    /**
85
     * @param mixed $variables
86
     */
87
    public function setVariables($variables)
88
    {
89
        $this->variables = $variables;
90
    }
91
92
    /**
93
     * @param string $module
94
     */
95
    public function render($module)
96
    {
97
        $dataLayer = $this->propertyRepository->getChildrenList(0, $module, true);
98
99
        if (!empty($dataLayer) && !empty($this->getViewVariables())) {
100
            $dataLayer = $this->fillValues($dataLayer);
101
102
            array_walk_recursive($dataLayer, [$this, 'castArrayValues']);
103
104
            $this->setVariables($dataLayer);
105
        }
106
    }
107
108
    /**
109
     * @param array $dataLayer
110
     *
111
     * @return mixed
112
     */
113
    public function fillValues($dataLayer)
114
    {
115
        $dataLayer = json_encode($dataLayer);
116
117
        $search = ['{\/', ',{"endArrayOf":true}'];
118
        $replace = ['{/', '{/literal}{if !$smarty.foreach.loop.last},{/if}{/foreach}{literal}'];
119
120
        $dataLayer = str_replace($search, $replace, $dataLayer);
121
122
        while (preg_match('/({"startArrayOf":".*?"},)/i', $dataLayer, $matches)) {
123
            foreach ($matches as $match) {
124
                $foreachObj = json_decode(rtrim($match, ','));
125
                if ($foreachObj->startArrayOf) {
126
                    $arguments = explode(' as ', $foreachObj->startArrayOf);
127
                    $dataLayer = str_replace(
128
                        $match,
129
                        '{/literal}{foreach from=' . $arguments[0] . ' item=' . ltrim($arguments[1], '$') . ' name=loop}{literal}',
130
                        $dataLayer
131
                    );
132
                }
133
            }
134
        }
135
136
        $dataLayer = '{literal}' . $dataLayer . '{/literal}';
137
138
        $dataLayer = $this->compileString($dataLayer);
139
140
        return json_decode($dataLayer, true);
141
    }
142
143
    /**
144
     * @param $source
145
     * @param bool $prettyPrint
146
     *
147
     * @return string
148
     */
149
    public function prependDataLayer($source, $prettyPrint = false)
150
    {
151
        return sprintf(
152
            '%s%s%s%s',
153
            '<script>',
154
            sprintf(
155
                'window.dataLayer.push(%s);',
156
                json_encode(
157
                    $this->getVariables(),
158
                    ($prettyPrint) ? JSON_PRETTY_PRINT : null
159
                )
160
            ),
161
            '</script>',
162
            $source
163
        );
164
    }
165
166
    /**
167
     * @param $value
168
     */
169
    private function castArrayValues(&$value)
170
    {
171
        switch (true) {
172
            case is_array(json_decode($value)):
173
            case is_int(json_decode($value)):
174
            case is_float(json_decode($value)):
175
                $value = json_decode($value);
176
        }
177
    }
178
179
    /**
180
     * @param $string
181
     *
182
     * @return string
183
     */
184
    private function compileString($string)
185
    {
186
        $view = new \Enlight_View_Default(
187
            $this->template
188
        );
189
190
        $compiler = new \Shopware_Components_StringCompiler($view->Engine());
191
192
        $compiler->setContext($this->getViewVariables());
193
194
        return $compiler->compileString($string);
195
    }
196
}
197