• 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

96.97
/src/State/Provider/ContentNegotiationProvider.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\Provider;
15

16
use ApiPlatform\Metadata\Error as ErrorOperation;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
20
use ApiPlatform\State\ProviderInterface;
21
use Negotiation\Negotiator;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
24

25
final class ContentNegotiationProvider implements ProviderInterface
26
{
27
    use ContentNegotiationTrait;
28

29
    /**
30
     * @param array<string, string[]> $formats
31
     * @param array<string, string[]> $errorFormats
32
     */
33
    public function __construct(private readonly ?ProviderInterface $decorated = null, ?Negotiator $negotiator = null, private readonly array $formats = [], private readonly array $errorFormats = [])
34
    {
35
        $this->negotiator = $negotiator ?? new Negotiator();
1,010✔
36
    }
37

38
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
39
    {
40
        if (!($request = $context['request'] ?? null) || !$operation instanceof HttpOperation) {
997✔
41
            return $this->decorated?->provide($operation, $uriVariables, $context);
×
42
        }
43

44
        $isErrorOperation = $operation instanceof ErrorOperation;
997✔
45

46
        $formats = $operation->getOutputFormats() ?? ($isErrorOperation ? $this->errorFormats : $this->formats);
997✔
47
        $this->addRequestFormats($request, $formats);
997✔
48
        $request->attributes->set('input_format', $this->getInputFormat($operation, $request));
997✔
49
        $request->setRequestFormat($this->getRequestFormat($request, $formats, !$isErrorOperation));
995✔
50

51
        return $this->decorated?->provide($operation, $uriVariables, $context);
995✔
52
    }
53

54
    /**
55
     * Adds the supported formats to the request.
56
     *
57
     * This is necessary for {@see Request::getMimeType} and {@see Request::getMimeTypes} to work.
58
     * Note that this replaces default mime types configured at {@see Request::initializeFormats}
59
     *
60
     * @param array<string, string|string[]> $formats
61
     */
62
    private function addRequestFormats(Request $request, array $formats): void
63
    {
64
        foreach ($formats as $format => $mimeTypes) {
997✔
65
            $request->setFormat($format, (array) $mimeTypes);
997✔
66
        }
67
    }
68

69
    /**
70
     * Flattened the list of MIME types.
71
     *
72
     * @param array<string, string|string[]> $formats
73
     *
74
     * @return array<string, string>
75
     */
76
    private function flattenMimeTypes(array $formats): array
77
    {
78
        $flattenedMimeTypes = [];
995✔
79
        foreach ($formats as $format => $mimeTypes) {
995✔
80
            foreach ($mimeTypes as $mimeType) {
995✔
81
                $flattenedMimeTypes[$mimeType] = $format;
995✔
82
            }
83
        }
84

85
        return $flattenedMimeTypes;
995✔
86
    }
87

88
    /**
89
     * Extracts the format from the Content-Type header and check that it is supported.
90
     *
91
     * @throws UnsupportedMediaTypeHttpException
92
     */
93
    private function getInputFormat(HttpOperation $operation, Request $request): ?string
94
    {
95
        if (
96
            false === ($input = $operation->getInput())
997✔
97
            || (\is_array($input) && null === $input['class'])
997✔
98
            || false === $operation->canDeserialize()
997✔
99
        ) {
100
            return null;
595✔
101
        }
102

103
        $contentType = $request->headers->get('CONTENT_TYPE');
438✔
104
        if (null === $contentType || '' === $contentType) {
438✔
105
            return null;
242✔
106
        }
107

108
        /** @var string $contentType */
109
        $formats = $operation->getInputFormats() ?? [];
196✔
110
        if ($format = $this->getMimeTypeFormat($contentType, $formats)) {
196✔
111
            return $format;
191✔
112
        }
113

114
        if (!$request->isMethodSafe() && 'DELETE' !== $request->getMethod()) {
5✔
115
            $supportedMimeTypes = [];
3✔
116
            foreach ($formats as $mimeTypes) {
3✔
UNCOV
117
                foreach ($mimeTypes as $mimeType) {
1✔
UNCOV
118
                    $supportedMimeTypes[] = $mimeType;
1✔
119
                }
120
            }
121

122
            throw new UnsupportedMediaTypeHttpException(\sprintf('The content-type "%s" is not supported. Supported MIME types are "%s".', $contentType, implode('", "', $supportedMimeTypes)));
3✔
123
        }
124

125
        return null;
2✔
126
    }
127
}
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