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

api-platform / core / 5942232691

22 Aug 2023 05:43PM UTC coverage: 58.819% (+0.001%) from 58.818%
5942232691

push

github

web-flow
fix(metadata): fix POST on subresource (#5734)

4 of 4 new or added lines in 1 file covered. (100.0%)

10841 of 18431 relevant lines covered (58.82%)

19.76 hits per line

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

90.48
/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\HttpKernel\Exception\NotFoundHttpException;
23
use Symfony\Component\PropertyAccess\PropertyAccess;
24
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
25

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

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

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

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

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

62
        $relation = $this->decorated->provide(new Get(uriVariables: $relationUriVariables, class: $relationClass), $uriVariables);
4✔
63
        if (!$relation) {
4✔
64
            throw new NotFoundHttpException('Not Found');
×
65
        }
66

67
        try {
68
            $resource = new ($operation->getClass());
4✔
69
        } catch (\Throwable $e) {
2✔
70
            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);
2✔
71
        }
72
        $property = $operationUriVariables[$key]->getToProperty() ?? $key;
2✔
73
        $this->propertyAccessor->setValue($resource, $property, $relation);
2✔
74

75
        return $resource;
2✔
76
    }
77
}
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