Completed
Push — dynamic_siteaccesses ( 16c7ff )
by
unknown
20:23
created

ContainerBasedConfigResolver::getParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
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 ContainerBasedConfigResolver 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($paramName, $namespace = null, $scope = null)
33
    {
34
        list($namespace, $scope) = $this->resolveNamespaceAndScope($namespace, $scope);
35
36
        $scopeRelativeParamName = $this->getScopeRelativeParamName($paramName, $namespace, $scope);
37
        if ($this->container->hasParameter($scopeRelativeParamName)) {
38
            return $this->container->getParameter($scopeRelativeParamName);
39
        }
40
41
        throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
42
    }
43
44
    public function hasParameter($paramName, $namespace = null, $scope = null): bool
45
    {
46
        return $this->container->hasParameter($this->resolveScopeRelativeParamName($paramName, $namespace, $scope));
47
    }
48
49
    public function getDefaultNamespace(): string
50
    {
51
        return $this->defaultNamespace;
52
    }
53
54
    public function setDefaultNamespace($defaultNamespace): void
55
    {
56
        $this->defaultNamespace = $defaultNamespace;
57
    }
58
59
    private function resolveScopeRelativeParamName($paramName, $namespace = null, $scope = null): string
60
    {
61
        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...
62
    }
63
64
    private function resolveNamespaceAndScope($namespace = null, $scope = null): array
0 ignored issues
show
Unused Code introduced by
The parameter $scope 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...
65
    {
66
        return [$namespace ?: $this->getDefaultNamespace(), $this->scope];
67
    }
68
69
    private function getScopeRelativeParamName(string $paramName, string $namespace, string $scope): string
70
    {
71
        return "$namespace.$scope.$paramName";
72
    }
73
}
74