Completed
Pull Request — master (#4)
by
unknown
29:40
created

DesarrollaCacheFactory::createMemoryDriver()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 6
nc 1
nop 0
crap 2.0116
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2015 Krishnaprasad MG <[email protected]>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace Sunspikes\Ratelimit\Cache\Factory;
27
28
use Desarrolla2\Cache\Adapter\Apcu;
29
use Desarrolla2\Cache\Adapter\File;
30
use Desarrolla2\Cache\Adapter\Memcache;
31
use Desarrolla2\Cache\Adapter\Memory;
32
use Desarrolla2\Cache\Adapter\Mongo;
33
use Desarrolla2\Cache\Adapter\Mysqli;
34
use Desarrolla2\Cache\Adapter\NotCache;
35
use Desarrolla2\Cache\Adapter\Predis;
36
use Desarrolla2\Cache\Cache;
37
use Sunspikes\Ratelimit\Cache\Exception\DriverNotFoundException;
38
use Sunspikes\Ratelimit\Cache\Exception\InvalidConfigException;
39
40
class DesarrollaCacheFactory implements FactoryInterface
41
{
42
    /* @const DEFAULT_TTL */
43
    const DEFAULT_TTL = 3600;
44
    /* @const DEFAULT_LIMIT */
45
    const DEFAULT_LIMIT = 1000;
46
47
    /* @var array */
48
    protected $config;
49
50
    /**
51
     * @param string|null $configFile
52
     * @param array       $configArray
53
     */
54 5
    public function __construct($configFile = null, array $configArray = [])
55
    {
56
        // Default config from distribution
57 5
        if (null === $configFile) {
58 5
            $configFile = __DIR__.'/../../../config/config.php';
59 5
        }
60
61 5
        $config = include $configFile;
62
63 5
        $this->config = array_merge($config, $configArray);
64 5
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 5
    public function make()
70
    {
71 5
        return new Cache($this->getDriver());
72
    }
73
74
    /**
75
     * Make the driver based on given config
76
     *
77
     * @return null|\Desarrolla2\Cache\Adapter\AdapterInterface
78
     *
79
     * @throws DriverNotFoundException
80
     * @throws InvalidConfigException
81
     */
82 5
    protected function getDriver()
83
    {
84 5
        $driver = $this->config['driver'];
85
86 5
        if (is_null($driver)) {
87
            throw new InvalidConfigException('Cache driver is not defined in configuration.');
88
        }
89
90 5
        $driverCreateMethod = 'create' . ucfirst($driver) . 'Driver';
91
92 5
        if (method_exists($this, $driverCreateMethod)) {
93 5
            $driver = $this->{$driverCreateMethod}();
94 5
            $driver->setOption('ttl',
95 5
                $this->config['default_ttl']
96
                    ?: static::DEFAULT_TTL
97 5
            );
98
99 5
            return $driver;
100
        }
101
102
        throw new DriverNotFoundException('Cannot find the driver ' . $driver . ' for Desarrolla');
103
    }
104
105
    /**
106
     * Create NotCache driver
107
     *
108
     * @return NotCache
109
     */
110 1
    protected function createNotcacheDriver()
111
    {
112 1
        return new NotCache();
113
    }
114
115
    /**
116
     * Create File driver
117
     *
118
     * @return File
119
     */
120 1
    protected function createFileDriver()
121
    {
122 1
        return new File($this->config['file']['cache_dir']);
123
    }
124
125
    /**
126
     * Create APC driver
127
     *
128
     * @return Apcu
129
     */
130 1
    protected function createApcDriver()
131
    {
132 1
        return new Apcu();
133
    }
134
135
    /**
136
     * Create Memory driver
137
     *
138
     * @return Memory
139
     */
140 1
    protected function createMemoryDriver()
141
    {
142 1
        $memory = new Memory();
143 1
        $memory->setOption('limit',
144 1
            $this->config['memory']['limit']
145
                ?: static::DEFAULT_LIMIT
146 1
        );
147
148 1
        return $memory;
149
    }
150
151
    /**
152
     * Create Mongo driver
153
     *
154
     * @return Mongo
155
     */
156
    protected function createMongoDriver()
157
    {
158
        return new Mongo($this->config['mongo']['server']);
159
    }
160
161
    /**
162
     * Create MySQL driver
163
     *
164
     * @return Mysqli
165
     */
166 1
    protected function createMysqlDriver()
167
    {
168 1
        $server = null;
169
170 1
        if (!empty($this->config['mysql'])) {
171
            $server = new \mysqli(
172
                $this->config['mysql']['host'],
173
                $this->config['mysql']['username'],
174
                $this->config['mysql']['password'],
175
                $this->config['mysql']['dbname'],
176
                $this->config['mysql']['port']
177
            );
178
        }
179
180 1
        return new Mysqli($server);
181
    }
182
183
    /**
184
     * Create Redis driver
185
     *
186
     * @return Predis
187
     */
188
    protected function createRedisDriver()
189
    {
190
        return new Predis();
191
    }
192
193
    /**
194
     * Create MemCache driver
195
     *
196
     * @return MemCache
197
     */
198
    protected function createMemcacheDriver()
199
    {
200
        $server = null;
201
202
        if (isset($this->config['servers'])) {
203
            $server = new \Memcache();
204
205
            foreach ($this->config['servers'] as $host) {
206
                $server->addserver($host);
207
            }
208
        }
209
210
        return new Memcache($server);
211
    }
212
}
213