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

api-platform / core / 10814494863

11 Sep 2024 03:11PM UTC coverage: 7.008% (-0.7%) from 7.679%
10814494863

push

github

web-flow
fix(state): remove resource_class change (#6607)

11516 of 164321 relevant lines covered (7.01%)

22.85 hits per line

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

95.74
/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\Metadata\Exception\HttpExceptionInterface;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\IriConverterInterface;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
21
use ApiPlatform\Metadata\Put;
22
use ApiPlatform\Metadata\ResourceClassResolverInterface;
23
use ApiPlatform\Metadata\UrlGeneratorInterface;
24
use ApiPlatform\Metadata\Util\ClassInfoTrait;
25
use ApiPlatform\Metadata\Util\CloneTrait;
26
use ApiPlatform\State\ProcessorInterface;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
29

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

40
    public const METHOD_TO_CODE = [
41
        'POST' => Response::HTTP_CREATED,
42
        'DELETE' => Response::HTTP_NO_CONTENT,
43
    ];
44

45
    public function __construct(
46
        private ?IriConverterInterface $iriConverter = null,
47
        private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
48
        private readonly ?OperationMetadataFactoryInterface $operationMetadataFactory = null,
49
    ) {
50
    }
1,851✔
51

52
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
53
    {
54
        if ($data instanceof Response || !$operation instanceof HttpOperation) {
1,839✔
55
            return $data;
16✔
56
        }
57

58
        if (!($request = $context['request'] ?? null)) {
1,823✔
59
            return $data;
×
60
        }
61

62
        $headers = [
1,823✔
63
            'Content-Type' => \sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())),
1,823✔
64
            'Vary' => 'Accept',
1,823✔
65
            'X-Content-Type-Options' => 'nosniff',
1,823✔
66
            'X-Frame-Options' => 'deny',
1,823✔
67
        ];
1,823✔
68

69
        $exception = $request->attributes->get('exception');
1,823✔
70
        if (($exception instanceof HttpExceptionInterface || $exception instanceof SymfonyHttpExceptionInterface) && $exceptionHeaders = $exception->getHeaders()) {
1,823✔
71
            $headers = array_merge($headers, $exceptionHeaders);
2✔
72
        }
73

74
        if ($operationHeaders = $operation->getHeaders()) {
1,823✔
75
            $headers = array_merge($headers, $operationHeaders);
3✔
76
        }
77

78
        $status = $operation->getStatus();
1,823✔
79

80
        if ($sunset = $operation->getSunset()) {
1,823✔
81
            $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTimeInterface::RFC1123);
6✔
82
        }
83

84
        if ($acceptPatch = $operation->getAcceptPatch()) {
1,823✔
85
            $headers['Accept-Patch'] = $acceptPatch;
535✔
86
        }
87

88
        $method = $request->getMethod();
1,823✔
89
        $originalData = $context['original_data'] ?? null;
1,823✔
90

91
        $outputMetadata = $operation->getOutput() ?? ['class' => $operation->getClass()];
1,823✔
92
        $hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
1,823✔
93
        $hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));
1,823✔
94

95
        if ($hasData && $this->iriConverter) {
1,823✔
96
            if (
97
                !isset($headers['Location'])
999✔
98
                && 300 <= $status && $status < 400
999✔
99
                && (($operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false) || ($operation->getExtraProperties()['canonical_uri_template'] ?? null))
999✔
100
            ) {
101
                $canonicalOperation = $operation;
2✔
102
                if ($this->operationMetadataFactory && null !== ($operation->getExtraProperties()['canonical_uri_template'] ?? null)) {
2✔
103
                    $canonicalOperation = $this->operationMetadataFactory->create($operation->getExtraProperties()['canonical_uri_template'], $context);
×
104
                }
105

106
                $headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $canonicalOperation);
2✔
107
            } elseif ('PUT' === $method && !$request->attributes->get('previous_data') && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
997✔
108
                $status = 201;
7✔
109
            }
110
        }
111

112
        $status ??= self::METHOD_TO_CODE[$method] ?? 200;
1,823✔
113

114
        if ($hasData && $this->iriConverter && !isset($headers['Content-Location'])) {
1,823✔
115
            $iri = $this->iriConverter->getIriFromResource($originalData);
999✔
116
            $headers['Content-Location'] = $iri;
999✔
117

118
            if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
999✔
119
                $headers['Location'] = $iri;
367✔
120
            }
121
        }
122

123
        return new Response(
1,823✔
124
            $data,
1,823✔
125
            $status,
1,823✔
126
            $headers
1,823✔
127
        );
1,823✔
128
    }
129
}
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

© 2025 Coveralls, Inc