Completed
Push — master ( ea4d71...e95b62 )
by David
9s
created

AnonymousRequestMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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