DefinitionContainer::remove()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\IoC\Definition\Container;
26
27
use Brickoo\Component\IoC\Definition\Container\Exception\DefinitionNotAvailableException;
28
use Brickoo\Component\Common\Assert;
29
30
/**
31
 * Implements an abstract definition container.
32
 */
33
abstract class DefinitionContainer implements \IteratorAggregate, \Countable {
34
35
    /** @var array */
36
    protected $entries = [];
37
38
    /**
39
     * Check if the definition container is empty.
40
     * @return boolean check result
41
     */
42 2
    public function isEmpty() {
43 2
        return empty($this->entries);
44
    }
45
46
    /**
47
     * Check if the definition contains an entry.
48
     * @param string $entryKey
49
     * @throws \InvalidArgumentException
50
     * @return boolean check result
51
     */
52 2
    public function contains($entryKey) {
53 2
        Assert::isString($entryKey);
54 2
        return isset($this->entries[$entryKey]);
55
    }
56
57
    /**
58
     * Add an entry to the list.
59
     * @param string $key
60
     * @param mixed $value
61
     * @return \Brickoo\Component\IoC\Definition\Container\DefinitionContainer
62
     */
63 4
    public function add($key, $value) {
64 4
        $this->entries[$key] = $value;
65 4
        return $this;
66
    }
67
68
    /**
69
     * Return an entry by its key..
70
     * @param string $entryKey
71
     * @throws \Brickoo\Component\IoC\Definition\Container\Exception\DefinitionNotAvailableException
72
     * @return \Brickoo\Component\IoC\Definition\ArgumentDefinition
73
     */
74 4
    public function get($entryKey) {
75 4
        Assert::isString($entryKey);
76
77 4
        if (!$this->contains($entryKey)) {
78 2
            throw new DefinitionNotAvailableException($entryKey);
79
        }
80
81 2
        return $this->entries[$entryKey];
82
    }
83
84
    /**
85
     * Remove an entry from container.
86
     * @param string $entryKey
87
     * @throws \InvalidArgumentException
88
     * @return \Brickoo\Component\IoC\Definition\Container\DefinitionContainer
89
     */
90 2
    public function remove($entryKey) {
91 2
        Assert::isString($entryKey);
92
93 2
        if ($this->contains($entryKey)) {
94 2
            unset($this->entries[$entryKey]);
95 2
        }
96 2
        return $this;
97
    }
98
99
    /**
100
     * Return the entries definitions as a list.
101
     * @return array
102
     */
103 2
    public function getAll() {
104 2
        return array_values($this->entries);
105
    }
106
107
    /**
108
     * Retrieve an array iterator containing the entries.
109
     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
110
     * @return \ArrayIterator
111
     */
112 2
    public function getIterator() {
113 2
        return new \ArrayIterator($this->getAll());
114
    }
115
116
    /**
117
     * Count the entries in the container.
118
     * @link http://php.net/manual/en/countable.count.php
119
     * @return integer the amount of injections
120
     */
121 4
    public function count() {
122 4
        return count($this->entries);
123
    }
124
125
}
126