ShopUiCompatibilityTwigExtension   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 230
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
eloc 92
c 3
b 1
f 1
dl 0
loc 230
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getQaAttribute() 0 19 5
A getTemplateTemplate() 0 3 1
A getFilters() 0 3 1
A getTokenParsers() 0 4 1
A getViewTemplate() 0 3 1
A getPublicFolderPath() 0 3 1
A getComponentTemplate() 0 3 1
A getGlobals() 0 4 1
B getFunctions() 0 104 1
A getModelTemplate() 0 3 1
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Yves\ShopUiCompatibility\Twig;
9
10
use Spryker\Shared\Twig\TwigExtension;
11
use SprykerEco\Yves\ShopUiCompatibility\Twig\Node\ShopUiCompatibilityDefineTwigNode;
12
use SprykerEco\Yves\ShopUiCompatibility\Twig\TokenParser\ShopUiCompatibilityDefineTwigTokenParser;
13
use Twig_SimpleFunction;
0 ignored issues
show
Bug introduced by
The type Twig_SimpleFunction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class ShopUiCompatibilityTwigExtension extends TwigExtension
16
{
17
    public const FUNCTION_GET_PUBLIC_FOLDER_PATH = 'publicPath';
18
    public const FUNCTION_GET_QA_ATTRIBUTE = 'qa';
19
    public const FUNCTION_GET_QA_ATTRIBUTE_SUB = 'qa_*';
20
21
    public const FUNCTION_GET_UI_MODEL_COMPONENT_TEMPLATE = 'model';
22
    public const FUNCTION_GET_UI_ATOM_COMPONENT_TEMPLATE = 'atom';
23
    public const FUNCTION_GET_UI_MOLECULE_COMPONENT_TEMPLATE = 'molecule';
24
    public const FUNCTION_GET_UI_ORGANISM_COMPONENT_TEMPLATE = 'organism';
25
    public const FUNCTION_GET_UI_TEMPLATE_COMPONENT_TEMPLATE = 'template';
26
    public const FUNCTION_GET_UI_VIEW_COMPONENT_TEMPLATE = 'view';
27
    public const DEFAULT_MODULE = 'ShopUi';
28
29
    /**
30
     * @return string[]
31
     */
32
    public function getGlobals(): array
33
    {
34
        return [
35
            'required' => ShopUiCompatibilityDefineTwigNode::REQUIRED_VALUE,
36
        ];
37
    }
38
39
    /**
40
     * @return \Twig_SimpleFilter[]
41
     */
42
    public function getFilters(): array
43
    {
44
        return [];
45
    }
46
47
    /**
48
     * @return \Twig_SimpleFunction[]
49
     */
50
    public function getFunctions(): array
51
    {
52
        return [
53
            new Twig_SimpleFunction(self::FUNCTION_GET_PUBLIC_FOLDER_PATH, function ($relativePath) {
54
                $publicFolderPath = $this->getPublicFolderPath();
55
                return $publicFolderPath . $relativePath;
56
            }, [
57
                $this,
58
                self::FUNCTION_GET_PUBLIC_FOLDER_PATH,
59
            ]),
60
61
            new Twig_SimpleFunction(self::FUNCTION_GET_QA_ATTRIBUTE, function (array $qaValues = []) {
62
                return $this->getQaAttribute($qaValues);
63
            }, [
64
                $this,
65
                self::FUNCTION_GET_QA_ATTRIBUTE,
66
                'is_safe' => ['html'],
67
                'is_variadic' => true,
68
            ]),
69
70
            new Twig_SimpleFunction(self::FUNCTION_GET_QA_ATTRIBUTE_SUB, function ($qaName, array $qaValues = []) {
71
                return $this->getQaAttribute($qaValues, $qaName);
72
            }, [
73
                $this,
74
                self::FUNCTION_GET_QA_ATTRIBUTE_SUB,
75
                'is_safe' => ['html'],
76
                'is_variadic' => true,
77
            ]),
78
79
            new Twig_SimpleFunction(self::FUNCTION_GET_UI_MODEL_COMPONENT_TEMPLATE, function ($modelName) {
80
                return $this->getModelTemplate($modelName);
81
            }, [
82
                $this,
83
                self::FUNCTION_GET_UI_MODEL_COMPONENT_TEMPLATE,
84
            ]),
85
86
            new Twig_SimpleFunction(self::FUNCTION_GET_UI_ATOM_COMPONENT_TEMPLATE, function ($componentName, $componentModule = self::DEFAULT_MODULE) {
87
                return $this->getComponentTemplate($componentModule, 'atoms', $componentName);
88
            }, [
89
                $this,
90
                self::FUNCTION_GET_UI_ATOM_COMPONENT_TEMPLATE,
91
            ]),
92
93
            new Twig_SimpleFunction(self::FUNCTION_GET_UI_MOLECULE_COMPONENT_TEMPLATE, function ($componentName, $componentModule = self::DEFAULT_MODULE) {
94
                return $this->getComponentTemplate($componentModule, 'molecules', $componentName);
95
            }, [
96
                $this,
97
                self::FUNCTION_GET_UI_MOLECULE_COMPONENT_TEMPLATE,
98
            ]),
99
100
            new Twig_SimpleFunction(self::FUNCTION_GET_UI_ORGANISM_COMPONENT_TEMPLATE, function ($componentName, $componentModule = self::DEFAULT_MODULE) {
101
                return $this->getComponentTemplate($componentModule, 'organisms', $componentName);
102
            }, [
103
                $this,
104
                self::FUNCTION_GET_UI_ORGANISM_COMPONENT_TEMPLATE,
105
            ]),
106
107
            new Twig_SimpleFunction(self::FUNCTION_GET_UI_TEMPLATE_COMPONENT_TEMPLATE, function ($templateName, $templateModule = self::DEFAULT_MODULE) {
108
                return $this->getTemplateTemplate($templateModule, $templateName);
109
            }, [
110
                $this,
111
                self::FUNCTION_GET_UI_TEMPLATE_COMPONENT_TEMPLATE,
112
            ]),
113
114
            new Twig_SimpleFunction(self::FUNCTION_GET_UI_VIEW_COMPONENT_TEMPLATE, function ($viewName, $viewModule = self::DEFAULT_MODULE) {
115
                return $this->getViewTemplate($viewModule, $viewName);
116
            }, [
117
                $this,
118
                self::FUNCTION_GET_UI_VIEW_COMPONENT_TEMPLATE,
119
            ]),
120
121
            new Twig_SimpleFunction('widget', function () {
122
                return null;
123
            }, [
124
                $this,
125
                'widget',
126
            ]),
127
128
            new Twig_SimpleFunction('widgetBlock', function () {
129
                return null;
130
            }, [
131
                $this,
132
                'widgetBlock',
133
            ]),
134
135
            new Twig_SimpleFunction('widgetGlobal', function () {
136
                return null;
137
            }, [
138
                $this,
139
                'widgetGlobal',
140
            ]),
141
142
            new Twig_SimpleFunction('widgetExists', function () {
143
                return false;
144
            }, [
145
                $this,
146
                'widgetExists',
147
            ]),
148
149
            new Twig_SimpleFunction('widgetGlobalExists', function () {
150
                return false;
151
            }, [
152
                $this,
153
                'widgetGlobalExists',
154
            ]),
155
        ];
156
    }
157
158
    /**
159
     * @return \Twig_TokenParser[]
160
     */
161
    public function getTokenParsers(): array
162
    {
163
        return [
164
            new ShopUiCompatibilityDefineTwigTokenParser(),
165
        ];
166
    }
167
168
    /**
169
     * @return string
170
     */
171
    protected function getPublicFolderPath(): string
172
    {
173
        return '/assets/';
174
    }
175
176
    /**
177
     * @param array $qaValues
178
     * @param string|null $qaName
179
     *
180
     * @return string
181
     */
182
    protected function getQaAttribute(array $qaValues = [], ?string $qaName = null): string
183
    {
184
        $value = '';
185
186
        if (empty($qaValues)) {
187
            return '';
188
        }
189
190
        foreach ($qaValues as $qaValue) {
191
            if (!empty($qaValue)) {
192
                $value .= $qaValue . ' ';
193
            }
194
        }
195
196
        if (empty($qaName)) {
197
            return 'data-qa="' . trim($value) . '"';
198
        }
199
200
        return 'data-qa-' . $qaName . '="' . trim($value) . '"';
201
    }
202
203
    /**
204
     * @param string $modelName
205
     *
206
     * @return string
207
     */
208
    protected function getModelTemplate(string $modelName): string
209
    {
210
        return '@ShopUi/models/' . $modelName . '.twig';
211
    }
212
213
    /**
214
     * @param string $componentModule
215
     * @param string $componentType
216
     * @param string $componentName
217
     *
218
     * @return string
219
     */
220
    protected function getComponentTemplate(string $componentModule, string $componentType, string $componentName): string
221
    {
222
        return '@' . $componentModule . '/components/' . $componentType . '/' . $componentName . '/' . $componentName . '.twig';
223
    }
224
225
    /**
226
     * @param string $templateModule
227
     * @param string $templateName
228
     *
229
     * @return string
230
     */
231
    protected function getTemplateTemplate(string $templateModule, string $templateName): string
232
    {
233
        return '@' . $templateModule . '/templates/' . $templateName . '/' . $templateName . '.twig';
234
    }
235
236
    /**
237
     * @param string $viewModule
238
     * @param string $viewName
239
     *
240
     * @return string
241
     */
242
    protected function getViewTemplate(string $viewModule, string $viewName): string
243
    {
244
        return '@' . $viewModule . '/views/' . $viewName . '/' . $viewName . '.twig';
245
    }
246
}
247