Completed
Push — master ( b4619a...ab6ee6 )
by Kamil
12:06 queued 06:35
created

TaxonCollectionDataProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A supports() 0 4 1
A getCollection() 0 12 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ApiBundle\DataProvider;
15
16
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
17
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
18
use Sylius\Bundle\ApiBundle\Context\UserContextInterface;
19
use Sylius\Bundle\ApiBundle\Serializer\ContextKeys;
20
use Sylius\Component\Core\Model\TaxonInterface;
21
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
22
use Webmozart\Assert\Assert;
23
24
final class TaxonCollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
25
{
26
    /** @var TaxonRepositoryInterface */
27
    private $taxonRepository;
28
29
    /** @var UserContextInterface */
30
    private $userContext;
31
32
    public function __construct(TaxonRepositoryInterface $taxonRepository, UserContextInterface $userContext)
33
    {
34
        $this->taxonRepository = $taxonRepository;
35
        $this->userContext = $userContext;
36
    }
37
38
    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
39
    {
40
        return is_a($resourceClass, TaxonInterface::class, true);
41
    }
42
43
    public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
44
    {
45
        Assert::keyExists($context, ContextKeys::CHANNEL);
46
        $channelMenuTaxon = $context[ContextKeys::CHANNEL]->getMenuTaxon();
47
48
        $user = $this->userContext->getUser();
49
        if ($user !== null && in_array('ROLE_API_ACCESS', $user->getRoles())) {
50
            return $this->taxonRepository->findAll();
51
        }
52
53
        return $this->taxonRepository->findChildrenByChannelMenuTaxon($channelMenuTaxon);
54
    }
55
}
56