Completed
Push — ezp-31088-refactor-content-mod... ( ab3ba3...3726c8 )
by
unknown
14:09
created

TwigVariableProviderExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 26 2
A hasParameterProvider() 0 5 2
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\ExpressionLanguage;
10
11
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
12
use eZ\Publish\Core\MVC\Symfony\View\ContentView;
13
use eZ\Publish\Core\MVC\Symfony\View\VariableProviderRegistry;
14
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
15
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
16
17
final class TwigVariableProviderExtension implements ExpressionFunctionProviderInterface
18
{
19
    public const PROVIDER_REGISTRY_PARAMETER = 'providerRegistry';
20
    public const VIEW_PARAMETER = 'view';
21
22
    /**
23
     * @return \Symfony\Component\ExpressionLanguage\ExpressionFunction[]
24
     */
25
    public function getFunctions(): array
26
    {
27
        return [
28
            new ExpressionFunction(
29
                'twig_variable_provider',
30
                function (string $identifier) {
0 ignored issues
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
                    return 'Not implemented: Not a Dependency Injection expression';
32
                },
33
                function (array $variables, string $identifier) {
34
                    if (!$this->hasParameterProvider($variables)) {
35
                        throw new InvalidArgumentException(
36
                            self::PROVIDER_REGISTRY_PARAMETER,
37
                            'Expression parameter is not a valid type of ' . VariableProviderRegistry::class
38
                        );
39
                    }
40
41
                    $view = $variables[self::VIEW_PARAMETER] ?? new ContentView();
42
                    $providerRegistry = $variables[self::PROVIDER_REGISTRY_PARAMETER];
43
44
                    $provider = $providerRegistry->getTwigVariableProvider($identifier);
45
46
                    return $provider->getTwigVariables($view, $variables);
47
                }
48
            ),
49
        ];
50
    }
51
52
    private function hasParameterProvider(array $variables): bool
53
    {
54
        return !empty($variables[self::PROVIDER_REGISTRY_PARAMETER])
55
            && $variables[self::PROVIDER_REGISTRY_PARAMETER] instanceof VariableProviderRegistry;
56
    }
57
}
58