Completed
Push — master ( 0faaef...a96626 )
by Nikola
02:09
created

Options   A

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
declare(strict_types=1);
12
13
namespace RateLimit\Middleware;
14
15
use Psr\Http\Message\RequestInterface;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * @author Nikola Posa <[email protected]>
20
 */
21
class Options
22
{
23
    /**
24
     * @var callable
25
     */
26
    protected $whitelist;
27
28
    /**
29
     * @var callable
30
     */
31
    protected $limitExceededHandler;
32
33 7
    public function __construct(callable $whitelist, callable $limitExceededHandler)
34
    {
35 7
        $this->whitelist = $whitelist;
36 7
        $this->limitExceededHandler = $limitExceededHandler;
37 7
    }
38
39 7
    public static function fromArray(array $options)
40
    {
41 7
        $options = array_merge(self::getDefaultOptions(), $options);
42
43 7
        return new self(
44 7
            $options['whitelist'],
45 7
            $options['limitExceededHandler']
46
        );
47
    }
48
49 7
    public function getWhitelist() : callable
50
    {
51 7
        return $this->whitelist;
52
    }
53
54 3
    public function getLimitExceededHandler() : callable
55
    {
56 3
        return $this->limitExceededHandler;
57
    }
58
59 7
    private static function getDefaultOptions() : array
60
    {
61
        return [
62
            '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...
63 6
                return false;
64 7
            },
65 7
            'limitExceededHandler' => function (RequestInterface $request, ResponseInterface $response) {
66 2
                return $response;
67 7
            },
68
        ];
69
    }
70
}
71