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

api-platform / core / 3712739783

pending completion
3712739783

Pull #5254

github

GitHub
Merge 9dfa88fa6 into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

199 of 199 new or added lines in 6 files covered. (100.0%)

7494 of 12363 relevant lines covered (60.62%)

67.55 hits per line

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

86.84
/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\State\ProcessorInterface;
22
use ApiPlatform\State\UriVariablesResolverTrait;
23
use ApiPlatform\Util\ClassInfoTrait;
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 const OPERATION_ATTRIBUTE_KEY = 'write';
43

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

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

59
        if (
60
            $controllerResult instanceof Response
460✔
61
            || $request->isMethodSafe()
460✔
62
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
460✔
63
        ) {
64
            return;
319✔
65
        }
66

67
        if (!($operation?->canWrite() ?? true) || !$attributes['persist']) {
144✔
68
            return;
×
69
        }
70

71
        if (!$operation?->getProcessor()) {
144✔
72
            return;
×
73
        }
74

75
        $context = ['operation' => $operation, 'resource_class' => $attributes['resource_class'], 'previous_data' => $attributes['previous_data'] ?? null];
144✔
76
        try {
77
            $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $attributes['resource_class']);
144✔
78
        } catch (InvalidIdentifierException $e) {
×
79
            throw new NotFoundHttpException('Invalid identifier value or configuration.', $e);
×
80
        }
81

82
        switch ($request->getMethod()) {
144✔
83
            case 'PUT':
144✔
84
            case 'PATCH':
121✔
85
            case 'POST':
117✔
86
                $persistResult = $this->processor->process($controllerResult, $operation, $uriVariables, $context);
138✔
87

88
                if ($persistResult) {
137✔
89
                    $controllerResult = $persistResult;
137✔
90
                    $event->setControllerResult($controllerResult);
137✔
91
                }
92

93
                if ($controllerResult instanceof Response) {
137✔
94
                    break;
×
95
                }
96

97
                $outputMetadata = $operation->getOutput() ?? ['class' => $attributes['resource_class']];
137✔
98
                $hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
137✔
99
                if (!$hasOutput) {
137✔
100
                    break;
2✔
101
                }
102

103
                if ($this->resourceClassResolver->isResourceClass($this->getObjectClass($controllerResult))) {
135✔
104
                    $request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromResource($controllerResult));
129✔
105
                }
106

107
                break;
135✔
108
            case 'DELETE':
6✔
109
                $this->processor->process($controllerResult, $operation, $uriVariables, $context);
6✔
110
                $event->setControllerResult(null);
6✔
111
                break;
6✔
112
        }
113
    }
114
}
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