Container   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 39.38%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 65
ccs 13
cts 33
cp 0.3938
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 2
A set() 0 6 1
A has() 0 4 1
A assertIdentifierSet() 0 6 2
A setAsService() 0 12 3
A remove() 0 10 3
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 9/22/16 7:00 PM
7
*/
8
9
namespace Youshido\GraphQL\Execution\Container;
10
11
class Container implements ContainerInterface
12
{
13
14
    private $keyset   = [];
15
    private $values   = [];
16
    private $services = [];
17
18
19
    /**
20
     * @param $id
21
     * @return mixed
22
     * @throws \Exception if there was no value set under specified id
23
     */
24 1
    public function get($id)
25
    {
26 1
        $this->assertIdentifierSet($id);
27 1
        if (isset($this->services['id'])) {
28
            return $this->services['id']($this);
29
        }
30 1
        return $this->values[$id];
31
    }
32
33 1
    public function set($id, $value)
34
    {
35 1
        $this->values[$id] = $value;
36 1
        $this->keyset[$id] = true;
37 1
        return $this;
38
    }
39
40
    protected function setAsService($id, $service)
41
    {
42
        if (!is_object($service)) {
43
            throw new \RuntimeException(sprintf('Service %s has to be an object', $id));
44
        }
45
46
        $this->services[$id] = $service;
47
        if (isset($this->values[$id])) {
48
            unset($this->values[$id]);
49
        }
50
        $this->keyset[$id]   = true;
51
    }
52
53
    public function remove($id)
54
    {
55
        $this->assertIdentifierSet($id);
56
        if (array_key_exists($id, $this->values)) {
57
            unset($this->values[$id]);
58
        }
59
        if (array_key_exists($id, $this->services)) {
60
            unset($this->services[$id]);
61
        }
62
    }
63
64 1
    public function has($id)
65
    {
66 1
        return isset($this->keyset[$id]);
67
    }
68
69 1
    private function assertIdentifierSet($id)
70
    {
71 1
        if (!$this->has($id)) {
72
            throw new \RuntimeException(sprintf('Container item "%s" was not set', $id));
73
        }
74
    }
75
}