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

api-platform / core / 17792263445

17 Sep 2025 08:54AM UTC coverage: 21.693% (-0.2%) from 21.902%
17792263445

push

github

soyuka
feat(state): remove @internal from the CreateProvider

fixes https://github.com/api-platform/api-platform/issues/571

11753 of 54178 relevant lines covered (21.69%)

12.64 hits per line

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

0.0
/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;
×
52
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
×
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();
×
61
        $operation = $this->initializeOperation($request);
×
62

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

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

71
        $uriVariables = $request->attributes->get('_api_uri_variables') ?? [];
×
72
        if (!$uriVariables && !$operation instanceof Error) {
×
73
            try {
74
                $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
×
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, [
×
81
            'request' => $request,
×
82
            'uri_variables' => $uriVariables,
×
83
            'resource_class' => $operation->getClass(),
×
84
            'previous_data' => false === $operation->canRead() ? null : $request->attributes->get('previous_data'),
×
85
        ]);
×
86

87
        if ($data) {
×
88
            $request->attributes->set('original_data', $data);
×
89
        }
90

91
        $event->setControllerResult($data);
×
92
    }
93
}
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