1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tidal/WampWatch package. |
5
|
|
|
* (c) 2016 Timo Michna <timomichna/yahoo.de> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tidal\WampWatch\Model\Property\Collection; |
13
|
|
|
|
14
|
|
|
use Tidal\WampWatch\Model\Contract; |
15
|
|
|
use Tidal\WampWatch\Model\Contract\Property\ObjectCollectionInterface; |
16
|
|
|
use Tidal\WampWatch\Model\Contract\RealmInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class HasRealmsTrait. |
20
|
|
|
* |
21
|
|
|
* Important! Classes using this trait have to also use trait |
22
|
|
|
* Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait |
23
|
|
|
* for this trait to work; |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
trait HasRealmsTrait |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
protected $realmsPropertyName = 'realms'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ObjectCollectionInterface |
31
|
|
|
*/ |
32
|
|
|
private $realms; |
33
|
|
|
|
34
|
|
|
public function addRealm(Contract\RealmInterface $realm) |
35
|
|
|
{ |
36
|
|
|
$this->getRealms()->set($realm->getName(), $realm); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return ObjectCollectionInterface |
41
|
|
|
*/ |
42
|
|
|
private function getRealms() |
43
|
|
|
{ |
44
|
|
|
return $this->getCollection($this->realmsPropertyName); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name |
49
|
|
|
* |
50
|
|
|
* @return ObjectCollectionInterface |
51
|
|
|
*/ |
52
|
|
|
abstract protected function getCollection($name); |
53
|
|
|
|
54
|
|
|
public function hasRealm($name) |
55
|
|
|
{ |
56
|
|
|
return $this->getRealms()->has($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $name |
61
|
|
|
* |
62
|
|
|
* @return Contract\RealmInterface |
63
|
|
|
*/ |
64
|
|
|
public function getRealm($name) |
65
|
|
|
{ |
66
|
|
|
return $this->getRealms()->get($name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $name |
71
|
|
|
*/ |
72
|
|
|
public function removeRealm($name) |
73
|
|
|
{ |
74
|
|
|
$this->getRealms()->offsetUnset($name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return \Generator |
79
|
|
|
*/ |
80
|
|
|
public function listRealms() |
81
|
|
|
{ |
82
|
|
|
foreach ($this->getRealms()->getIterator() as $name => $realm) { |
83
|
|
|
yield $name => $realm; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param ObjectCollectionInterface $realms |
89
|
|
|
*/ |
90
|
|
|
private function setRealms(ObjectCollectionInterface $realms) |
91
|
|
|
{ |
92
|
|
|
$this->initCollection($this->realmsPropertyName, $realms); |
|
|
|
|
93
|
|
|
$realms->setObjectConstrain(RealmInterface::class); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.