DelegatorTrait::removeFromLookup()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.2
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Delegator;
16
17
/**
18
 * DelegatorTrait
19
 *
20
 * Implementation of DelegatorInterface
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     DelegatorInterface
25
 * @version 2.0.16
26
 * @since   2.0.8  added
27
 * @since   2.0.15 modified, moved to new namespace
28
 * @since   2.0.16 added clearLookupCache()
29
 */
30
trait DelegatorTrait
31
{
32
    /**
33
     * lookup pool of registries
34
     *
35
     * @var    array
36
     * @access protected
37
     */
38
    protected $lookup_pool = [];
39
40
    /**
41
     * cached found key
42
     *
43
     * @var    string
44
     * @access private
45
     */
46
    private $cache_key;
47
48
    /**
49
     * cached lookup registry for found $cache_key
50
     *
51
     * @var    object
52
     * @access private
53
     */
54
    private $cache_reg;
55
56
    /**
57
     * Append one registry to lookup pool
58
     *
59
     * @param  object $registry
60
     * @return $this
61
     * @access protected
62
     */
63
    protected function addRegistry($registry)
64
    {
65
        // remove it if exists already
66
        $this->removeFromLookup($registry);
67
68
        // set delegator in registry
69
        if ($registry instanceof DelegatorAwareInterface) {
70
            $registry->setDelegator($this);
71
        }
72
73
        // append to the pool
74
        $this->lookup_pool[] = $registry;
75
76
        return $this;
77
    }
78
79
    /**
80
     * check existence in the whole lookup pool
81
     *
82
     * @param  string $key
83
     * @return bool
84
     * @access protected
85
     */
86
    protected function hasInLookup(/*# string */ $key)/*# : bool */
87
    {
88
        foreach ($this->lookup_pool as $registry) {
89
            if ($this->hasInRegistry($registry, $key)) {
90
                $this->cache_key = $key;
91
                $this->cache_reg = $registry;
92
                return true;
93
            }
94
        }
95
        return false;
96
    }
97
98
    /**
99
     * get from lookup pool, return NULL if not found
100
     *
101
     * @param  string $key
102
     * @return mixed|null
103
     * @access protected
104
     */
105
    protected function getFromLookup(/*# string */ $key)
106
    {
107
        // found already ? or try find
108
        if ($key === $this->cache_key || $this->hasInLookup($key)) {
109
            return $this->getFromRegistry($this->cache_reg, $key);
110
        }
111
        // not found
112
        return null;
113
    }
114
115
    /**
116
     * Remove one registry from the pool
117
     *
118
     * @param  object $registry
119
     * @return $this
120
     * @access protected
121
     */
122
    protected function removeFromLookup($registry)
123
    {
124
        foreach ($this->lookup_pool as $idx => $reg) {
125
            if ($registry === $reg) {
126
                if ($reg instanceof DelegatorAwareInterface) {
127
                    $reg->setDelegator();
128
                }
129
                unset($this->lookup_pool[$idx]);
130
            }
131
        }
132
        return $this;
133
    }
134
135
    /**
136
     * Clear lookup cache
137
     *
138
     * @access protected
139
     */
140
    protected function clearLookupCache()
141
    {
142
        $this->cache_key = null;
143
    }
144
145
    /**
146
     * Try HAS in registry
147
     *
148
     * @param  object $registry
149
     * @param  string $key
150
     * @return bool
151
     * @access protected
152
     */
153
    abstract protected function hasInRegistry(
154
        $registry,
155
        /*# string */ $key
156
    )/*# : bool */;
157
158
    /**
159
     * Try GET from registry
160
     *
161
     * @param  object $registry
162
     * @param  string $key
163
     * @return mixed|null
164
     * @access protected
165
     */
166
    abstract protected function getFromRegistry(
167
        $registry,
168
        /*# string */ $key
169
    );
170
}
171