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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

93.18
/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\Api\IriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Api\UriVariablesConverterInterface;
19
use ApiPlatform\Exception\InvalidIdentifierException;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Util\ClassInfoTrait;
22
use ApiPlatform\State\ProcessorInterface;
23
use ApiPlatform\State\UriVariablesResolverTrait;
24
use ApiPlatform\Util\OperationRequestInitiatorTrait;
25
use ApiPlatform\Util\RequestAttributesExtractor;
26
use Symfony\Component\HttpFoundation\Response;
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 OperationRequestInitiatorTrait;
40
    use UriVariablesResolverTrait;
41

42
    public function __construct(
43
        private readonly ProcessorInterface $processor,
44
        private readonly IriConverterInterface $iriConverter,
45
        private readonly ResourceClassResolverInterface $resourceClassResolver,
46
        ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
47
        UriVariablesConverterInterface $uriVariablesConverter = null,
48
    ) {
49
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
38✔
50
        $this->uriVariablesConverter = $uriVariablesConverter;
38✔
51
    }
52

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

62
        // API Platform 3.2 has a MainController where everything is handled by processors/providers
63
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
38✔
64
            return;
×
65
        }
66

67
        if (
68
            $controllerResult instanceof Response
38✔
69
            || $request->isMethodSafe()
38✔
70
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
38✔
71
        ) {
72
            return;
20✔
73
        }
74

75
        if (!$attributes['persist'] || !($operation?->canWrite() ?? true)) {
18✔
76
            return;
3✔
77
        }
78

79
        if (!$operation?->getProcessor()) {
15✔
80
            return;
×
81
        }
82

83
        $context = [
15✔
84
            'operation' => $operation,
15✔
85
            'resource_class' => $attributes['resource_class'],
15✔
86
            'previous_data' => $attributes['previous_data'] ?? null,
15✔
87
        ];
15✔
88

89
        try {
90
            $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $attributes['resource_class']);
15✔
91
        } catch (InvalidIdentifierException $e) {
3✔
92
            throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
3✔
93
        }
94

95
        switch ($request->getMethod()) {
12✔
96
            case 'PUT':
12✔
97
            case 'PATCH':
12✔
98
            case 'POST':
12✔
99
                $persistResult = $this->processor->process($controllerResult, $operation, $uriVariables, $context);
9✔
100

101
                if ($persistResult) {
9✔
102
                    $controllerResult = $persistResult;
9✔
103
                    $event->setControllerResult($controllerResult);
9✔
104
                }
105

106
                if ($controllerResult instanceof Response) {
9✔
107
                    break;
×
108
                }
109

110
                $outputMetadata = $operation->getOutput() ?? ['class' => $attributes['resource_class']];
9✔
111
                $hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
9✔
112
                if (!$hasOutput) {
9✔
113
                    break;
3✔
114
                }
115

116
                if ($this->resourceClassResolver->isResourceClass($this->getObjectClass($controllerResult))) {
6✔
117
                    $request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromResource($controllerResult));
6✔
118
                }
119

120
                break;
6✔
121
            case 'DELETE':
3✔
122
                $this->processor->process($controllerResult, $operation, $uriVariables, $context);
3✔
123
                $event->setControllerResult(null);
3✔
124
                break;
3✔
125
        }
126
    }
127
}
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

© 2026 Coveralls, Inc