• 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

83.33
/src/State/Processor/RespondProcessor.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\State\Processor;
15

16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Api\UrlGeneratorInterface;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Put;
21
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22
use ApiPlatform\Metadata\Util\ClassInfoTrait;
23
use ApiPlatform\State\ProcessorInterface;
24
use ApiPlatform\Util\CloneTrait;
25
use Symfony\Component\HttpFoundation\Response;
26

27
/**
28
 * Serializes data.
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
final class RespondProcessor implements ProcessorInterface
33
{
34
    use ClassInfoTrait;
35
    use CloneTrait;
36

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

42
    public function __construct(private ?IriConverterInterface $iriConverter = null, private readonly ?ResourceClassResolverInterface $resourceClassResolver = null)
43
    {
44
    }
58✔
45

46
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
47
    {
48
        if ($data instanceof Response || !$operation instanceof HttpOperation) {
58✔
49
            return $data;
×
50
        }
51

52
        if (!($request = $context['request'] ?? null)) {
58✔
53
            return $data;
×
54
        }
55

56
        $headers = [
58✔
57
            'Content-Type' => sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
58✔
58
            'Vary' => 'Accept',
58✔
59
            'X-Content-Type-Options' => 'nosniff',
58✔
60
            'X-Frame-Options' => 'deny',
58✔
61
        ];
58✔
62

63
        $status = $operation->getStatus();
58✔
64

65
        if ($sunset = $operation->getSunset()) {
58✔
66
            $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123);
×
67
        }
68

69
        if ($acceptPatch = $operation->getAcceptPatch()) {
58✔
70
            $headers['Accept-Patch'] = $acceptPatch;
10✔
71
        }
72

73
        $method = $request->getMethod();
58✔
74
        $originalData = $context['original_data'] ?? null;
58✔
75

76
        if ($hasData = ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData))) && $this->iriConverter) {
58✔
77
            if (
78
                ($operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false)
12✔
79
                && 301 === $operation->getStatus()
12✔
80
            ) {
81
                $status = 301;
×
82
                $headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $operation);
×
83
            } elseif ('PUT' === $method && !$request->attributes->get('previous_data') && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
12✔
84
                $status = 201;
×
85
            }
86
        }
87

88
        $status ??= self::METHOD_TO_CODE[$method] ?? 200;
58✔
89

90
        if ($hasData && $this->iriConverter) {
58✔
91
            $iri = $this->iriConverter->getIriFromResource($originalData);
12✔
92
            $headers['Content-Location'] = $iri;
12✔
93

94
            if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method) {
12✔
95
                $headers['Location'] = $iri;
6✔
96
            }
97
        }
98

99
        return new Response(
58✔
100
            $data,
58✔
101
            $status,
58✔
102
            $headers
58✔
103
        );
58✔
104
    }
105
}
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