|
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 AppendIterator; |
|
12
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
|
13
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccess; |
|
14
|
|
|
use eZ\Publish\Core\MVC\Symfony\SiteAccessGroup; |
|
15
|
|
|
|
|
16
|
|
|
class SiteAccessService implements SiteAccessServiceInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $providers; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\SiteAccessGroup[] |
|
25
|
|
|
*/ |
|
26
|
|
|
private $siteAccessGroups = []; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(iterable $providers, array $siteAccessGroups) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->providers = $providers; |
|
31
|
|
|
|
|
32
|
|
|
foreach ($siteAccessGroups as $name) { |
|
33
|
|
|
$this->siteAccessGroups[] = new SiteAccessGroup($name); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function exists(string $name): bool |
|
38
|
|
|
{ |
|
39
|
|
|
foreach ($this->providers as $provider) { |
|
40
|
|
|
if ($provider->isDefined($name)) { |
|
41
|
|
|
return true; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
public function get(string $name): SiteAccess |
|
49
|
|
|
{ |
|
50
|
|
|
foreach ($this->providers as $provider) { |
|
51
|
|
|
if ($provider->isDefined($name)) { |
|
52
|
|
|
return $provider->getSiteAccess($name); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
throw new NotFoundException('SiteAccess', $name); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getAll(): iterable |
|
60
|
|
|
{ |
|
61
|
|
|
$iterator = new AppendIterator(); |
|
62
|
|
|
foreach ($this->providers as $provider) { |
|
63
|
|
|
$iterator->append($provider->getSiteAccesses()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $iterator; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getAvailableGroups(): iterable |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->siteAccessGroups; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getCurrent(): SiteAccess |
|
75
|
|
|
{ |
|
76
|
|
|
// TODO: Implement getCurrent() method. |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
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.