• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.37
/src/Symfony/EventListener/RespondListener.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Symfony\EventListener;
15

16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Api\UrlGeneratorInterface;
18
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
19
use ApiPlatform\Metadata\Put;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Util\OperationRequestInitiatorTrait;
22
use ApiPlatform\Util\RequestAttributesExtractor;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpKernel\Event\ViewEvent;
25

26
/**
27
 * Builds the response object.
28
 *
29
 * @author Kévin Dunglas <dunglas@gmail.com>
30
 */
31
final class RespondListener
32
{
33
    use OperationRequestInitiatorTrait;
34

35
    public const METHOD_TO_CODE = [
36
        'POST' => Response::HTTP_CREATED,
37
        'DELETE' => Response::HTTP_NO_CONTENT,
38
    ];
39

40
    public function __construct(
41
        ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null,
42
        private readonly ?IriConverterInterface $iriConverter = null,
43
    ) {
44
        $this->resourceMetadataCollectionFactory = $resourceMetadataFactory;
47✔
45
    }
46

47
    /**
48
     * Creates a Response to send to the client according to the requested format.
49
     */
50
    public function onKernelView(ViewEvent $event): void
51
    {
52
        $request = $event->getRequest();
47✔
53
        $controllerResult = $event->getControllerResult();
47✔
54
        $operation = $this->initializeOperation($request);
47✔
55

56
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
47✔
57
            return;
×
58
        }
59

60
        $attributes = RequestAttributesExtractor::extractAttributes($request);
47✔
61

62
        if ($controllerResult instanceof Response && ($attributes['respond'] ?? false)) {
47✔
63
            $event->setResponse($controllerResult);
3✔
64

65
            return;
3✔
66
        }
67

68
        if ($controllerResult instanceof Response || !($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond'))) {
44✔
69
            return;
6✔
70
        }
71

72
        $headers = [
38✔
73
            'Content-Type' => sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
38✔
74
            'Vary' => 'Accept',
38✔
75
            'X-Content-Type-Options' => 'nosniff',
38✔
76
            'X-Frame-Options' => 'deny',
38✔
77
        ];
38✔
78

79
        $status = $operation?->getStatus();
38✔
80

81
        if ($sunset = $operation?->getSunset()) {
38✔
82
            $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
3✔
83
        }
84

85
        if ($acceptPatch = $operation?->getAcceptPatch()) {
38✔
86
            $headers['Accept-Patch'] = $acceptPatch;
5✔
87
        }
88

89
        $method = $request->getMethod();
38✔
90
        if (
91
            $this->iriConverter
38✔
92
            && $operation
93
            && ($operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false)
38✔
94
            && 301 === $operation->getStatus()
38✔
95
        ) {
96
            $status = 301;
×
97
            $headers['Location'] = $this->iriConverter->getIriFromResource($request->attributes->get('data'), UrlGeneratorInterface::ABS_PATH, $operation);
×
98
        } elseif ('PUT' === $method && !($attributes['previous_data'] ?? null) && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
38✔
99
            $status = Response::HTTP_CREATED;
×
100
        }
101

102
        $status ??= self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK;
38✔
103

104
        if ($request->attributes->has('_api_write_item_iri')) {
38✔
105
            $headers['Content-Location'] = $request->attributes->get('_api_write_item_iri');
12✔
106

107
            if ((Response::HTTP_CREATED === $status || (300 <= $status && $status < 400)) && 'POST' === $method) {
12✔
108
                $headers['Location'] = $request->attributes->get('_api_write_item_iri');
9✔
109
            }
110
        }
111

112
        if (($exception = $request->attributes->get('data')) instanceof HttpExceptionInterface) {
38✔
113
            $headers = array_merge($headers, $exception->getHeaders());
×
114
        }
115

116
        $event->setResponse(new Response(
38✔
117
            $controllerResult,
38✔
118
            $status,
38✔
119
            $headers
38✔
120
        ));
38✔
121
    }
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc