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

SiteAccessBasedConfigResolver::getParameter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 15
rs 9.7666
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 SiteAccessBasedConfigResolver implements VersatileScopeInterface, SiteAccessAware
13
{
14
    /**
15
     * @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface
16
     */
17
    protected $siteAccessProvider;
18
19
    /**
20
     * @var \eZ\Publish\Core\MVC\Symfony\SiteAccess
21
     */
22
    protected $currentSiteAccess;
23
24
    /**
25
     * @var string
26
     */
27
    protected $defaultScope;
28
29
    /**
30
     * @var string
31
     */
32
    protected $defaultNamespace;
33
34
    public function __construct(SiteAccess\SiteAccessProviderInterface $siteAccessProvider, string $defaultNamespace)
35
    {
36
        $this->siteAccessProvider = $siteAccessProvider;
37
        $this->defaultNamespace = $defaultNamespace;
38
    }
39
40
    public function hasParameter($paramName, $namespace = null, $scope = null)
41
    {
42
        list($namespace, $scope) = $this->resolveNamespaceAndScope($namespace, $scope);
43
        if (!$this->isSiteAccessScope($scope)) {
44
            return false;
45
        }
46
47
        $siteAccess = $this->siteAccessProvider->getSiteAccess($scope);
48
        if (!$this->isSiteAccessSupported($siteAccess)) {
49
            return false;
50
        }
51
52
        return $this->doHasParameter($siteAccess, $namespace, $scope);
53
    }
54
55
    public function getParameter($paramName, $namespace = null, $scope = null)
56
    {
57
        list($namespace, $scope) = $this->resolveNamespaceAndScope($namespace, $scope);
58
59
        if (!$this->isSiteAccessScope($scope)) {
60
            throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
61
        }
62
63
        $siteAccess = $this->siteAccessProvider->getSiteAccess($scope);
64
        if (!$this->isSiteAccessSupported($siteAccess)) {
65
            throw new ParameterNotFoundException($paramName, $namespace, [$scope]);
66
        }
67
68
        return $this->doGetParameter($siteAccess, $paramName, $namespace);
69
    }
70
71
    public function getDefaultNamespace(): string
72
    {
73
        return $this->defaultNamespace;
74
    }
75
76
    public function setDefaultNamespace($defaultNamespace): void
77
    {
78
        $this->defaultNamespace = $defaultNamespace;
79
    }
80
81
    /**
82
     * Returns current default scope.
83
     *
84
     * @return string
85
     */
86
    public function getDefaultScope(): string
87
    {
88
        return $this->defaultScope ?: $this->currentSiteAccess->name;
89
    }
90
91
    /**
92
     * Sets a new default scope.
93
     *
94
     * @param string $scope
95
     */
96
    public function setDefaultScope($scope): void
97
    {
98
        $this->defaultScope = $scope;
99
    }
100
101
    public function setSiteAccess(SiteAccess $siteAccess = null): void
102
    {
103
        $this->currentSiteAccess = $siteAccess;
104
    }
105
106
    /**
107
     * Return true if give scope is a Site Access
108
     *
109
     * @param string $scope
110
     *
111
     * @return bool
112
     */
113
    protected function isSiteAccessScope(string $scope): bool
114
    {
115
        return $this->siteAccessProvider->isDefined($scope);
116
    }
117
118
    /**
119
     * Returns true if current config  provider supports given Site Access.
120
     *
121
     * @param SiteAccess $siteAccess
122
     * @return bool
123
     */
124
    protected function isSiteAccessSupported(SiteAccess $siteAccess): bool
125
    {
126
        return true;
127
    }
128
129
    protected function resolveScopeRelativeParamName($paramName, $namespace = null, $scope = null): string
130
    {
131
        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...
132
    }
133
134
    protected function resolveNamespaceAndScope($namespace = null, $scope = null): array
135
    {
136
        return [$namespace ?: $this->getDefaultNamespace(), $scope ?: $this->getDefaultScope()];
137
    }
138
139
    protected function getScopeRelativeParamName(string $paramName, string $namespace, string $scope): string
140
    {
141
        return "$namespace.$scope.$paramName";
142
    }
143
144
    protected abstract function doHasParameter(SiteAccess $siteAccess, string $paramName, string $namespace): bool;
145
146
    protected abstract function doGetParameter(SiteAccess $siteAccess, string $paramName, string $namespace);
147
}
148