RateLimiterFactory::createInMemoryRateLimiter()   A
last analyzed

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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * This file is part of the Rate Limit package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
namespace RateLimit;
12
13
use Redis;
14
15
/**
16
 * @author Nikola Posa <[email protected]>
17
 */
18
final class RateLimiterFactory
19
{
20
    const DEFAULT_LIMIT = 100;
21
    const DEFAULT_WINDOW = 15 * 60;
22
23 8
    public static function createInMemoryRateLimiter($limit = self::DEFAULT_LIMIT, $window = self::DEFAULT_WINDOW)
24
    {
25 8
        return new InMemoryRateLimiter($limit, $window);
26
    }
27
28 2
    public static function createRedisBackedRateLimiter(array $redisOptions = [], $limit = self::DEFAULT_LIMIT, $window = self::DEFAULT_WINDOW)
29
    {
30 2
        $redisOptions = array_merge([
31 2
            'host' => '127.0.0.1',
32
            'port' => 6379,
33
            'timeout' => 0.0,
34 2
        ], $redisOptions);
35
36 2
        if (!class_exists('\Redis')) {
37
            throw new \Exception('\Redis class was not found.');
38
        }
39
40 2
        $redis = new Redis();
41
42 2
        $redis->connect($redisOptions['host'], $redisOptions['port'], $redisOptions['timeout']);
43
44 2
        return new RedisRateLimiter($redis, $limit, $window);
45
    }
46
}
47