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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

0 of 1 new or added line in 1 file covered. (0.0%)

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

75.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\Api\IriConverterInterface as LegacyIriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface as LegacyResourceClassResolverInterface;
18
use ApiPlatform\Api\UriVariablesConverterInterface as LegacyUriVariablesConverterInterface;
19
use ApiPlatform\Exception\InvalidIdentifierException;
20
use ApiPlatform\Metadata\IriConverterInterface;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\Metadata\ResourceClassResolverInterface;
23
use ApiPlatform\Metadata\UriVariablesConverterInterface;
24
use ApiPlatform\Metadata\Util\ClassInfoTrait;
25
use ApiPlatform\State\ProcessorInterface;
26
use ApiPlatform\State\UriVariablesResolverTrait;
27
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
28
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\HttpKernel\Event\ViewEvent;
31
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
32

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

45
    public function __construct(
46
        private readonly ProcessorInterface $processor,
47
        private readonly LegacyIriConverterInterface|IriConverterInterface $iriConverter,
48
        private readonly ResourceClassResolverInterface|LegacyResourceClassResolverInterface $resourceClassResolver,
49
        ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
50
        LegacyUriVariablesConverterInterface|UriVariablesConverterInterface $uriVariablesConverter = null,
51
    ) {
52
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
18✔
53
        $this->uriVariablesConverter = $uriVariablesConverter;
18✔
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
        $controllerResult = $event->getControllerResult();
18✔
62
        $request = $event->getRequest();
18✔
63
        $operation = $this->initializeOperation($request);
18✔
64

65
        // API Platform 3.2 has a MainController where everything is handled by processors/providers
66
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
18✔
67
            return;
×
68
        }
69

70
        if (
71
            $controllerResult instanceof Response
18✔
72
            || $request->isMethodSafe()
18✔
73
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
18✔
74
        ) {
75
            return;
3✔
76
        }
77

78
        if (!$attributes['persist'] || !($operation?->canWrite() ?? true)) {
15✔
UNCOV
79
            return;
×
80
        }
81

82
        if (!$operation?->getProcessor()) {
15✔
83
            return;
×
84
        }
85

86
        $context = [
15✔
87
            'operation' => $operation,
15✔
88
            'resource_class' => $attributes['resource_class'],
15✔
89
            'previous_data' => $attributes['previous_data'] ?? null,
15✔
90
        ];
15✔
91

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

98
        switch ($request->getMethod()) {
15✔
99
            case 'PUT':
15✔
100
            case 'PATCH':
15✔
101
            case 'POST':
15✔
102
                $persistResult = $this->processor->process($controllerResult, $operation, $uriVariables, $context);
15✔
103

104
                if ($persistResult) {
15✔
105
                    $controllerResult = $persistResult;
15✔
106
                    $event->setControllerResult($controllerResult);
15✔
107
                }
108

109
                if ($controllerResult instanceof Response) {
15✔
110
                    break;
×
111
                }
112

113
                $outputMetadata = $operation->getOutput() ?? ['class' => $attributes['resource_class']];
15✔
114
                $hasOutput = \is_array($outputMetadata) && \array_key_exists('class', $outputMetadata) && null !== $outputMetadata['class'];
15✔
115
                if (!$hasOutput) {
15✔
UNCOV
116
                    break;
×
117
                }
118

119
                if ($this->resourceClassResolver->isResourceClass($this->getObjectClass($controllerResult))) {
15✔
120
                    $request->attributes->set('_api_write_item_iri', $this->iriConverter->getIriFromResource($controllerResult));
9✔
121
                }
122

123
                break;
15✔
UNCOV
124
            case 'DELETE':
×
UNCOV
125
                $this->processor->process($controllerResult, $operation, $uriVariables, $context);
×
UNCOV
126
                $event->setControllerResult(null);
×
UNCOV
127
                break;
×
128
        }
129
    }
130
}
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