Completed
Pull Request — master (#4)
by Nikola
02:38
created

Options::__construct()   A

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
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