Completed
Pull Request — master (#1)
by Karsten
03:23
created

EntityPoolImpl::clear()   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
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * File was created 11.10.2015 13:22
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Data;
7
8
use PeekAndPoke\Component\Psi\Psi;
9
10
/**
11
 * @author Karsten J. Gerber <[email protected]>
12
 */
13
class EntityPoolImpl implements EntityPool
14
{
15
    /** @var array */
16
    private $pool = [];
17
    /** @var int */
18
    private $numHits = 0;
19
    /** @var int */
20
    private $numMisses = 0;
21
22
    /**
23
     * @return array
24
     */
25 1
    public function all()
26
    {
27 1
        return $this->pool;
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public function stats()
34
    {
35
        return [
36
            'hits'   => $this->numHits,
37
            'misses' => $this->numMisses,
38
        ];
39
    }
40
41
    ////  GET SET CHECK  ///////////////////////////////////////////////////////////////////////////////////////////////
42
43
    /**
44
     * @param \ReflectionClass $cls
45
     * @param string           $idKey
46
     * @param mixed            $idValue
47
     *
48
     * @return bool
49
     */
50 19
    public function has(\ReflectionClass $cls, $idKey, $idValue)
51
    {
52 19
        $isset = isset($this->pool[$cls->name . '@' . $idKey . '@' . $idValue]);
53
54 19
        if ($isset) {
55 3
            ++$this->numHits;
56
        } else {
57 18
            ++$this->numMisses;
58
        }
59
60 19
        return $isset;
61
    }
62
63
    /**
64
     * @param \ReflectionClass $cls
65
     * @param string           $idKey
66
     * @param mixed            $idValue
67
     *
68
     * @return mixed
69
     */
70 3
    public function get(\ReflectionClass $cls, $idKey, $idValue)
71
    {
72 3
        return $this->pool[$cls->name . '@' . $idKey . '@' . $idValue];
73
    }
74
75
    /**
76
     * @param \ReflectionClass $cls
77
     * @param string           $idKey
78
     * @param mixed            $idValue
79
     * @param mixed            $value
80
     */
81 21
    public function set(\ReflectionClass $cls, $idKey, $idValue, $value)
82
    {
83 21
        $this->pool[$cls->name . '@' . $idKey . '@' . $idValue] = $value;
84 21
    }
85
86
    ////  REMOVING ITEMS  //////////////////////////////////////////////////////////////////////////////////////////////
87
88
    /**
89
     * Remove all entities from the pool
90
     */
91 18
    public function clear()
92
    {
93 18
        $this->pool = [];
94 18
    }
95
96
    /**
97
     * @param \ReflectionClass $cls
98
     * @param string           $idKey
99
     * @param string           $idValue
100
     */
101
    public function remove(\ReflectionClass $cls, $idKey, $idValue)
102
    {
103
        unset ($this->pool[$cls->name . '@' . $idKey . '@' . $idValue]);
104
    }
105
106
    /**
107
     * @param \ReflectionClass $cls
108
     */
109
    public function removeAllOfType(\ReflectionClass $cls)
110
    {
111
        $search = $cls->name . '@';
112
113
        $this->pool = Psi::it($this->pool)
114
            ->filterKey(function ($k) use ($search) {
115
                return strpos($k, $search) !== 0;
116
            })
117
            ->toArray();
118
    }
119
}
120