Passed
Push — develop ( 96281a...e6f82b )
by Daniel
05:53 queued 26s
created

LayoutDataProvider::getItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

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