RateLimiterFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 29
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createInMemoryRateLimiter() 0 4 1
A createRedisBackedRateLimiter() 0 18 2
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