|
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\Publish\Core\MVC\Symfony\SiteAccess; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
|
12
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
|
13
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess; |
|
14
|
|
|
use function iterator_to_array; |
|
15
|
|
|
|
|
16
|
|
|
class SiteAccessService implements SiteAccessServiceInterface, SiteAccessAware |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface */ |
|
19
|
|
|
private $provider; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess */ |
|
22
|
|
|
private $siteAccess; |
|
23
|
|
|
|
|
24
|
|
|
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */ |
|
25
|
|
|
private $configResolver; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
SiteAccessProviderInterface $provider, |
|
29
|
|
|
ConfigResolverInterface $configResolver |
|
30
|
|
|
) { |
|
31
|
|
|
$this->provider = $provider; |
|
32
|
|
|
$this->configResolver = $configResolver; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function setSiteAccess(SiteAccess $siteAccess = null): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->siteAccess = $siteAccess; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function exists(string $name): bool |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->provider->isDefined($name); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function get(string $name): SiteAccess |
|
46
|
|
|
{ |
|
47
|
|
|
if ($this->provider->isDefined($name)) { |
|
48
|
|
|
return $this->provider->getSiteAccess($name); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
throw new NotFoundException('SiteAccess', $name); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getAll(): iterable |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->provider->getSiteAccesses(); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getCurrent(): ?SiteAccess |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->siteAccess === null){ |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $this->get($this->siteAccess->name); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getSiteAccessesRelation(?SiteAccess $siteAccess = null): array |
|
69
|
|
|
{ |
|
70
|
|
|
$siteAccess = $siteAccess ?? $this->siteAccess; |
|
71
|
|
|
$saRelationMap = []; |
|
72
|
|
|
|
|
73
|
|
|
/** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess[] $saList */ |
|
74
|
|
|
$saList = iterator_to_array($this->provider->getSiteAccesses()); |
|
75
|
|
|
// First build the SiteAccess relation map, indexed by repository and rootLocationId. |
|
76
|
|
|
foreach ($saList as $sa) { |
|
77
|
|
|
$siteAccessName = $sa->name; |
|
78
|
|
|
|
|
79
|
|
|
$repository = $this->configResolver->getParameter('repository', 'ezsettings', $siteAccessName); |
|
80
|
|
|
if (!isset($saRelationMap[$repository])) { |
|
81
|
|
|
$saRelationMap[$repository] = []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$rootLocationId = $this->configResolver->getParameter('content.tree_root.location_id', 'ezsettings', $siteAccessName); |
|
85
|
|
|
if (!isset($saRelationMap[$repository][$rootLocationId])) { |
|
86
|
|
|
$saRelationMap[$repository][$rootLocationId] = []; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$saRelationMap[$repository][$rootLocationId][] = $siteAccessName; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$siteAccessName = $siteAccess->name; |
|
93
|
|
|
$repository = $this->configResolver->getParameter('repository', 'ezsettings', $siteAccessName); |
|
94
|
|
|
$rootLocationId = $this->configResolver->getParameter('content.tree_root.location_id', 'ezsettings', $siteAccessName); |
|
95
|
|
|
|
|
96
|
|
|
return $saRelationMap[$repository][$rootLocationId]; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.