Container::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Directus\SDK;
4
5
class Container
6
{
7
    protected $data = [];
8
9
    public function set($key, $value)
10
    {
11
        $this->data[$key] = $value;
12
    }
13
14
    public function get($key)
15
    {
16
        if (!isset($this->data[$key])) {
17
            throw new \InvalidArgumentException(sprintf('Key "%s" is not defined.', $key));
18
        }
19
20
        $value = $this->data[$key];
21
        if (is_callable($value)) {
22
            $value = call_user_func_array($value, [$this]);
23
        }
24
25
        return $value;
26
    }
27
28
    public function singleton($key, $value)
29
    {
30
        $this->set($key, function($container) use ($value) {
31
            static $object = null;
32
            if ($object == null) {
33
                $object = call_user_func_array($value, [$container]);
34
            }
35
36
            return $object;
37
        });
38
    }
39
}