Completed
Push — EZP-31046-chain-config-resolve... ( cbbd6c...51eaf2 )
by
unknown
14:38
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
/**
17
 * @property-read \Symfony\Component\DependencyInjection\ContainerInterface $container
18
 */
19
abstract class ContainerConfigResolver implements ConfigResolverInterface, ContainerAwareInterface
20
{
21
    use ContainerAwareTrait;
22
23
    /** @var string */
24
    private $scope;
25
26
    /** @var string */
27
    private $defaultNamespace;
28
29
    public function __construct(string $scope, string $defaultNamespace)
30
    {
31
        $this->scope = $scope;
32
        $this->defaultNamespace = $defaultNamespace;
33
    }
34
35
    public function getParameter(string $paramName, ?string $namespace = null, ?string $scope = null)
36
    {
37
        [$namespace, $scope] = $this->resolveNamespaceAndScope($namespace, $scope);
38
        $scopeRelativeParamName = $this->getScopeRelativeParamName($paramName, $namespace, $scope);
39
        if ($this->container->hasParameter($scopeRelativeParamName)) {
40
            return $this->container->getParameter($scopeRelativeParamName);
41
        }
42
43
        throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
44
    }
45
46
    public function hasParameter(string $paramName, ?string $namespace = null, ?string $scope = null): bool
47
    {
48
        return $this->container->hasParameter($this->resolveScopeRelativeParamName($paramName, $namespace, $scope));
49
    }
50
51
    public function getDefaultNamespace(): string
52
    {
53
        return $this->defaultNamespace;
54
    }
55
56
    public function setDefaultNamespace(string $defaultNamespace): void
57
    {
58
        $this->defaultNamespace = $defaultNamespace;
59
    }
60
61
    private function resolveScopeRelativeParamName(string $paramName, string $namespace = null, string $scope = null): string
62
    {
63
        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...
64
    }
65
66
    private function resolveNamespaceAndScope(string $namespace = null, string $scope = null): array
67
    {
68
        return [$namespace ?: $this->getDefaultNamespace(), $scope ?? $this->scope];
69
    }
70
71
    private function getScopeRelativeParamName(string $paramName, string $namespace, string $scope): string
72
    {
73
        return "$namespace.$scope.$paramName";
74
    }
75
}