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
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class HasTopicsTrait. |
19
|
|
|
* |
20
|
|
|
* Important! Classes using this trait have to also use trait |
21
|
|
|
* Tidal\WampWatch\Model\Behavior\Property\HasCollectionsTrait |
22
|
|
|
* for this trait to work; |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
trait HasTopicsTrait |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
protected $topicsPropertyName = 'topics'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ObjectCollectionInterface |
30
|
|
|
*/ |
31
|
|
|
private $topics; |
32
|
|
|
|
33
|
|
|
public function addTopic(Contract\TopicInterface $topic) |
34
|
|
|
{ |
35
|
|
|
$this->getTopics()->set($topic->getUri(), $topic); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return ObjectCollectionInterface |
40
|
|
|
*/ |
41
|
|
|
private function getTopics() |
42
|
|
|
{ |
43
|
|
|
return $this->getCollection($this->topicsPropertyName); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function hasTopic($uri) |
47
|
|
|
{ |
48
|
|
|
return $this->getTopics()->has($uri); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $uri |
53
|
|
|
* |
54
|
|
|
* @return Contract\TopicInterface |
55
|
|
|
*/ |
56
|
|
|
public function getTopic($uri) |
57
|
|
|
{ |
58
|
|
|
return $this->getTopics()->get($uri); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $uri |
63
|
|
|
*/ |
64
|
|
|
public function removeTopic($uri) |
65
|
|
|
{ |
66
|
|
|
$this->getTopics()->offsetUnset($uri); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \Generator |
71
|
|
|
*/ |
72
|
|
|
public function listTopics() |
73
|
|
|
{ |
74
|
|
|
foreach ($this->getTopics()->getIterator() as $uri => $topic) { |
75
|
|
|
yield $uri => $topic; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ObjectCollectionInterface $topics |
81
|
|
|
*/ |
82
|
|
|
private function setTopics(ObjectCollectionInterface $topics) |
83
|
|
|
{ |
84
|
|
|
$this->initCollection($this->topicsPropertyName, $topics); |
|
|
|
|
85
|
|
|
$topics->setObjectConstrain(Contract\TopicInterface::class); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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.