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

api-platform / core / 14249622711

03 Apr 2025 05:58PM UTC coverage: 7.308%. Remained the same
14249622711

push

github

soyuka
docs: changelog 4.0.22

12514 of 171232 relevant lines covered (7.31%)

12.07 hits per line

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

86.36
/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\HttpOperation;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\UriVariablesConverterInterface;
22
use ApiPlatform\Metadata\Util\ClassInfoTrait;
23
use ApiPlatform\Metadata\Util\CloneTrait;
24
use ApiPlatform\State\ProcessorInterface;
25
use ApiPlatform\State\UriVariablesResolverTrait;
26
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
27
use ApiPlatform\State\Util\RequestAttributesExtractor;
28
use Symfony\Component\HttpKernel\Event\ViewEvent;
29
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
30

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

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

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

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

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

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

81
        $data = $this->processor->process($event->getControllerResult(), $operation, $uriVariables, [
103✔
82
            'request' => $request,
103✔
83
            'uri_variables' => $uriVariables,
103✔
84
            'resource_class' => $operation->getClass(),
103✔
85
            'previous_data' => false === $operation->canRead() ? null : $request->attributes->get('previous_data'),
103✔
86
        ]);
103✔
87

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

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