Passed
Push — develop ( 74761d...9bb873 )
by Daniel
06:04
created

CollectionDataProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 6
cts 15
cp 0.4
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getItem() 0 14 3
A __construct() 0 4 1
A supports() 0 3 1
1
<?php
2
3
namespace Silverback\ApiComponentBundle\DataProvider\Item;
4
5
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
6
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
7
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
8
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
9
use Doctrine\Common\Persistence\ManagerRegistry;
10
use Silverback\ApiComponentBundle\Entity\Content\Component\Collection\Collection;
11
12
final class CollectionDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
13
{
14
    /** @var ManagerRegistry */
15
    private $managerRegistry;
16
    /** @var ContextAwareCollectionDataProviderInterface  */
17
    private $collectionDataProvider;
18
19
    /**
20
     * LayoutDataProvider constructor.
21
     *
22
     * @param ManagerRegistry $managerRegistry
23
     * @param ContextAwareCollectionDataProviderInterface $collectionDataProvider
24
     */
25 3
    public function __construct(ManagerRegistry $managerRegistry, ContextAwareCollectionDataProviderInterface $collectionDataProvider)
26
    {
27 3
        $this->managerRegistry = $managerRegistry;
28 3
        $this->collectionDataProvider = $collectionDataProvider;
29 3
    }
30
31
    /**
32
     * @param string $resourceClass
33
     * @param string|null $operationName
34
     * @param array $context
35
     * @return bool
36
     */
37 4
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
38
    {
39 4
        return $resourceClass === Collection::class;
40
    }
41
42
    /**
43
     * @param string $resourceClass
44
     * @param int|string $id
45
     * @param string|null $operationName
46
     * @param array $context
47
     * @return Collection|null
48
     * @throws ResourceClassNotSupportedException
49
     */
50
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
51
    {
52
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
53
        if (null === $manager) {
54
            throw new ResourceClassNotSupportedException(sprintf('No manager for the class `%s`', $resourceClass));
55
        }
56
        $repository = $manager->getRepository($resourceClass);
57
        /** @var Collection|null $collection */
58
        $collection = $repository->find($id);
59
        if ($collection) {
60
            // In future we should find the resource's data provider in case it isn't default
61
            $collection->setCollection($this->collectionDataProvider->getCollection($collection->getResource(), $operationName, $context));
62
        }
63
        return $collection;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $collection also could return the type Silverback\ApiComponentB...t\Collection\Collection which is incompatible with the return type mandated by ApiPlatform\Core\DataPro...derInterface::getItem() of object|null.
Loading history...
64
    }
65
}
66