Passed
Push — develop ( c0a645...f538e8 )
by Daniel
05:28
created

LayoutDataProvider::getItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 2
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
    public function __construct(LayoutRepository $repository)
21
    {
22
        $this->repository = $repository;
23 5
    }
24
25 5
    /**
26 5
     * @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 4
    }
35
36 4
    /**
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
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Layout
45
    {
46
        if ($id !== 'default') {
47 1
            throw new ResourceClassNotSupportedException('LayoutDataProvider only supports the id `default`');
48
        }
49 1
        return $this->repository->findOneBy(['default' => true]);
50
    }
51
}
52