Completed
Push — master ( 7898d7...bd6151 )
by
unknown
44s
created

Container   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 49
ccs 13
cts 13
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 4 1
A remove() 0 6 2
A get() 0 8 2
A has() 0 4 1
1
<?php
2
declare(strict_types=1);
3
namespace Zewa;
4
5
use Zewa\Exception\LookupException;
6
7
class Container
8
{
9
    /**
10
     * @var array
11
     */
12
    private $container = [];
13
14
    /**
15
     * Add an object to the container
16
     *
17
     * @param string $key The name of a service to set in the container
18
     * @param mixed $value A closure of object representing a serive
19
     * @return $this
20
     */
21 28
    public function set($key, $value = null)
22
    {
23 28
        $this->container[$key] = $value;
24 28
    }
25
26 1
    public function remove($key)
27
    {
28 1
        if ($this->has($key)) {
29 1
            unset($this->container[$key]);
30
        }
31 1
    }
32
33
    /**
34
     * @param $key
35
     * @return mixed
36
     * @throws LookupException
37
     */
38 29
    public function get($key)
39
    {
40 29
        if (!$this->has($key)) {
41 1
            throw new LookupException('Container doesn\'t exist.');
42
        }
43
44 28
        return $this->container[$key];
45
    }
46
47
    /**
48
     * @param $key
49
     * @return bool
50
     */
51 29
    public function has($key)
52
    {
53 29
        return array_key_exists($key, $this->container);
54
    }
55
}
56