Completed
Pull Request — master (#329)
by David
03:26
created

AnonymousRequestMatcher   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 30%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 28
ccs 3
cts 10
cp 0.3
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B matches() 0 15 5
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle 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\HttpCacheBundle\UserContext;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
16
17
/**
18
 * Matches anonymous requests using a list of identification headers.
19
 */
20
class AnonymousRequestMatcher implements RequestMatcherInterface
21
{
22
    private $userIdentifierHeaders;
23
24
    /**
25
     * @param array $userIdentifierHeaders List of request headers that authenticate a non-anonymous request
26
     */
27 15
    public function __construct(array $userIdentifierHeaders)
28
    {
29 15
        $this->userIdentifierHeaders = $userIdentifierHeaders;
30 15
    }
31
32
    public function matches(Request $request)
33
    {
34
        foreach ($this->userIdentifierHeaders as $header) {
35
            if ($request->headers->has($header)) {
36
                if (strtolower($header) === 'cookie' && 0 === $request->cookies->count()) {
37
                    // ignore empty cookie header
38
                    continue;
39
                }
40
41
                return false;
42
            }
43
        }
44
45
        return true;
46
    }
47
}
48