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

HasCollectionsTrait::appendTo()   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 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)
1 ignored issue
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)
1 ignored issue
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