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

api-platform / core / 18967544912

31 Oct 2025 08:52AM UTC coverage: 24.603%. Remained the same
18967544912

push

github

web-flow
fix(symfony): add missing symfony/asset dependency (#7491)

14060 of 57147 relevant lines covered (24.6%)

26.32 hits per line

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

87.5
/src/Symfony/EventListener/WriteListener.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\Exception\InvalidIdentifierException;
18
use ApiPlatform\Metadata\Exception\InvalidUriVariableException;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\Metadata\UriVariablesConverterInterface;
21
use ApiPlatform\Metadata\Util\ClassInfoTrait;
22
use ApiPlatform\Metadata\Util\CloneTrait;
23
use ApiPlatform\State\ProcessorInterface;
24
use ApiPlatform\State\UriVariablesResolverTrait;
25
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
26
use ApiPlatform\State\Util\RequestAttributesExtractor;
27
use Symfony\Component\HttpKernel\Event\ViewEvent;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29

30
/**
31
 * Bridges persistence and the API system.
32
 *
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
35
 */
36
final class WriteListener
37
{
38
    use ClassInfoTrait;
39
    use CloneTrait;
40
    use OperationRequestInitiatorTrait;
41
    use UriVariablesResolverTrait;
42

43
    /**
44
     * @param ProcessorInterface<mixed, mixed> $processor
45
     */
46
    public function __construct(
47
        private readonly ProcessorInterface $processor,
48
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
49
        ?UriVariablesConverterInterface $uriVariablesConverter = null,
50
    ) {
51
        $this->uriVariablesConverter = $uriVariablesConverter;
274✔
52
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
274✔
53
    }
54

55
    /**
56
     * Persists, updates or delete data return by the controller if applicable.
57
     */
58
    public function onKernelView(ViewEvent $event): void
59
    {
60
        $request = $event->getRequest();
274✔
61
        $operation = $this->initializeOperation($request);
274✔
62

63
        if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !$attributes['persist'] || !$operation) {
274✔
64
            return;
×
65
        }
66

67
        if (null === $operation->canWrite()) {
274✔
68
            $operation = $operation->withWrite(!$request->isMethodSafe());
272✔
69
        }
70

71
        $uriVariables = $request->attributes->get('_api_uri_variables') ?? [];
274✔
72
        if (!$uriVariables && !$operation instanceof Error) {
274✔
73
            try {
74
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
170✔
75
            } catch (InvalidIdentifierException|InvalidUriVariableException $e) {
×
76
                throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
77
            }
78
        }
79

80
        $data = $this->processor->process($event->getControllerResult(), $operation, $uriVariables, [
274✔
81
            'request' => $request,
274✔
82
            'uri_variables' => $uriVariables,
274✔
83
            'resource_class' => $operation->getClass(),
274✔
84
            'previous_data' => false === $operation->canRead() ? null : $request->attributes->get('previous_data'), // this is a clone
274✔
85
            'read_data' => false === $operation->canRead() ? null : $request->attributes->get('read_data'), // this is what we read
274✔
86
            'data' => false === $operation->canRead() ? null : $request->attributes->get('data'), // this should be the same as getControllerResult but is the result of deserialization
274✔
87
        ]);
274✔
88

89
        if ($data) {
274✔
90
            $request->attributes->set('original_data', $data);
274✔
91
        }
92

93
        $event->setControllerResult($data);
274✔
94
    }
95
}
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