Completed
Push — master ( 26e625...88c602 )
by Timo
02:58
created

HasProceduresTrait::addProcedure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
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
17
/**
18
 * Class HasProceduresTrait.
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
trait HasProceduresTrait
25
{
26
    protected static $proceduresPropertyName = 'procedures';
27
28
    /**
29
     * @var ObjectCollectionInterface
30
     */
31
    private $procedures;
32
33
    public function addProcedure(Contract\ProcedureInterface $procedure)
34
    {
35
        $this->getProcedures()->set($procedure->getUri(), $procedure);
36
    }
37
38
    /**
39
     * @return ObjectCollectionInterface
40
     */
41
    private function getProcedures()
42
    {
43
        return $this->getCollection(static::$proceduresPropertyName);
0 ignored issues
show
Bug introduced by
It seems like getCollection() 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...
44
    }
45
46
    public function hasProcedure($uri)
47
    {
48
        return $this->getProcedures()->has($uri);
49
    }
50
51
    /**
52
     * @param string $uri
53
     *
54
     * @return Contract\ProcedureInterface
55
     */
56
    public function getProcedure($uri)
57
    {
58
        return $this->getProcedures()->get($uri);
59
    }
60
61
    /**
62
     * @param string $uri
63
     */
64
    public function removeProcedure($uri)
65
    {
66
        $this->getProcedures()->offsetUnset($uri);
67
    }
68
69
    /**
70
     * @return \Generator
71
     */
72
    public function listProcedures()
73
    {
74
        foreach ($this->getProcedures()->getIterator() as $uri => $procedure) {
75
            yield $uri => $procedure;
76
        }
77
    }
78
79
    /**
80
     * @param ObjectCollectionInterface $procedures
81
     */
82
    private function setProcedures(ObjectCollectionInterface $procedures)
83
    {
84
        $this->initCollection(static::$proceduresPropertyName, $procedures);
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...
85
        $procedures->setObjectConstrain(Contract\ProcedureInterface::class);
86
    }
87
}
88