HasCollectionsTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 35
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initCollection() 0 6 2
A appendTo() 0 4 1
A getCollection() 0 8 2
A hasCollection() 0 4 2
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\Behavior\Property;
13
14
use InvalidArgumentException;
15
use Tidal\WampWatch\Model\Contract\Property\CollectionInterface;
16
17
trait HasCollectionsTrait
18
{
19
    private function initCollection($name, CollectionInterface $collection)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
20
    {
21
        if (property_exists($this, $name)) {
22
            $this->{$name} = $collection;
23
        }
24
    }
25
26
    private function appendTo($name, $value)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
27
    {
28
        $this->getCollection($name)->append($value);
29
    }
30
31
    /**
32
     * @param string $name the name of the collection
33
     *
34
     * @throws InvalidArgumentException when the collection has not been initialized before
35
     *
36
     * @return CollectionInterface the collection object
37
     */
38
    private function getCollection($name)
39
    {
40
        if (!$this->hasCollection($name)) {
41
            throw new InvalidArgumentException("No Collection with name '$name' registered.");
42
        }
43
44
        return $this->{$name};
45
    }
46
47
    private function hasCollection($name)
48
    {
49
        return property_exists($this, $name) && is_a($this->{$name}, CollectionInterface::class);
50
    }
51
}
52