Completed
Push — github-actions ( 673aea...a81297 )
by Samuel
01:18
created

CacheTool::addProxy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 = [];
30
31
    /**
32
     * @var array
33
     */
34
    protected $functions = [];
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 24
    public function __construct($tempDir = null, LoggerInterface $logger = null)
51
    {
52 24
        $this->logger = $logger ?: new Logger('cachetool');
53
54 24
        if (is_null($tempDir)) {
55 8
            $tempDirs = ['/dev/shm', '/var/run', sys_get_temp_dir()];
56 8
            foreach ($tempDirs as $dir) {
57 8
                if (is_dir($dir) && is_writable($dir)) {
58 8
                    $tempDir = $dir;
59 8
                    break;
60
                }
61
            }
62
        }
63 24
        $this->tempDir = $tempDir;
64 24
    }
65
66
    /**
67
     * @param  AbstractAdapter $adapter
68
     * @param  string          $tempDir
69
     * @param  LoggerInterface $logger
70
     * @return CacheTool
71
     */
72 21
    public static function factory(AbstractAdapter $adapter = null, $tempDir = null, LoggerInterface $logger = null)
73
    {
74 21
        $cacheTool = new static($tempDir, $logger);
75 21
        $cacheTool->addProxy(new Proxy\ApcuProxy());
76 21
        $cacheTool->addProxy(new Proxy\PhpProxy());
77 21
        $cacheTool->addProxy(new Proxy\OpcacheProxy());
78
79 21
        if ($adapter instanceof AbstractAdapter) {
80 20
            $cacheTool->setAdapter($adapter);
81
        }
82
83 21
        return $cacheTool;
84
    }
85
86
    /**
87
     * @param  AbstractAdapter $adapter
88
     * @return CacheTool
89
     */
90 20
    public function setAdapter(AbstractAdapter $adapter)
91
    {
92 20
        $this->logger->info(sprintf('Setting adapter: %s', get_class($adapter)));
93
94 20
        $this->adapter = $adapter;
95 20
        $this->adapter->setLogger($this->logger);
96 20
        $this->adapter->setTempDir($this->tempDir);
97
98 20
        return $this;
99
    }
100
101
    /**
102
     * @return AbstractAdapter
103
     */
104 3
    public function getAdapter()
105
    {
106 3
        return $this->adapter;
107
    }
108
109 1
    public function getTempDir()
110
    {
111 1
        return $this->tempDir;
112
    }
113
114
    /**
115
     * @param ProxyInterface $proxy
116
     * @return CacheTool
117
     */
118 21
    public function addProxy(ProxyInterface $proxy)
119
    {
120 21
        $this->logger->info(sprintf('Adding Proxy: %s', get_class($proxy)));
121
122 21
        $this->proxies[] = $proxy;
123
124
        // reset functions (to be built when needed)
125 21
        $this->functions = [];
126
127 21
        return $this;
128
    }
129
130
    /**
131
     * @return array
132
     */
133 3
    public function getProxies()
134
    {
135 3
        return $this->proxies;
136
    }
137
138
    /**
139
     * @param  LoggerInterface $logger
140
     * @return CacheTool
141
     */
142
    public function setLogger(LoggerInterface $logger)
143
    {
144
        $this->logger = $logger;
145
146
        if ($this->adapter instanceof AbstractAdapter) {
147
            $this->adapter->setLogger($logger);
148
        }
149
150
        return $this;
151
    }
152
153
    /**
154
     * @return LoggerInterface
155
     */
156 1
    public function getLogger()
157
    {
158 1
        return $this->logger;
159
    }
160
161
    /**
162
     * Calls proxy functions
163
     *
164
     * @param  string $name
165
     * @param  array $arguments
166
     * @return mixed
167
     */
168 16
    public function __call($name, $arguments)
169
    {
170 16
        $this->logger->notice(sprintf('Executing: %s(%s)', $name, implode(', ', array_map('json_encode', $arguments))));
171
172 16
        $function = $this->getFunction($name);
173 14
        if ($function) {
174 14
            return $function(...$arguments);
175
        }
176
    }
177
178
    /**
179
     * Initializes functions and return callable
180
     *
181
     * @param  string $name
182
     * @return callable
183
     */
184 16
    protected function getFunction($name)
185
    {
186 16
        if (empty($this->functions)) {
187 16
            foreach ($this->proxies as $proxy) {
188 14
                $this->logger->info(sprintf('Loading Proxy: %s', get_class($proxy)));
189
190
                // lazily set adapter
191 14
                $proxy->setAdapter($this->adapter);
192
193 14
                foreach ($proxy->getFunctions() as $fn) {
194 14
                    $this->logger->debug(sprintf('Loading Function: %s', $fn));
195 14
                    $this->functions[$fn] = [$proxy, $fn];
196
                }
197
            }
198
        }
199
200 16
        if (isset($this->functions[$name])) {
201 14
            return $this->functions[$name];
202
        }
203
204 2
        throw new \InvalidArgumentException("Function with name: {$name} is not provided by any Proxy.");
205
    }
206
}
207