Passed
Push — feature/uploadable ( 7c6d25...a7ed20 )
by Daniel
11:07
created

LayoutDataProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 22
ccs 0
cts 9
cp 0
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getItem() 0 7 2
A supports() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\DataProvider\Item;
15
16
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
17
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
18
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
19
use Silverback\ApiComponentsBundle\Entity\Core\Layout;
20
use Silverback\ApiComponentsBundle\Repository\Core\LayoutRepository;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
final class LayoutDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
26
{
27
    private LayoutRepository $repository;
28
29
    public function __construct(LayoutRepository $repository)
30
    {
31
        $this->repository = $repository;
32
    }
33
34
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
35
    {
36
        return Layout::class === $resourceClass;
37
    }
38
39
    /** @throws ResourceClassNotSupportedException */
40
    public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Layout
41
    {
42
        if ('default' !== $id) {
43
            throw new ResourceClassNotSupportedException('LayoutDataProvider only supports the id `default`');
44
        }
45
46
        return $this->repository->findOneBy(['default' => true]);
47
    }
48
}
49