Completed
Push — master ( aa6f89...863f7f )
by John
08:07
created

RequestMatcher::isMatchUnsecured()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\SwaggerBundle\Security;
9
10
use KleijnWeb\PhpApi\Descriptions\Description\Repository;
11
use KleijnWeb\SwaggerBundle\EventListener\Request\RequestMeta;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
14
15
16
/**
17
 * @author John Kleijn <[email protected]>
18
 */
19
class RequestMatcher implements RequestMatcherInterface
20
{
21
    /**
22
     * @var Repository
23
     */
24
    private $repository;
25
26
    /**
27
     * @var bool
28
     */
29
    private $matchUnsecured;
30
31
    /**
32
     * @param Repository $repository
33
     * @param bool       $matchUnsecured
34
     */
35
    public function __construct(Repository $repository, bool $matchUnsecured = false)
36
    {
37
        $this->repository     = $repository;
38
        $this->matchUnsecured = $matchUnsecured;
39
    }
40
41
    /**
42
     * @return boolean
43
     */
44
    public function isMatchUnsecured(): bool
45
    {
46
        return $this->matchUnsecured;
47
    }
48
49
    /**
50
     * @param boolean $matchUnsecured
51
     */
52
    public function setMatchUnsecured(bool $matchUnsecured)
53
    {
54
        $this->matchUnsecured = $matchUnsecured;
55
    }
56
57
    /**
58
     * @param Request $request
59
     * @return bool
60
     */
61
    public function matches(Request $request)
62
    {
63
        if (!$request->attributes->has(RequestMeta::ATTRIBUTE_URI)) {
64
            return false;
65
        }
66
67
        if ($this->matchUnsecured) {
68
            return true;
69
        }
70
71
        $description = $this->repository->get($request->attributes->get(RequestMeta::ATTRIBUTE_URI));
72
        $operation   = $description
73
            ->getPath($request->attributes->get(RequestMeta::ATTRIBUTE_PATH))
74
            ->getOperation($request->getMethod());
75
76
        return $operation->isSecured();
77
    }
78
}
79