Completed
Push — master ( 05ab3c...1a19c2 )
by David
02:03
created

TagManagerVariables::fillValues()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 29
rs 8.5806
cc 4
eloc 17
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\ModelManager;
20
use WbmTagManager\Models\Repository;
21
22
/**
23
 * Class TagManagerVariables
24
 */
25
class TagManagerVariables implements TagManagerVariablesInterface
26
{
27
    /**
28
     * @var \Enlight_Template_Manager
29
     */
30
    private $template;
31
    /**
32
     * @var ModelManager
33
     */
34
    private $em;
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 ModelManager              $em
50
     * @param \Enlight_Template_Manager $template
51
     */
52
    public function __construct(
53
        ModelManager $em,
54
        \Enlight_Template_Manager $template
55
    ) {
56
        $this->template = $template;
57
        $this->em = $em;
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
        /** @var Repository $propertyRepo */
98
        $propertyRepo = $this->em->getRepository('WbmTagManager\Models\Property');
99
        $dataLayer = $propertyRepo->getChildrenList(0, $module, true);
100
101
        if (!empty($dataLayer) && !empty($this->getViewVariables())) {
102
            $dataLayer = $this->fillValues($dataLayer);
103
104
            array_walk_recursive($dataLayer, [$this, 'castArrayValues']);
105
106
            $this->setVariables($dataLayer);
107
        }
108
    }
109
110
    /**
111
     * @param array $dataLayer
112
     *
113
     * @return mixed
114
     */
115
    public function fillValues($dataLayer)
116
    {
117
        $dataLayer = json_encode($dataLayer);
118
119
        $search = ['{\/', ',{"endArrayOf":true}'];
120
        $replace = ['{/', '{/literal}{if !$smarty.foreach.loop.last},{/if}{/foreach}{literal}'];
121
122
        $dataLayer = str_replace($search, $replace, $dataLayer);
123
124
        while (preg_match('/({"startArrayOf":".*?"},)/i', $dataLayer, $matches)) {
125
            foreach ($matches as $match) {
126
                $foreachObj = json_decode(rtrim($match, ','));
127
                if ($foreachObj->startArrayOf) {
128
                    $arguments = explode(' as ', $foreachObj->startArrayOf);
129
                    $dataLayer = str_replace(
130
                        $match,
131
                        '{/literal}{foreach from=' . $arguments[0] . ' item=' . ltrim($arguments[1], '$') . ' name=loop}{literal}',
132
                        $dataLayer
133
                    );
134
                }
135
            }
136
        }
137
138
        $dataLayer = '{literal}' . $dataLayer . '{/literal}';
139
140
        $dataLayer = $this->compileString($dataLayer);
141
142
        return json_decode($dataLayer, true);
143
    }
144
145
    /**
146
     * @param $source
147
     * @param bool $prettyPrint
148
     *
149
     * @return string
150
     */
151
    public function prependDataLayer($source, $prettyPrint = false)
152
    {
153
        return '<script>window.dataLayer.push(' .
154
            json_encode(
155
                $this->getVariables(),
156
                ($prettyPrint) ? JSON_PRETTY_PRINT : null
157
            ) .
158
            ');</script>' .
159
            $source;
160
    }
161
162
    /**
163
     * @param $value
164
     */
165
    private function castArrayValues(&$value)
166
    {
167
        switch (true) {
168
            case is_array(json_decode($value)):
169
            case is_int(json_decode($value)):
170
            case is_float(json_decode($value)):
171
                $value = json_decode($value);
172
        }
173
    }
174
175
    /**
176
     * @param $string
177
     *
178
     * @return string
179
     */
180
    private function compileString($string)
181
    {
182
        $view = new \Enlight_View_Default(
183
            $this->template
184
        );
185
186
        $compiler = new \Shopware_Components_StringCompiler($view->Engine());
187
188
        $compiler->setContext($this->viewVariables);
189
190
        return $compiler->compileString($string);
191
    }
192
}
193