Completed
Push — master ( 6b8ce2...059d2d )
by John
02:29
created

RequestMeta::getOperation()   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
9
namespace KleijnWeb\SwaggerBundle\EventListener\Request;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\Description;
12
use KleijnWeb\PhpApi\Descriptions\Description\Operation;
13
use KleijnWeb\PhpApi\Descriptions\Description\Repository;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * @author John Kleijn <[email protected]>
18
 */
19
class RequestMeta
20
{
21
    const ATTRIBUTE      = '_swagger.meta';
22
    const ATTRIBUTE_URI  = '_swagger.uri';
23
    const ATTRIBUTE_PATH = '_swagger.path';
24
25
    /**
26
     * @var Description
27
     */
28
    private $description;
29
30
    /**
31
     * @var Operation
32
     */
33
    private $operation;
34
35
    /**
36
     * RequestMeta constructor.
37
     *
38
     * @param Description $description
39
     * @param Operation   $operation
40
     */
41
    public function __construct(Description $description, Operation $operation)
42
    {
43
        $this->description = $description;
44
        $this->operation   = $operation;
45
    }
46
47
    /**
48
     * @return Operation
49
     */
50
    public function getOperation(): Operation
51
    {
52
        return $this->operation;
53
    }
54
55
    /**
56
     * @return Description
57
     */
58
    public function getDescription(): Description
59
    {
60
        return $this->description;
61
    }
62
63
    /**
64
     * @param Request    $request
65
     * @param Repository $repository
66
     * @return RequestMeta|null
67
     */
68
    public static function fromRequest(Request $request, Repository $repository)
69
    {
70
        if ($request->attributes->has(RequestMeta::ATTRIBUTE)) {
71
            return $request->attributes->get(RequestMeta::ATTRIBUTE);
72
        }
73
74
        if (!$request->attributes->has(RequestMeta::ATTRIBUTE_URI)) {
75
            return null;
76
        }
77
78
        $description = $repository->get($request->attributes->get(RequestMeta::ATTRIBUTE_URI));
79
        $operation   = $description
80
            ->getPath($request->attributes->get(RequestMeta::ATTRIBUTE_PATH))
81
            ->getOperation($request->getMethod());
82
83
        $self = new self($description, $operation);
84
        $request->attributes->set(RequestMeta::ATTRIBUTE, $self);
85
86
        return $self;
87
    }
88
}
89