Completed
Push — develop ( 98584e...565e24 )
by Mathieu
01:45
created

Registry::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Suricate;
3
4
class Registry
5
{
6
    protected static $data = [];
7
    protected static $context;
8
9 2
    public static function get($key, $defaultValue = null)
10
    {
11 2
        $data = &static::getFromContext();
12 2
        if (isset($data[$key])) {
13 2
            return $data[$key];
14
        }
15
        
16 2
        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 2
    public static function set($key, $value)
31
    {
32 2
        $data = &static::getFromContext();
33 2
        $data[$key] = $value;
34 2
    }
35
36 1
    public static function exists($key)
37
    {
38 1
        $data = &static::getFromContext();
39
40 1
        return isset($data[$key]);
41
    }
42
43 2
    private static function &getFromContext()
44
    {
45 2
        if (static::$context !== null) {
46
            if (!isset(static::$data[static::$context])) {
47
                static::$data[static::$context] = array();
48
            }
49
            return static::$data[static::$context];
50
        }
51
52 2
        return static::$data;
53
    }
54
55 1
    public static function clean()
56
    {
57 1
        $data = &static::getFromContext();
58 1
        $data = array();
59 1
    }
60
}
61