Completed
Push — EZP-31046-chain-config-resolve... ( cbbd6c...51eaf2 )
by
unknown
14:38
created

SiteAccessConfigResolver::doGetParameter()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver;
6
7
use eZ\Publish\Core\MVC\Exception\ParameterNotFoundException;
8
use eZ\Publish\Core\MVC\Symfony\Configuration\VersatileScopeInterface;
9
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
10
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
11
12
abstract class SiteAccessConfigResolver implements VersatileScopeInterface, SiteAccessAware
13
{
14
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface */
15
    protected $siteAccessProvider;
16
17
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess */
18
    protected $currentSiteAccess;
19
20
    /** @var string */
21
    protected $defaultScope;
22
23
    /** @var string */
24
    protected $defaultNamespace;
25
26
    public function __construct(
27
        SiteAccess\SiteAccessProviderInterface $siteAccessProvider,
28
        string $defaultNamespace
29
    ) {
30
        $this->siteAccessProvider = $siteAccessProvider;
31
        $this->defaultNamespace = $defaultNamespace;
32
    }
33
34
    public function hasParameter(string $paramName, ?string $namespace = null, ?string $scope = null): bool
35
    {
36
        [$namespace, $scope] = $this->resolveNamespaceAndScope($namespace, $scope);
37
        if (!$this->isSiteAccessScope($scope)) {
38
            return false;
39
        }
40
41
        $siteAccess = $this->siteAccessProvider->getSiteAccess($scope);
42
        if (!$this->isSiteAccessSupported($siteAccess)) {
43
            return false;
44
        }
45
46
        return $this->doHasParameter($siteAccess, $namespace, $scope);
47
    }
48
49
    public function getParameter(string $paramName, ?string $namespace = null, ?string $scope = null)
50
    {
51
        [$namespace, $scope] = $this->resolveNamespaceAndScope($namespace, $scope);
52
53
        if (!$this->isSiteAccessScope($scope)) {
54
            throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
55
        }
56
57
        $siteAccess = $this->siteAccessProvider->getSiteAccess($scope);
58
        if (!$this->isSiteAccessSupported($siteAccess)) {
59
            throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
60
        }
61
62
        return $this->doGetParameter($siteAccess, $paramName, $namespace);
63
    }
64
65
    public function getDefaultNamespace(): string
66
    {
67
        return $this->defaultNamespace;
68
    }
69
70
    public function setDefaultNamespace($defaultNamespace): void
71
    {
72
        $this->defaultNamespace = $defaultNamespace;
73
    }
74
75
    public function getDefaultScope(): string
76
    {
77
        return $this->defaultScope ?: $this->currentSiteAccess->name;
78
    }
79
80
    public function setDefaultScope(string $scope): void
81
    {
82
        $this->defaultScope = $scope;
83
    }
84
85
    public function setSiteAccess(SiteAccess $siteAccess = null): void
86
    {
87
        $this->currentSiteAccess = $siteAccess;
88
    }
89
90
    protected function isSiteAccessScope(string $scope): bool
91
    {
92
        return $this->siteAccessProvider->isDefined($scope);
93
    }
94
95
    /**
96
     * Returns true if current config provider supports given Site Access.
97
     */
98
    protected function isSiteAccessSupported(SiteAccess $siteAccess): bool
99
    {
100
        return true;
101
    }
102
103
    protected function resolveScopeRelativeParamName(string $paramName, ?string $namespace = null, ?string $scope = null): string
104
    {
105
        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...
106
    }
107
108
    protected function resolveNamespaceAndScope(?string $namespace = null, ?string $scope = null): array
109
    {
110
        return [$namespace ?: $this->getDefaultNamespace(), $scope ?: $this->getDefaultScope()];
111
    }
112
113
    protected function getScopeRelativeParamName(string $paramName, string $namespace, string $scope): string
114
    {
115
        return "$namespace.$scope.$paramName";
116
    }
117
118
    abstract protected function doHasParameter(SiteAccess $siteAccess, string $paramName, string $namespace): bool;
119
120
    abstract protected function doGetParameter(SiteAccess $siteAccess, string $paramName, string $namespace);
121
}