Completed
Pull Request — master (#81)
by John
03:37
created

RequestListener::onKernelRequest()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 14
nc 4
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
9
namespace KleijnWeb\SwaggerBundle\EventListener;
10
11
use KleijnWeb\SwaggerBundle\Document\DocumentRepository;
12
use KleijnWeb\SwaggerBundle\Document\Specification;
13
use KleijnWeb\SwaggerBundle\Document\Specification\Operation;
14
use KleijnWeb\SwaggerBundle\Request\RequestMeta;
15
use KleijnWeb\SwaggerBundle\Request\RequestProcessor;
16
use KleijnWeb\SwaggerBundle\Serialize\Serializer;
17
use KleijnWeb\SwaggerBundle\Serialize\TypeResolver\SerializerTypeDefinitionMapBuilder;
18
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19
20
/**
21
 * @author John Kleijn <[email protected]>
22
 */
23
class RequestListener
24
{
25
    /**
26
     * @var DocumentRepository
27
     */
28
    private $documentRepository;
29
30
    /**
31
     * @var RequestProcessor
32
     */
33
    private $processor;
34
35
    /**
36
     * @var SerializerTypeDefinitionMapBuilder
37
     */
38
    private $serializerTypeDefinitionMapBuilder;
39
40
    /**
41
     * RequestListener constructor.
42
     *
43
     * @param DocumentRepository                      $schemaRepository
44
     * @param RequestProcessor                        $processor
45
     * @param SerializerTypeDefinitionMapBuilder|null $serializerTypeDefinitionMapBuilder
46
     */
47
    public function __construct(
48
        DocumentRepository $schemaRepository,
49
        RequestProcessor $processor,
50
        SerializerTypeDefinitionMapBuilder $serializerTypeDefinitionMapBuilder = null
51
    ) {
52
        $this->documentRepository                 = $schemaRepository;
53
        $this->processor                          = $processor;
54
        $this->serializerTypeDefinitionMapBuilder = $serializerTypeDefinitionMapBuilder;
55
    }
56
57
    /**
58
     * @param GetResponseEvent $event
59
     */
60
    public function onKernelRequest(GetResponseEvent $event)
61
    {
62
        if (!$event->isMasterRequest()) {
63
            return;
64
        }
65
        $request = $event->getRequest();
66
        if (!$request->attributes->get('_swagger.file')) {
67
            return;
68
        }
69
        if (!$request->get('_swagger.path')) {
70
            throw new \LogicException("Request does not contain reference to Swagger path");
71
        }
72
73
        $specification = $this->documentRepository->get($request->attributes->get('_swagger.file'));
74
        $operation     = $specification->getOperation(
75
            $request->attributes->get('_swagger.path'),
76
            $request->getMethod()
77
        );
78
79
        $request->attributes->set('_swagger.meta', $this->createRequestMeta($specification, $operation));
80
81
        $this->processor->process($request, $operation);
82
    }
83
84
    /**
85
     * @param Specification $specification
86
     * @param Operation     $operation
87
     *
88
     * @return RequestMeta
89
     */
90
    private function createRequestMeta(Specification $specification, Operation $operation): RequestMeta
91
    {
92
        if ($this->serializerTypeDefinitionMapBuilder) {
93
            return new RequestMeta(
94
                $specification,
95
                $operation,
96
                $this->serializerTypeDefinitionMapBuilder->build($specification)
97
            );
98
        }
99
100
        return new RequestMeta($specification, $operation);
101
    }
102
}
103