Options   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 50
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromArray() 0 9 1
A getWhitelist() 0 4 1
A getLimitExceededHandler() 0 4 1
A getDefaultOptions() 0 11 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\Middleware;
12
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * @author Nikola Posa <[email protected]>
18
 */
19
class Options
20
{
21
    /**
22
     * @var callable
23
     */
24
    protected $whitelist;
25
26
    /**
27
     * @var callable
28
     */
29
    protected $limitExceededHandler;
30
31 7
    public function __construct(callable $whitelist, callable $limitExceededHandler)
32
    {
33 7
        $this->whitelist = $whitelist;
34 7
        $this->limitExceededHandler = $limitExceededHandler;
35 7
    }
36
37 7
    public static function fromArray(array $options)
38
    {
39 7
        $options = array_merge(self::getDefaultOptions(), $options);
40
41 7
        return new self(
42 7
            $options['whitelist'],
43 7
            $options['limitExceededHandler']
44
        );
45
    }
46
47 7
    public function getWhitelist()
48
    {
49 7
        return $this->whitelist;
50
    }
51
52 3
    public function getLimitExceededHandler()
53
    {
54 3
        return $this->limitExceededHandler;
55
    }
56
57
    private static function getDefaultOptions()
58
    {
59
        return [
60 7
            'whitelist' => function (RequestInterface $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61 6
                return false;
62 7
            },
63 7
            'limitExceededHandler' => function (RequestInterface $request, ResponseInterface $response) {
64 2
                return $response;
65 7
            },
66
        ];
67
    }
68
}
69