Completed
Push — master ( 63b238...29b170 )
by Hong
03:12
created

DelegatorTrait::getFromLookup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 6
nc 3
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\Reference;
16
17
use Phossa2\Shared\Message\Message;
18
use Phossa2\Shared\Exception\InvalidArgumentException;
19
20
/**
21
 * DelegatorTrait
22
 *
23
 * Implementation of DelegatorInterface
24
 *
25
 * @package Phossa2\Shared
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     DelegatorInterface
28
 * @version 2.0.8
29
 * @since   2.0.8 added
30
 */
31
trait DelegatorTrait
32
{
33
    /**
34
     * lookup pool of registries
35
     *
36
     * @var    array
37
     * @access private
38
     */
39
    private $lookup_pool = [];
40
41
    /**
42
     * cached lookup key
43
     *
44
     * @var    string
45
     * @access private
46
     */
47
    private $cache_key;
48
49
    /**
50
     * cached lookup registry
51
     *
52
     * @var    object
53
     * @access private
54
     */
55
    private $cache_reg;
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function addRegistry($registry)
61
    {
62
        if ($this->isValidRegistry($registry)) {
63
            // remove this registry if exists already
64
            $this->removeFromLookup($registry);
65
66
            // append to the pool end
67
            $this->lookup_pool[] = $registry;
68
69
            return $this;
70
        }
71
72
        throw new InvalidArgumentException(
73
            Message::get(Message::MSG_ARGUMENT_INVALID, get_class($registry)),
74
            Message::MSG_ARGUMENT_INVALID
75
        );
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function hasInLookup(/*# string */ $key)/*# : bool */
82
    {
83
        foreach ($this->lookup_pool as $registry) {
84
            if ($this->hasInRegistry($registry, $key)) {
85
                $this->cache_key = $key;
86
                $this->cache_reg = $registry;
87
                return true;
88
            }
89
        }
90
        return false;
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function getFromLookup(/*# string */ $key)
97
    {
98
        // check cache first
99
        if ($key === $this->cache_key) {
100
            return $this->getFromRegistry($this->cache_reg, $key);
101
        }
102
103
        // try lookup
104
        if ($this->hasInLookup($key)) {
105
            return $this->getFromRegistry($this->cache_reg, $key);
106
        }
107
108
        // not found
109
        return null;
110
    }
111
112
    /**
113
     * Remove one object from the pool
114
     *
115
     * @param  object $registry
116
     * @return $this
117
     * @access protected
118
     */
119
    protected function removeFromLookup($registry)
120
    {
121
        foreach ($this->lookup_pool as $idx => $reg) {
122
            if ($registry === $reg) {
123
                unset($this->lookup_pool[$idx]);
124
            }
125
        }
126
        return $this;
127
    }
128
129
    /**
130
     * Is registry type allowed in lookup pool ?
131
     *
132
     * @param  object $registry
133
     * @return bool
134
     * @access protected
135
     */
136
    abstract protected function isValidRegistry($registry)/*# : bool */;
137
138
    /**
139
     * Try has in registry
140
     *
141
     * @param  object $registry
142
     * @param  name $key
143
     * @return bool
144
     * @access protected
145
     */
146
    abstract protected function hasInRegistry(
147
        $registry,
148
        /*# string */ $key
149
    )/*# : bool */;
150
151
    /**
152
     * Try get from registry
153
     *
154
     * @param  object $registry
155
     * @param  name $key
156
     * @return mixed
157
     * @access protected
158
     */
159
    abstract protected function getFromRegistry(
160
        $registry,
161
        /*# string */ $key
162
    );
163
}
164