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

api-platform / core / 10537652610

24 Aug 2024 10:04AM UTC coverage: 7.707%. Remained the same
10537652610

push

github

dunglas
cleanup

12490 of 162060 relevant lines covered (7.71%)

22.98 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;
92✔
53
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
92✔
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();
92✔
62
        $operation = $this->initializeOperation($request);
92✔
63

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

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

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

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

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