Cache   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 115
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 4 1
A getAllForType() 0 7 2
A clearType() 0 7 2
A clearAll() 0 5 1
A push() 0 5 1
A remove() 0 7 2
A get() 0 8 2
A has() 0 4 1
1
<?php
2
3
namespace As3\Modlr\Store;
4
5
use As3\Modlr\Models\Model;
6
7
/**
8
 * Manages Model objects that are currently loaded into memory.
9
 *
10
 * @author Jacob Bare <[email protected]>
11
 */
12
class Cache
13
{
14
    /**
15
     * All models currently loaded in memory.
16
     * Is an associative array of model type keys to model objects.
17
     *
18
     * @var array
19
     */
20
    private $models = [];
21
22
    /**
23
     * Gets all models in the memory cache.
24
     * Will be keyed by model type.
25
     *
26
     * @return  array
27
     */
28
    public function getAll()
29
    {
30
        return $this->models;
31
    }
32
33
    /**
34
     * Gets all models in the memory cache for a specific type.
35
     *
36
     * @param   string  $typeKey
37
     * @return  Model[]
38
     */
39
    public function getAllForType($typeKey)
40
    {
41
        if (isset($this->models[$typeKey])) {
42
            return $this->models[$typeKey];
43
        }
44
        return [];
45
    }
46
47
    /**
48
     * Clears all models in the memory cache for a specific type.
49
     *
50
     * @param   string  $typeKey
51
     * @return  self
52
     */
53
    public function clearType($typeKey)
54
    {
55
        if (isset($this->models[$typeKey])) {
56
            unset($this->models[$typeKey]);
57
        }
58
        return $this;
59
    }
60
61
    /**
62
     * Clears all models in the memory cache.
63
     *
64
     * @return  self
65
     */
66
    public function clearAll()
67
    {
68
        $this->models = [];
69
        return $this;
70
    }
71
72
    /**
73
     * Pushes a model into the memory cache.
74
     *
75
     * @param   Model   $model
76
     * @return  self
77
     */
78
    public function push(Model $model)
79
    {
80
        $this->models[$model->getType()][$model->getId()] = $model;
81
        return $this;
82
    }
83
84
    /**
85
     * Removes a model from the memory cache, based on type and identifier.
86
     *
87
     * @param   string  $typeKey
88
     * @param   string  $identifier
89
     * @return  self
90
     */
91
    public function remove($typeKey, $identifier)
92
    {
93
        if (isset($this->models[$typeKey][$identifier])) {
94
            unset($this->models[$typeKey][$identifier]);
95
        }
96
        return $this;
97
    }
98
99
    /**
100
     * Gets a model from the memory cache, based on type and identifier.
101
     *
102
     * @param   string  $typeKey
103
     * @param   string  $identifier
104
     * @return  Model|null
105
     */
106
    public function get($typeKey, $identifier)
107
    {
108
        $map = $this->getAllForType($typeKey);
109
        if (isset($map[$identifier])) {
110
            return $map[$identifier];
111
        }
112
        return null;
113
    }
114
115
    /**
116
     * Determines if a model exists in the memory cache, based on type and identifier.
117
     *
118
     * @param   string  $typeKey
119
     * @param   string  $identifier
120
     * @return  bool
121
     */
122
    public function has($typeKey, $identifier)
123
    {
124
        return null !== $this->get($typeKey, $identifier);
125
    }
126
}
127