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 = []) |
|
|
|
|
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
|
|
|
|
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.