Completed
Pull Request — master (#3)
by
unknown
06:03 queued 48s
created

DesarrollaCacheFactory::createNotcacheDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Apc;
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\MySQL;
34
use Desarrolla2\Cache\Adapter\NotCache;
35
use Desarrolla2\Cache\Adapter\Redis;
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 1
    public function __construct($configFile = null, array $configArray = [])
55
    {
56
        // Default config from distribution
57 1
        if (null === $configFile) {
58 1
            $configFile = __DIR__.'/../../../config/config.php';
59 1
        }
60
61 1
        $config = include $configFile;
62
63 1
        if (!isset($config['adapter']) || !isset($config['desarrolla']) || 'desarrolla' !== $config['adapter']) {
64
            throw new \InvalidArgumentException('Invalid adapter found, please check your config.');
65
        }
66
67 1
        $this->config = array_merge($config['desarrolla'], $configArray);
68 1
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 1
    public function make()
74
    {
75 1
        return new Cache($this->getDriver());
76
    }
77
78
    /**
79
     * Make the driver based on given config
80
     *
81
     * @return null|\Desarrolla2\Cache\Adapter\AdapterInterface
82
     *
83
     * @throws DriverNotFoundException
84
     * @throws InvalidConfigException
85
     */
86 1
    protected function getDriver()
87
    {
88 1
        $driver = $this->config['driver'];
89
90 1
        if (is_null($driver)) {
91
            throw new InvalidConfigException('Cache driver is not defined in configuration.');
92
        }
93
94 1
        $driverCreateMethod = 'create' . ucfirst($driver) . 'Driver';
95
96 1
        if (method_exists($this, $driverCreateMethod)) {
97 1
            $driver = $this->{$driverCreateMethod}();
98 1
            $driver->setOption('ttl',
99 1
                $this->config['default_ttl']
100
                    ?: static::DEFAULT_TTL
101 1
            );
102
103 1
            return $driver;
104
        }
105
106
        throw new DriverNotFoundException('Cannot find the driver ' . $driver . ' for Desarrolla');
107
    }
108
109
    /**
110
     * Create NotCache driver
111
     *
112
     * @return NotCache
113
     */
114 1
    protected function createNotcacheDriver()
115
    {
116 1
        return new NotCache();
117
    }
118
119
    /**
120
     * Create File driver
121
     *
122
     * @return File
123
     */
124
    protected function createFileDriver()
125
    {
126
        return new File($this->config['cache_dir']);
127
    }
128
129
    /**
130
     * Create APC driver
131
     *
132
     * @return Apc
133
     */
134
    protected function createApcDriver()
135
    {
136
        return new Apc();
137
    }
138
139
    /**
140
     * Create Memory driver
141
     *
142
     * @return Memory
143
     * @throws \Desarrolla2\Cache\Adapter\MemoryCacheException
144
     */
145
    protected function createMemoryDriver()
146
    {
147
        $memory = new Memory();
148
        $memory->setOption('limit',
149
            $this->config['limit']
150
                ?: static::DEFAULT_LIMIT
151
        );
152
153
        return $memory;
154
    }
155
156
    /**
157
     * Create Mongo driver
158
     *
159
     * @return Mongo
160
     */
161
    protected function createMongoDriver()
162
    {
163
        return new Mongo($this->config['server']);
164
    }
165
166
    /**
167
     * Create MySQL driver
168
     *
169
     * @return MySQL
170
     */
171
    protected function createMysqlDriver()
172
    {
173
        return new MySQL(
174
            $this->config['host'],
175
            $this->config['username'],
176
            $this->config['password'],
177
            $this->config['port']
178
        );
179
    }
180
181
    /**
182
     * Create Redis driver
183
     *
184
     * @return Redis
185
     */
186
    protected function createRedisDriver()
187
    {
188
        return new Redis();
189
    }
190
191
    /**
192
     * Create MemCache driver
193
     *
194
     * @return MemCache
195
     */
196
    protected function createMemcacheDriver()
197
    {
198
        return new MemCache();
199
    }
200
}
201