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

AnonymousRequestMatcher::matches()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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