Passed
Push — master ( 843207...29f725 )
by mark
36:53 queued 19:40
created

AbstractTwigChartPlugin   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getChartContext() 0 13 2
A getChartFunctions() 0 7 1
A getChartPluginByName() 0 5 1
A getDefaultTwigOptions() 0 5 1
A renderChart() 0 6 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 SprykerShop\Yves\ChartWidget\Plugin\Twig;
9
10
use Spryker\Shared\Chart\Dependency\Plugin\ChartLayoutablePluginInterface;
1 ignored issue
show
Bug introduced by
The type Spryker\Shared\Chart\Dep...youtablePluginInterface 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...
11
use Spryker\Shared\Chart\Dependency\Plugin\ChartPluginInterface;
1 ignored issue
show
Bug introduced by
The type Spryker\Shared\Chart\Dep...in\ChartPluginInterface 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...
12
use Spryker\Shared\Chart\Dependency\Plugin\TwigChartFunctionPluginInterface;
1 ignored issue
show
Bug introduced by
The type Spryker\Shared\Chart\Dep...FunctionPluginInterface 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...
13
use Spryker\Yves\Kernel\AbstractPlugin;
14
use Twig_Environment;
1 ignored issue
show
Bug introduced by
The type Twig_Environment 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...
15
use Twig_SimpleFunction;
1 ignored issue
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...
16
17
/**
18
 * @method \SprykerShop\Yves\ChartWidget\ChartWidgetFactory getFactory()
19
 * @method \SprykerShop\Yves\ChartWidget\ChartWidgetConfig getConfig()
20
 */
21
abstract class AbstractTwigChartPlugin extends AbstractPlugin implements TwigChartFunctionPluginInterface
22
{
23
    public const TWIG_FUNCTION_NAME = 'chart';
24
25
    /**
26
     * @return string
27
     */
28
    public function getName(): string
29
    {
30
        return static::TWIG_FUNCTION_NAME;
31
    }
32
33
    /**
34
     * @return \Twig_SimpleFunction[]
35
     */
36
    public function getChartFunctions(): array
37
    {
38
        return [
39
            new Twig_SimpleFunction(
40
                static::TWIG_FUNCTION_NAME,
41
                [$this, 'renderChart'],
42
                $this->getDefaultTwigOptions()
43
            ),
44
        ];
45
    }
46
47
    /**
48
     * @param \Twig_Environment $twig
49
     * @param string $chartPluginName
50
     * @param string|null $dataIdentifier
51
     *
52
     * @return string
53
     */
54
    public function renderChart(Twig_Environment $twig, $chartPluginName, $dataIdentifier = null): string
55
    {
56
        $context = $this->getChartContext($chartPluginName, $dataIdentifier);
57
        $rendered = $twig->render($this->getTemplateName(), $context);
58
59
        return $rendered;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    abstract protected function getTemplateName(): string;
66
67
    /**
68
     * @param string $chartPluginName
69
     * @param string|null $dataIdentifier
70
     *
71
     * @return array
72
     */
73
    protected function getChartContext($chartPluginName, $dataIdentifier): array
74
    {
75
        $chartPlugin = $this->getChartPluginByName($chartPluginName);
76
77
        $context = [
78
            'data' => $chartPlugin->getChartData($dataIdentifier),
79
            'layout' => $this->getConfig()->getDefaultChartLayout(),
80
        ];
81
        if ($chartPlugin instanceof ChartLayoutablePluginInterface) {
82
            $context['layout'] = $chartPlugin->getChartLayout();
83
        }
84
85
        return $context;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    protected function getDefaultTwigOptions(): array
92
    {
93
        return [
94
            'is_safe' => ['html'],
95
            'needs_environment' => true,
96
        ];
97
    }
98
99
    /**
100
     * @param string $pluginName
101
     *
102
     * @return \Spryker\Shared\Chart\Dependency\Plugin\ChartPluginInterface
103
     */
104
    protected function getChartPluginByName($pluginName): ChartPluginInterface
105
    {
106
        return $this->getFactory()
107
            ->createChartPluginCollection()
108
            ->getChartPluginByName($pluginName);
109
    }
110
}
111