Completed
Push — master ( 3b7ffc...93bca7 )
by Łukasz
15:02
created

ContainerConfigResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getParameter() 0 10 2
A hasParameter() 0 4 1
A getDefaultNamespace() 0 4 1
A setDefaultNamespace() 0 4 1
A resolveScopeRelativeParamName() 0 4 1
A resolveNamespaceAndScope() 0 4 2
A getScopeRelativeParamName() 0 4 1
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\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver;
10
11
use eZ\Publish\Core\MVC\ConfigResolverInterface;
12
use eZ\Publish\Core\MVC\Exception\ParameterNotFoundException;
13
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
14
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
15
16
abstract class ContainerConfigResolver implements ConfigResolverInterface, ContainerAwareInterface
17
{
18
    use ContainerAwareTrait;
19
20
    /** @var string */
21
    private $scope;
22
23
    /** @var string */
24
    private $defaultNamespace;
25
26
    public function __construct(string $scope, string $defaultNamespace)
27
    {
28
        $this->scope = $scope;
29
        $this->defaultNamespace = $defaultNamespace;
30
    }
31
32
    public function getParameter(string $paramName, ?string $namespace = null, ?string $scope = null)
33
    {
34
        [$namespace, $scope] = $this->resolveNamespaceAndScope($namespace, $scope);
35
        $scopeRelativeParamName = $this->getScopeRelativeParamName($paramName, $namespace, $scope);
36
        if ($this->container->hasParameter($scopeRelativeParamName)) {
37
            return $this->container->getParameter($scopeRelativeParamName);
38
        }
39
40
        throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
41
    }
42
43
    public function hasParameter(string $paramName, ?string $namespace = null, ?string $scope = null): bool
44
    {
45
        return $this->container->hasParameter($this->resolveScopeRelativeParamName($paramName, $namespace, $scope));
46
    }
47
48
    public function getDefaultNamespace(): string
49
    {
50
        return $this->defaultNamespace;
51
    }
52
53
    public function setDefaultNamespace(string $defaultNamespace): void
54
    {
55
        $this->defaultNamespace = $defaultNamespace;
56
    }
57
58
    private function resolveScopeRelativeParamName(string $paramName, string $namespace = null, string $scope = null): string
59
    {
60
        return $this->getScopeRelativeParamName($paramName, ...$this->resolveNamespaceAndScope($namespace, $scope));
0 ignored issues
show
Bug introduced by
The call to getScopeRelativeParamName() misses a required argument $scope.

This check looks for function calls that miss required arguments.

Loading history...
61
    }
62
63
    private function resolveNamespaceAndScope(string $namespace = null, string $scope = null): array
64
    {
65
        return [$namespace ?: $this->getDefaultNamespace(), $scope ?? $this->scope];
66
    }
67
68
    private function getScopeRelativeParamName(string $paramName, string $namespace, string $scope): string
69
    {
70
        return "$namespace.$scope.$paramName";
71
    }
72
}
73