Completed
Pull Request — master (#38)
by Boris
04:23 queued 02:11
created

CacheTool::getFunction()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.125

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 22
ccs 7
cts 14
cp 0.5
rs 8.6737
nc 4
cc 5
eloc 11
nop 1
crap 8.125
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool;
13
14
use CacheTool\Adapter\AbstractAdapter;
15
use CacheTool\Proxy\ProxyInterface;
16
use Psr\Log\LoggerInterface;
17
use Monolog\Logger;
18
19
class CacheTool
20
{
21
    /**
22
     * @var AbstractAdapter
23
     */
24
    protected $adapter;
25
26
    /**
27
     * @var array
28
     */
29
    protected $proxies = array();
30
31
    /**
32
     * @var array
33
     */
34
    protected $functions = array();
35
36
    /**
37
     * @var string
38
     */
39
    protected $tempDir;
40
41
    /**
42
     * @var LoggerInterface
43
     */
44
    protected $logger;
45
46
    /**
47
     * @param string          $tempDir
48
     * @param LoggerInterface $logger
49
     */
50 10
    public function __construct($tempDir = null, LoggerInterface $logger = null)
51
    {
52 10
        $this->logger = $logger ?: new Logger('cachetool');
53
54 10
        $tempDirs = array($tempDir, '/dev/shm', '/var/run', sys_get_temp_dir());
55
56 10
        foreach ($tempDirs as $tempDir) {
57 10
            if (is_dir($tempDir) && is_writable($tempDir)) {
58 10
                $this->tempDir = $tempDir;
59 10
                break;
60
            }
61 10
        }
62 10
    }
63
64
    /**
65
     * @param  AbstractAdapter $adapter
66
     * @param  string          $tempDir
67
     * @param  LoggerInterface $logger
68
     * @return CacheTool
69
     */
70 6
    public static function factory(AbstractAdapter $adapter = null, $tempDir = null, LoggerInterface $logger = null)
71
    {
72 6
        $cacheTool = new static($tempDir, $logger);
73 6
        $cacheTool->addProxy(new Proxy\ApcProxy());
74 6
        $cacheTool->addProxy(new Proxy\ApcuProxy());
75 6
        $cacheTool->addProxy(new Proxy\PhpProxy());
76 6
        $cacheTool->addProxy(new Proxy\OpcacheProxy());
77
78 6
        if ($adapter instanceof AbstractAdapter) {
79 5
            $cacheTool->setAdapter($adapter);
80 5
        }
81
82 6
        return $cacheTool;
83
    }
84
85
    /**
86
     * @param  AbstractAdapter $adapter
87
     * @return CacheTool
88
     */
89 6
    public function setAdapter(AbstractAdapter $adapter)
90
    {
91 6
        $this->logger->info(sprintf('Setting adapter: %s', get_class($adapter)));
92
93 6
        $this->adapter = $adapter;
94 6
        $this->adapter->setLogger($this->logger);
95 6
        $this->adapter->setTempDir($this->tempDir);
96
97 6
        return $this;
98
    }
99
100
    /**
101
     * @return AbstractAdapter
102
     */
103 3
    public function getAdapter()
104
    {
105 3
        return $this->adapter;
106
    }
107
108
    /**
109
     * @param ProxyInterface $proxy
110
     * @return CacheTool
111
     */
112 6
    public function addProxy(ProxyInterface $proxy)
113
    {
114 6
        $this->logger->info(sprintf('Adding Proxy: %s', get_class($proxy)));
115
116 6
        $this->proxies[] = $proxy;
117
118
        // reset functions (to be built when needed)
119 6
        $this->functions = array();
120
121 6
        return $this;
122
    }
123
124
    /**
125
     * @return array
126
     */
127 3
    public function getProxies()
128
    {
129 3
        return $this->proxies;
130
    }
131
132
    /**
133
     * @param  LoggerInterface $logger
134
     * @return CacheTool
135
     */
136 1
    public function setLogger(LoggerInterface $logger)
137
    {
138 1
        $this->logger = $logger;
139
140 1
        if ($this->adapter instanceof AbstractAdapter) {
141 1
            $this->adapter->setLogger($logger);
142 1
        }
143
144 1
        return $this;
145
    }
146
147
    /**
148
     * @return LoggerInterface
149
     */
150 1
    public function getLogger()
151
    {
152 1
        return $this->logger;
153
    }
154
155
    /**
156
     * Calls proxy functions
157
     *
158
     * @param  string $name
159
     * @param  array $arguments
160
     * @return mixed
161
     */
162 2
    public function __call($name, $arguments)
163
    {
164 2
        $this->logger->notice(sprintf('Executing: %s(%s)', $name, implode(', ', array_map('json_encode', $arguments))));
165
166 2
        if ($this->getFunction($name)) {
167
            return call_user_func_array($this->getFunction($name), $arguments);
168
        }
169
    }
170
171
    /**
172
     * Initializes functions and return callable
173
     *
174
     * @param  string $name
175
     * @return callable
176
     */
177 2
    protected function getFunction($name)
178
    {
179 2
        if (empty($this->functions)) {
180 2
            foreach ($this->proxies as $proxy) {
181
                $this->logger->info(sprintf('Loading Proxy: %s', get_class($proxy)));
182
183
                // lazily set adapter
184
                $proxy->setAdapter($this->adapter);
185
186
                foreach ($proxy->getFunctions() as $fn) {
187
                    $this->logger->debug(sprintf('Loading Function: %s', $fn));
188
                    $this->functions[$fn] = array($proxy, $fn);
189
                }
190 2
            }
191 2
        }
192
193 2
        if (isset($this->functions[$name])) {
194
            return $this->functions[$name];
195
        }
196
197 2
        throw new \InvalidArgumentException("Function with name: {$name} is not provided by any Proxy.");
198
    }
199
}
200