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

api-platform / core / 13724363992

07 Mar 2025 03:55PM UTC coverage: 8.174% (-0.3%) from 8.518%
13724363992

Pull #7004

github

web-flow
Merge c88b49ea9 into 1e0bc9dc8
Pull Request #7004: fix(doctrine): correct the use statement for ManagerRegistry

12837 of 157041 relevant lines covered (8.17%)

13.55 hits per line

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

94.44
/src/Symfony/EventListener/SerializeListener.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\Metadata\Error;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\State\ProcessorInterface;
19
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
20
use ApiPlatform\State\Util\RequestAttributesExtractor;
21
use Symfony\Component\HttpKernel\Event\ViewEvent;
22

23
/**
24
 * Serializes data.
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 */
28
final class SerializeListener
29
{
30
    use OperationRequestInitiatorTrait;
31

32
    /**
33
     * @param ProcessorInterface<mixed,mixed> $processor
34
     */
35
    public function __construct(private readonly ProcessorInterface $processor, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null)
36
    {
37
        $this->resourceMetadataCollectionFactory = $resourceMetadataFactory;
188✔
38
    }
39

40
    /**
41
     * Serializes the data to the requested format.
42
     */
43
    public function onKernelView(ViewEvent $event): void
44
    {
45
        $controllerResult = $event->getControllerResult();
188✔
46
        $request = $event->getRequest();
188✔
47
        $operation = $this->initializeOperation($request);
188✔
48

49
        $attributes = RequestAttributesExtractor::extractAttributes($request);
188✔
50

51
        if (!($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond', false)) || !$operation) {
188✔
52
            return;
×
53
        }
54

55
        if (null === $operation->canSerialize()) {
188✔
56
            $operation = $operation->withSerialize(true);
188✔
57
        }
58

59
        if ($operation instanceof Error) {
188✔
60
            // we don't want the FlattenException
61
            $controllerResult = $request->attributes->get('data') ?? $controllerResult;
23✔
62
        }
63

64
        $uriVariables = $request->attributes->get('_api_uri_variables') ?? [];
188✔
65
        $serialized = $this->processor->process($controllerResult, $operation, $uriVariables, [
188✔
66
            'request' => $request,
188✔
67
            'uri_variables' => $uriVariables,
188✔
68
            'resource_class' => $operation->getClass(),
188✔
69
        ]);
188✔
70

71
        $event->setControllerResult($serialized);
188✔
72
    }
73
}
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