Passed
Push — develop ( 75d1fe...ccc913 )
by Daniel
06:14
created

LayoutDataProvider::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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