Completed
Pull Request — master (#322)
by David de
14:19 queued 11:01
created

AccessControlledSubscriber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 67
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionsResolver() 0 12 1
A isRequestAllowed() 0 4 1
B __construct() 0 14 5
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCache\SymfonyCache;
13
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\RequestMatcher;
17
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
/**
21
 * Base class for handlers for the symfony built-in HttpCache that need access
22
 * control on requests.
23
 *
24
 * @author David Buchmann <[email protected]>
25
 *
26
 * {@inheritdoc}
27
 */
28
abstract class AccessControlledSubscriber implements EventSubscriberInterface
29
{
30
    /**
31
     * @var RequestMatcher
32
     */
33
    private $clientMatcher;
34
35
    /**
36
     * When creating this subscriber, you can configure a number of options.
37
     *
38
     * - client_matcher: RequestMatcherInterface to identify clients that are allowed to send request.
39
     * - client_ips:     IP or array of IPs of clients that are allowed to send requests.
40
     *
41
     * Only one of request matcher or IPs may be a non-null value. If you use a
42
     * RequestMatcher, configure your IPs into it.
43
     *
44
     * If neither parameter is set, the filter is IP 127.0.0.1
45
     *
46
     * @param array $options Options to overwrite the default options
47
     *
48
     * @throws \InvalidArgumentException if both client_matcher and client_ips are set or unknown keys are found in $options
49
     */
50 14
    public function __construct(array $options = [])
51
    {
52 14
        $options = $this->getOptionsResolver()->resolve($options);
53
54 12
        $clientMatcher = $options['client_matcher'];
55 12
        if ($clientMatcher && $options['client_ips']) {
56 1
            throw new \InvalidArgumentException('You may not set both a request matcher and an IP.');
57
        }
58 11
        if (!$clientMatcher) {
59 8
            $clientMatcher = new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1');
60 8
        }
61
62 11
        $this->clientMatcher = $clientMatcher;
63 11
    }
64
65
    /**
66
     * Get the options resolver for the constructor arguments.
67
     *
68
     * @return OptionsResolver
69
     */
70 14
    protected function getOptionsResolver()
71
    {
72 14
        $resolver = new OptionsResolver();
73 14
        $resolver->setDefaults([
74 14
            'client_matcher' => null,
75 14
            'client_ips' => null,
76 14
        ]);
77
        $resolver->setAllowedTypes('client_matcher', [RequestMatcherInterface::class, 'null']);
78
        $resolver->setAllowedTypes('client_ips', ['string', 'array', 'null']);
79
80
        return $resolver;
81
    }
82
83
    /**
84
     * Check whether the request is allowed.
85
     *
86
     * @param Request $request The request to check
87
     *
88
     * @return bool Whether access is granted
89
     */
90
    protected function isRequestAllowed(Request $request)
91
    {
92
        return $this->clientMatcher->matches($request);
93
    }
94
}
95