Passed
Push — develop ( b22678...97e476 )
by Daniel
08:45
created

LayoutDataProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 39
ccs 6
cts 9
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supports() 0 3 1
A getItem() 0 6 2
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
     * @param LayoutRepository $repository
21
     */
22 1
    public function __construct(LayoutRepository $repository)
23
    {
24 1
        $this->repository = $repository;
25 1
    }
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
     * @throws ResourceClassNotSupportedException
45
     */
46 1
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Layout
47
    {
48 1
        if ($id !== 'default') {
49
            throw new ResourceClassNotSupportedException('LayoutDataProvider only supports the id `default`');
50
        }
51 1
        return $this->repository->findOneBy(['default' => true]);
52
    }
53
}
54