|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 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\GridImportExport\Grid\Checker; |
| 15 | + |
| 16 | +use Sylius\Component\Grid\Definition\Grid; |
| 17 | +use Sylius\Resource\Metadata\RegistryInterface; |
| 18 | +use Symfony\Component\HttpFoundation\Request; |
| 19 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 20 | + |
| 21 | +final class ExportableChecker implements ExportableCheckerInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @param array<array-key, string> $allowedSections |
| 25 | + * @param array<array-key, string> $allowedResources |
| 26 | + */ |
| 27 | + public function __construct( |
| 28 | + private RequestStack $requestStack, |
| 29 | + private RegistryInterface $resourceRegistry, |
| 30 | + private array $allowedSections, |
| 31 | + private array $allowedResources, |
| 32 | + ) { |
| 33 | + } |
| 34 | + |
| 35 | + public function canBeExported(Grid $grid): bool |
| 36 | + { |
| 37 | + $resourceClass = $grid->getDriverConfiguration()['class'] ?? null; |
| 38 | + if (null === $resourceClass) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + $request = $this->requestStack->getMainRequest(); |
| 43 | + if (!$request instanceof Request) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + if (!$request->attributes->has('_sylius')) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + $syliusAttributes = $request->attributes->all()['_sylius']; |
| 52 | + if (!in_array($syliusAttributes['section'] ?? null, $this->allowedSections)) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + $resourceMetadata = $this->resourceRegistry->getByClass($resourceClass); |
| 57 | + |
| 58 | + return in_array($resourceMetadata->getAlias(), $this->allowedResources); |
| 59 | + } |
| 60 | +} |
0 commit comments