Completed
Push — master ( fd9de3...4c7769 )
by Stephen
03:51
created

Store   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 108
ccs 33
cts 33
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getIterator() 0 4 1
A offsetExists() 0 7 2
A offsetGet() 0 7 2
A offsetSet() 0 5 2
A offsetUnset() 0 5 3
A count() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
1
<?php
2
namespace StarCitizen\Models;
3
4
use ArrayAccess;
5
use Countable;
6
use IteratorAggregate;
7
use ArrayIterator;
8
9
/**
10
 * Class Store
11
 *
12
 * @package StarCitizen\Models;
13
 */
14
class Store implements ArrayAccess, Countable, IteratorAggregate
15
{
16
    protected $items = [];
17
18
    private $className;
19
20
    /**
21
     * Store constructor.
22
     *
23
     * @param array $data
24
     * @param string $className
25
     * @param string $dataRoot
26
     * @param string $idName
27
     */
28 10
    public function __construct(array $data, $className, $dataRoot, $idName)
29
    {
30 10
        $this->className = 'StarCitizen\Models' . $className;
31 10
        foreach ($data as $item) {
32 10
            if ($dataRoot != '')
33 9
                $objectData = $item[$dataRoot];
34
            else
35 3
                $objectData = $item;
36
37 10
            $storageObject = new \ReflectionClass('StarCitizen\Models' . $className);
38 10
            $this->offsetSet($objectData[$idName], $storageObject->newInstance($objectData));
39
        }
40 10
    }
41
42
    /**
43
     * @return ArrayIterator
44
     */
45 9
    public function getIterator()
46
    {
47 9
        return new ArrayIterator($this->items);
48
    }
49
50
    /**
51
     * @param mixed $offset
52
     *
53
     * @return bool
54
     */
55 5
    public function offsetExists($offset)
56
    {
57 5
        if (array_key_exists($offset, $this->items))
58 3
            return true;
59
60 2
        return false;
61
    }
62
63
    /**
64
     * @param mixed $offset
65
     *
66
     * @return bool|mixed
67
     */
68 3
    public function offsetGet($offset)
69
    {
70 3
        if ($this->offsetExists($offset))
71 2
            return $this->items[$offset];
72
73 1
        return false;
74
    }
75
76
    /**
77
     * @param mixed $offset
78
     * @param mixed $value
79
     */
80 10
    public function offsetSet($offset, $value)
81
    {
82 10
        if ($value instanceof $this->className)
83 10
            $this->items[$offset] = $value;
84 10
    }
85
86
    /**
87
     * @param mixed $offset
88
     */
89 1
    public function offsetUnset($offset)
90
    {
91 1
        if ($this->offsetExists($offset) && isset($this->items[$offset]))
92
            unset ($this->items[$offset]);
93 1
    }
94
95
    /**
96
     * @return int
97
     */
98 3
    public function count()
99
    {
100 3
        return count($this->items);
101
    }
102
103
    /**
104
     * @param $name
105
     *
106
     * @return bool|mixed
107
     */
108 3
    public function __get($name)
109
    {
110 3
        return $this->offsetGet($name);
111
    }
112
113
    /**
114
     * @param $name
115
     * @param $value
116
     */
117 2
    public function __set($name, $value)
118
    {
119 2
        $this->offsetSet($name, $value);
120
    }
121
}