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

api-platform / core / 3712686585

pending completion
3712686585

Pull #5254

github

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

197 of 197 new or added lines in 5 files covered. (100.0%)

7493 of 12362 relevant lines covered (60.61%)

67.56 hits per line

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

0.0
/src/State/CreateProvider.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\State;
15

16
use ApiPlatform\Exception\RuntimeException;
17
use ApiPlatform\Metadata\Get;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Link;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\Metadata\Post;
22
use Symfony\Component\PropertyAccess\PropertyAccess;
23
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
24

25
/**
26
 * An ItemProvider for POST operations on generated subresources.
27
 *
28
 * @see ApiPlatform\Tests\Fixtures\TestBundle\Entity\SubresourceEmployee
29
 *
30
 * @author Antoine Bluchet <soyuka@gmail.com>
31
 *
32
 * @experimental
33
 */
34
final class CreateProvider implements ProviderInterface
35
{
36
    public function __construct(private ProviderInterface $decorated, private ?PropertyAccessorInterface $propertyAccessor = null)
37
    {
38
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
×
39
    }
40

41
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
42
    {
43
        if (!$uriVariables || !$operation instanceof HttpOperation || null !== $operation->getController()) {
×
44
            return $this->decorated->provide($operation, $uriVariables, $context);
×
45
        }
46

47
        $operationUriVariables = $operation->getUriVariables();
×
48
        $relationClass = current($operationUriVariables)->getFromClass();
×
49
        $key = key($operationUriVariables);
×
50
        $relationUriVariables = [];
×
51

52
        foreach ($operationUriVariables as $parameterName => $value) {
×
53
            if ($key === $parameterName) {
×
54
                $relationUriVariables['id'] = new Link(identifiers: $value->getIdentifiers(), fromClass: $value->getFromClass(), parameterName: $key);
×
55
                continue;
×
56
            }
57

58
            $relationUriVariables[$parameterName] = $value;
×
59
        }
60

61
        $relation = $this->decorated->provide(new Get(uriVariables: $relationUriVariables, class: $relationClass), $uriVariables);
×
62
        try {
×
63
            $resource = new ($operation->getClass());
×
64
        } catch (\Throwable $e) {
×
65
            throw new RuntimeException(sprintf('An error occurred while trying to create an instance of the "%s" resource. Consider writing your own "%s" implementation and setting it as `provider` on your operation instead.', $operation->getClass(), ProviderInterface::class), 0, $e);
×
66
        }
67
        $this->propertyAccessor->setValue($resource, $key, $relation);
×
68

69
        return $resource;
×
70
    }
71
}
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