Completed
Push — develop ( e326d5...b2cbdf )
by Daniel
09:21
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.1481

Importance

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