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

ResponseFactory::createResponse()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 0
Metric Value
c 7
b 1
f 0
dl 0
loc 34
rs 6.7272
cc 7
eloc 20
nc 20
nop 2
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\Response;
10
11
use KleijnWeb\SwaggerBundle\Document\DocumentRepository;
12
use KleijnWeb\SwaggerBundle\Document\Specification;
13
use KleijnWeb\SwaggerBundle\Request\RequestMeta;
14
use KleijnWeb\SwaggerBundle\Serialize\Serializer;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
/**
19
 * @author John Kleijn <[email protected]>
20
 */
21
class ResponseFactory
22
{
23
    /**
24
     * @var Serializer
25
     */
26
    private $serializer;
27
28
    /**
29
     * @var DocumentRepository
30
     */
31
    private $documentRepository;
32
33
    /**
34
     * @param DocumentRepository $documentRepository
35
     * @param Serializer         $serializer
36
     */
37
    public function __construct(
38
        DocumentRepository $documentRepository,
39
        Serializer $serializer
40
    ) {
41
        $this->serializer         = $serializer;
42
        $this->documentRepository = $documentRepository;
43
    }
44
45
    /**
46
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
47
     *
48
     * @param Request $request
49
     * @param mixed   $data
50
     *
51
     * @return Response
52
     */
53
    public function createResponse(Request $request, $data)
54
    {
55
        /** @var RequestMeta $meta */
56
        $meta = $request->attributes->get('_swagger.meta');
57
        $specification = $meta->getSpecification();
58
59
        if ($data !== null) {
60
            $data = $this->serializer->serialize($data, $meta->getDefinitionMap());
0 ignored issues
show
Bug introduced by
It seems like $meta->getDefinitionMap() can be null; however, serialize() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
61
        }
62
63
        $operation = $specification
64
            ->getOperation(
65
                $request->get('_swagger.path'),
66
                $request->getMethod()
67
            );
68
69
        $responseCode   = 200;
70
        $understands204 = false;
71
        foreach ($operation->getResponseCodes() as $statusCode) {
72
            if ($statusCode == 204) {
73
                $understands204 = true;
74
            }
75
            if (2 == substr($statusCode, 0, 1)) {
76
                $responseCode = $statusCode;
77
                break;
78
            }
79
        }
80
81
        if ($data === null && $understands204) {
82
            $responseCode = 204;
83
        }
84
85
        return new Response($data, $responseCode, ['Content-Type' => 'application/json']);
86
    }
87
}
88