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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

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

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

43
    public const METHOD_TO_CODE = [
44
        'POST' => Response::HTTP_CREATED,
45
        'DELETE' => Response::HTTP_NO_CONTENT,
46
    ];
47

48
    public function __construct(
49
        private ?IriConverterInterface $iriConverter = null,
50
        private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
51
        private readonly ?OperationMetadataFactoryInterface $operationMetadataFactory = null,
52
    ) {
53
    }
991✔
54

55
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
56
    {
57
        if ($data instanceof Response || !$operation instanceof HttpOperation) {
984✔
58
            return $data;
15✔
59
        }
60

61
        if (!($request = $context['request'] ?? null)) {
971✔
62
            return $data;
×
63
        }
64

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

72
        $exception = $request->attributes->get('exception');
971✔
73
        if (($exception instanceof HttpExceptionInterface || $exception instanceof SymfonyHttpExceptionInterface) && $exceptionHeaders = $exception->getHeaders()) {
971✔
74
            $headers = array_merge($headers, $exceptionHeaders);
2✔
75
        }
76

77
        if ($operationHeaders = $operation->getHeaders()) {
971✔
78
            $headers = array_merge($headers, $operationHeaders);
3✔
79
        }
80

81
        $status = $operation->getStatus();
971✔
82

83
        if ($sunset = $operation->getSunset()) {
971✔
UNCOV
84
            $headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTimeInterface::RFC1123);
2✔
85
        }
86

87
        if ($acceptPatch = $operation->getAcceptPatch()) {
971✔
88
            $headers['Accept-Patch'] = $acceptPatch;
185✔
89
        }
90

91
        $method = $request->getMethod();
971✔
92
        $originalData = $context['original_data'] ?? null;
971✔
93

94
        $outputMetadata = $operation->getOutput() ?? ['class' => $operation->getClass()];
971✔
95
        $hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
971✔
96
        $hasData = !$hasOutput ? false : ($this->resourceClassResolver && $originalData && \is_object($originalData) && $this->resourceClassResolver->isResourceClass($this->getObjectClass($originalData)));
971✔
97

98
        if ($hasData) {
971✔
99
            $isAlternateResourceMetadata = $operation->getExtraProperties()['is_alternate_resource_metadata'] ?? false;
435✔
100
            $canonicalUriTemplate = $operation->getExtraProperties()['canonical_uri_template'] ?? null;
435✔
101

102
            if (
103
                !isset($headers['Location'])
435✔
104
                && 300 <= $status && $status < 400
435✔
105
                && ($isAlternateResourceMetadata || $canonicalUriTemplate)
435✔
106
            ) {
107
                $canonicalOperation = $operation;
2✔
108
                if ($this->operationMetadataFactory && null !== $canonicalUriTemplate) {
2✔
109
                    $canonicalOperation = $this->operationMetadataFactory->create($canonicalUriTemplate, $context);
2✔
110
                }
111

112
                if ($this->iriConverter) {
2✔
113
                    $headers['Location'] = $this->iriConverter->getIriFromResource($originalData, UrlGeneratorInterface::ABS_PATH, $canonicalOperation);
2✔
114
                }
115
            } elseif ('PUT' === $method && !$request->attributes->get('previous_data') && null === $status && ($operation instanceof Put && ($operation->getAllowCreate() ?? false))) {
435✔
UNCOV
116
                $status = 201;
2✔
117
            }
118
        }
119

120
        $status ??= self::METHOD_TO_CODE[$method] ?? 200;
971✔
121

122
        $requestParts = parse_url($request->getRequestUri());
971✔
123
        if ($this->iriConverter && !isset($headers['Content-Location'])) {
971✔
124
            try {
125
                $iri = null;
967✔
126
                if ($hasData) {
967✔
127
                    $iri = $this->iriConverter->getIriFromResource($originalData);
435✔
128
                } elseif ($operation->getClass()) {
559✔
129
                    $iri = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation);
549✔
130
                }
131

132
                if ($iri && 'GET' !== $method) {
913✔
133
                    $location = \sprintf('%s.%s', $iri, $request->getRequestFormat());
153✔
134
                    if (isset($requestParts['query'])) {
153✔
UNCOV
135
                        $location .= '?'.$requestParts['query'];
12✔
136
                    }
137

138
                    $headers['Content-Location'] = $location;
153✔
139
                    if ((201 === $status || (300 <= $status && $status < 400)) && 'POST' === $method && !isset($headers['Location'])) {
153✔
140
                        $headers['Location'] = $iri;
913✔
141
                    }
142
                }
143
            } catch (InvalidArgumentException|ItemNotFoundException|RuntimeException) {
55✔
144
            }
145
        }
146

147
        return new Response(
971✔
148
            $data,
971✔
149
            $status,
971✔
150
            $headers
971✔
151
        );
971✔
152
    }
153
}
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