Test Failed
Branch develop (ac2838)
by Daniel
09:09
created

LayoutDataProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getItem() 0 12 4
A __construct() 0 3 1
1
<?php
2
3
namespace Silverback\ApiComponentBundle\DataProvider\Item;
4
5
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
6
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
7
use Doctrine\Common\Persistence\ManagerRegistry;
8
use Doctrine\Common\Persistence\ObjectRepository;
9
use Silverback\ApiComponentBundle\Entity\Layout\Layout;
10
11
final class LayoutDataProvider implements ItemDataProviderInterface
12
{
13
    /**
14
     * @var ObjectRepository
15
     */
16
    private $managerRegistry;
17
18
    /**
19
     * LayoutDataProvider constructor.
20
     *
21
     * @param ManagerRegistry $managerRegistry
22
     */
23
    public function __construct(ManagerRegistry $managerRegistry)
24
    {
25
        $this->managerRegistry = $managerRegistry;
0 ignored issues
show
Documentation Bug introduced by
It seems like $managerRegistry of type Doctrine\Common\Persistence\ManagerRegistry is incompatible with the declared type Doctrine\Common\Persistence\ObjectRepository of property $managerRegistry.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
    }
27
28
    /**
29
     * @param string      $resourceClass
30
     * @param int|string  $id
31
     * @param string|null $operationName
32
     * @param array       $context
33
     *
34
     * @return Layout|null
35
     * @throws ResourceClassNotSupportedException
36
     */
37
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
38
    {
39
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
40
        if (
41
            null === $manager ||
42
            Layout::class !== $resourceClass ||
43
            $id !== 'default'
44
        ) {
45
            throw new ResourceClassNotSupportedException('This provider only supports getting the default layout');
46
        }
47
        $repository = $manager->getRepository($resourceClass);
48
        return $repository->findOneBy(['default' => true]);
49
    }
50
}
51