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

SiteAccessConfigResolver::hasParameter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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