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

api-platform / core / 10367357205

13 Aug 2024 09:38AM UTC coverage: 7.027% (-0.8%) from 7.84%
10367357205

push

github

soyuka
chore: align dependencies across components

11371 of 161813 relevant lines covered (7.03%)

22.93 hits per line

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

75.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\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\State\Exception\ProviderNotFoundException;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
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
 * @internal
35
 */
36
final class CreateProvider implements ProviderInterface
37
{
38
    public function __construct(
39
        private ProviderInterface $decorated,
40
        private ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
41
        private ?PropertyAccessorInterface $propertyAccessor = null,
42
    ) {
43
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
4✔
44
    }
45

46
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?object
47
    {
48
        if (!$uriVariables || !$operation instanceof HttpOperation || (null !== $operation->getController() && 'api_platform.symfony.main_controller' !== $operation->getController())) {
4✔
49
            return $this->decorated->provide($operation, $uriVariables, $context);
×
50
        }
51

52
        $operationUriVariables = $operation->getUriVariables();
4✔
53
        $relationClass = current($operationUriVariables)->getFromClass();
4✔
54
        $key = key($operationUriVariables);
4✔
55

56
        $parentOperation = $this->resourceMetadataCollectionFactory
4✔
57
            ->create($relationClass)
4✔
58
            ->getOperation($operation->getExtraProperties()['parent_uri_template'] ?? null);
4✔
59
        try {
60
            $relation = $this->decorated->provide($parentOperation, $uriVariables, $context);
4✔
61
        } catch (ProviderNotFoundException) {
×
62
            $relation = null;
×
63
        }
64
        if (!$relation) {
4✔
65
            throw new NotFoundHttpException('Not Found');
3✔
66
        }
67

68
        try {
69
            $resource = new ($operation->getClass());
4✔
70
        } catch (\Throwable $e) {
×
71
            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);
×
72
        }
73

74
        $property = $operationUriVariables[$key]->getToProperty() ?? $key;
4✔
75
        $this->propertyAccessor->setValue($resource, $property, $relation);
4✔
76

77
        return $resource;
4✔
78
    }
79
}
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