Completed
Push — master ( b69735...6d6276 )
by John
05:05
created

RequestMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
     * @param Repository $repository
28
     */
29
    public function __construct(Repository $repository)
30
    {
31
        $this->repository = $repository;
32
    }
33
34
    /**
35
     * @param Request $request
36
     * @return bool
37
     */
38
    public function matches(Request $request)
39
    {
40
        if(!$request->attributes->has(RequestMeta::ATTRIBUTE_URI)){
41
            return false;
42
        }
43
        $description = $this->repository->get($request->attributes->get(RequestMeta::ATTRIBUTE_URI));
44
        $operation   = $description
45
            ->getPath($request->attributes->get(RequestMeta::ATTRIBUTE_PATH))
46
            ->getOperation($request->getMethod());
47
48
        return $operation->isSecured();
49
    }
50
}
51