Passed
Push — master ( 0d72e8...44be60 )
by Gabor
04:58
created

TwigExtension::getFilters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
dl 23
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Renderer\ServiceAdapter\Twig;
15
16
use RuntimeException;
17
use Twig_Extension;
18
use Twig_SimpleFilter;
19
use Twig_SimpleFunction;
20
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface;
21
use WebHemi\DependencyInjection\ServiceInterface as DependencyInjectionInterface;
22
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
23
use WebHemi\Renderer\FilterInterface;
24
use WebHemi\Renderer\HelperInterface;
25
26
/**
27
 * Class TwigExtension
28
 *
29
 * @codeCoverageIgnore - Test helpers and filters individually. It's only the shipped solution
30
 *                       to add them to the renderer.
31
 */
32
class TwigExtension extends Twig_Extension
33
{
34
    /** @var DependencyInjectionInterface */
35
    private $dependencyInjectionAdapter;
36
    /** @var ConfigurationInterface */
37
    private $configuration;
38
    /** @var EnvironmentInterface */
39
    private $environmentManager;
40
41
    /**
42
     * TwigExtension constructor.
43
     */
44
    public function __construct()
45
    {
46
        // Oh, this is a disgusting ugly hack...
47
        global $dependencyInjection;
1 ignored issue
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
48
        $this->dependencyInjectionAdapter = $dependencyInjection;
49
        $this->configuration = $this->dependencyInjectionAdapter->get(ConfigurationInterface::class);
50
        $this->environmentManager = $this->dependencyInjectionAdapter->get(EnvironmentInterface::class);
51
    }
52
53
    /**
54
     * Returns extension filters.
55
     *
56
     * @return array<Twig_SimpleFilter>
57
     */
58 View Code Duplication
    public function getFilters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $filters = [];
61
        $filterConfig = $this->getConfig('filter');
62
63
        foreach ($filterConfig as $className) {
64
            /** @var FilterInterface $callable */
65
            $callable = $this->dependencyInjectionAdapter->get($className);
66
            if ($callable instanceof FilterInterface) {
67
                $filters[] = new Twig_SimpleFilter($callable::getName(), $callable);
68
            } else {
69
                throw new RuntimeException(
70
                    sprintf(
71
                        'The class %s cannot be registered as Renderer/Filter!',
72
                        get_class($callable)
73
                    ),
74
                    1000
75
                );
76
            }
77
        }
78
79
        return $filters;
80
    }
81
82
    /**
83
     * Returns extension functions.
84
     *
85
     * @return array<Twig_SimpleFunction>
86
     */
87 View Code Duplication
    public function getFunctions() : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $functions = [];
90
        $helperConfig = $this->getConfig('helper');
91
92
        foreach ($helperConfig as $className) {
93
            /** @var HelperInterface $callable */
94
            $callable = $this->dependencyInjectionAdapter->get($className);
95
            if ($callable instanceof HelperInterface) {
96
                $functions[] = new Twig_SimpleFunction($callable::getName(), $callable);
97
            } else {
98
                throw new RuntimeException(
99
                    sprintf(
100
                        'The class %s cannot be registered as Renderer/Helper!',
101
                        get_class($callable)
102
                    ),
103
                    1001
104
                );
105
            }
106
        }
107
108
        return $functions;
109
    }
110
111
    /**
112
     * Returns the renderer config by type.
113
     *
114
     * @param string $type
115
     * @return array
116
     */
117
    private function getConfig(string $type) : array
118
    {
119
        $module = $this->environmentManager->getSelectedModule();
120
        $config = [];
121
122
        if ($this->configuration->has('renderer/Global/'.$type)) {
123
            $config = $this->configuration->getData('renderer/Global/' . $type);
124
        }
125
126
        if ($this->configuration->has('renderer/'.$module.'/'.$type)) {
127
            $config = merge_array_overwrite(
128
                $config,
129
                $this->configuration->getData('renderer/'.$module.'/'.$type)
130
            );
131
        }
132
133
        return $config;
134
    }
135
}
136