Completed
Pull Request — master (#12)
by
unknown
12:17 queued 02:05
created

Options::getWhitelist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
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
    public function __construct(callable $whitelist, callable $limitExceededHandler)
34
    {
35
        $this->whitelist = $whitelist;
36
        $this->limitExceededHandler = $limitExceededHandler;
37
    }
38
39
    public static function fromArray(array $options)
40
    {
41
        $options = array_merge(self::getDefaultOptions(), $options);
42
43
        return new self(
44
            $options['whitelist'],
45
            $options['limitExceededHandler']
46
        );
47
    }
48
49
    public function getWhitelist() : callable
50
    {
51
        return $this->whitelist;
52
    }
53
54
    public function getLimitExceededHandler() : callable
55
    {
56
        return $this->limitExceededHandler;
57
    }
58
59
    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
                return false;
64
            },
65
            'limitExceededHandler' => function (RequestInterface $request, ResponseInterface $response) {
66
                return $response;
67
            },
68
        ];
69
    }
70
}
71