Options::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\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