Passed
Push — develop ( 96281a...e6f82b )
by Daniel
05:53 queued 26s
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 supports() 0 3 1
A getItem() 0 6 2
A __construct() 0 3 1
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