Completed
Push — master ( 0b2651...52ea06 )
by Samuel
10:43 queued 09:39
created

CacheTool::getWritableTempDir()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

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