HasRealmsTrait::removeRealm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like initCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
93
        $realms->setObjectConstrain(RealmInterface::class);
94
    }
95
}
96