Passed
Push — develop ( bcfe80...bd9059 )
by Daniel
05:18
created

LayoutDataProvider::getItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 4
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\DataProvider\Item;
4
5
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
6
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
7
use Doctrine\Common\Persistence\ManagerRegistry;
8
use Silverback\ApiComponentBundle\Entity\Layout\Layout;
9
10
final class LayoutDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
11
{
12
    /**
13
     * @var ManagerRegistry
14
     */
15
    private $managerRegistry;
16
17
    /**
18
     * LayoutDataProvider constructor.
19
     *
20
     * @param ManagerRegistry $managerRegistry
21
     */
22 4
    public function __construct(ManagerRegistry $managerRegistry)
23
    {
24 4
        $this->managerRegistry = $managerRegistry;
25 4
    }
26
27
    /**
28
     * @param string $resourceClass
29
     * @param string|null $operationName
30
     * @param array $context
31
     * @return bool
32
     */
33 4
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
34
    {
35 4
        return $resourceClass === Layout::class;
36
    }
37
38
    /**
39
     * @param string      $resourceClass
40
     * @param int|string  $id
41
     * @param string|null $operationName
42
     * @param array       $context
43
     * @return Layout|null
44
     */
45 1
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
46
    {
47 1
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
48 1
        if (null === $manager) {
49
            return null;
50
        }
51 1
        $repository = $manager->getRepository($resourceClass);
52 1
        return $id === 'default' ? $repository->findOneBy(['default' => true]) : $repository->find($id);
53
    }
54
}
55