Registry::getContext()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
namespace Suricate;
3
4
class Registry
5
{
6
    protected static $data = [];
7
    protected static $context;
8
9 3
    public static function get($key, $defaultValue = null)
10
    {
11 3
        $data = &static::getFromContext();
12 3
        if (isset($data[$key])) {
13 3
            return $data[$key];
14
        }
15
        
16 3
        return $defaultValue;
17
    }
18
19 1
    public static function getProperty($key, $property, $defaultValue = null)
20
    {
21 1
        if ($object = static::get($key)) {
22 1
            if (isset($object->$property)) {
23 1
                return $object->$property;
24
            }
25
        }
26
27 1
        return $defaultValue;
28
    }
29
30 3
    public static function set($key, $value)
31
    {
32 3
        $data = &static::getFromContext();
33 3
        $data[$key] = $value;
34 3
    }
35
36 1
    public static function setContext($context)
37
    {
38 1
        static::$context = $context;
39 1
    }
40
41 1
    public static function getContext()
42
    {
43 1
        return static::$context;
44
    }
45
46 1
    public static function exists($key)
47
    {
48 1
        $data = &static::getFromContext();
49
50 1
        return isset($data[$key]);
51
    }
52
53 3
    private static function &getFromContext()
54
    {
55 3
        if (static::$context !== null) {
56 1
            if (!isset(static::$data[static::$context])) {
57 1
                static::$data[static::$context] = [];
58
            }
59 1
            return static::$data[static::$context];
60
        }
61
62 3
        return static::$data;
63
    }
64
65 2
    public static function clean()
66
    {
67 2
        $data = &static::getFromContext();
68 2
        $data = [];
69 2
    }
70
}
71