Skip to content

Commit 5b4dfaf

Browse files
[Export] Extract checking if resource can be exported
1 parent 803bc23 commit 5b4dfaf

File tree

4 files changed

+75
-43
lines changed

4 files changed

+75
-43
lines changed

config/services.xml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
<argument type="service" id=".inner" />
9595
</service>
9696

97+
<service id="sylius_import_export.grid.checker.exportable" class="Sylius\GridImportExport\Grid\Checker\ExportableChecker">
98+
<argument type="service" id="request_stack" />
99+
<argument type="service" id="sylius.resource_registry" />
100+
<argument>%sylius_import_export.grid.export.allowed_sections%</argument>
101+
<argument>%sylius_import_export.grid.export.allowed_resources%</argument>
102+
</service>
103+
97104
<service
98105
id="sylius_import_export.grid.array_to_definition_converter.common_event_dispatching"
99106
class="Sylius\GridImportExport\Grid\Definition\CommonEventDispatchingArrayToDefinitionConverter"
@@ -103,11 +110,8 @@
103110
<argument type="service" id=".inner" />
104111
</service>
105112

106-
<service id="sylius_import_export.listener.grid.admin.export_action" class="Sylius\B2BKit\Grid\Listener\ExportActionAdminGridListener">
107-
<argument type="service" id="request_stack" />
108-
<argument type="service" id="sylius.resource_registry" />
109-
<argument>%sylius_import_export.grid.export.allowed_sections%</argument>
110-
<argument>%sylius_import_export.grid.export.allowed_resources%</argument>
113+
<service id="sylius_import_export.listener.grid.admin.export_action" class="Sylius\Grid\Listener\ExportActionAdminGridListener">
114+
<argument type="service" id="sylius_import_export.grid.checker.exportable" />
111115

112116
<tag name="kernel.event_listener" event="sylius.grid.common" method="addExportMainAction" />
113117
</service>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\GridImportExport\Grid\Checker;
6+
7+
use Sylius\Component\Grid\Definition\Grid;
8+
use Sylius\Resource\Metadata\RegistryInterface;
9+
use Symfony\Component\HttpFoundation\Request;
10+
use Symfony\Component\HttpFoundation\RequestStack;
11+
12+
final class ExportableChecker implements ExportableCheckerInterface
13+
{
14+
/**
15+
* @param array<array-key, string> $allowedSections
16+
* @param array<array-key, string> $allowedResources
17+
*/
18+
public function __construct(
19+
private RequestStack $requestStack,
20+
private RegistryInterface $resourceRegistry,
21+
private array $allowedSections,
22+
private array $allowedResources,
23+
) {
24+
}
25+
26+
public function canBeExported(Grid $grid): bool
27+
{
28+
$resourceClass = $grid->getDriverConfiguration()['class'] ?? null;
29+
if (null === $resourceClass) {
30+
return false;
31+
}
32+
33+
$request = $this->requestStack->getMainRequest();
34+
if (!$request instanceof Request) {
35+
return false;
36+
}
37+
38+
if (!$request->attributes->has('_sylius')) {
39+
return false;
40+
}
41+
42+
$syliusAttributes = $request->attributes->all()['_sylius'];
43+
if (!in_array($syliusAttributes['section'] ?? null, $this->allowedSections)) {
44+
return false;
45+
}
46+
47+
$resourceMetadata = $this->resourceRegistry->getByClass($resourceClass);
48+
49+
return in_array($resourceMetadata->getAlias(), $this->allowedResources);
50+
}
51+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\GridImportExport\Grid\Checker;
6+
7+
use Sylius\Component\Grid\Definition\Grid;
8+
9+
interface ExportableCheckerInterface
10+
{
11+
public function canBeExported(Grid $grid): bool;
12+
}

src/Grid/Listener/ExportActionAdminGridListener.php

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,13 @@
1616
use Sylius\Component\Grid\Definition\Action;
1717
use Sylius\Component\Grid\Definition\ActionGroup;
1818
use Sylius\Component\Grid\Event\GridDefinitionConverterEvent;
19-
use Sylius\Resource\Metadata\RegistryInterface;
2019
use Sylius\Bundle\GridBundle\Doctrine\ORM\Driver as ORMDriver;
21-
use Symfony\Component\HttpFoundation\Request;
22-
use Symfony\Component\HttpFoundation\RequestStack;
20+
use Sylius\GridImportExport\Grid\Checker\ExportableCheckerInterface;
2321

2422
final readonly class ExportActionAdminGridListener
2523
{
26-
/**
27-
* @param array<array-key, string> $allowedSections
28-
* @param array<array-key, string> $allowedResources
29-
*/
3024
public function __construct(
31-
private RequestStack $requestStack,
32-
private RegistryInterface $resourceRegistry,
33-
private array $allowedSections,
34-
private array $allowedResources,
25+
private ExportableCheckerInterface $exportableChecker,
3526
) {
3627
}
3728

@@ -42,7 +33,7 @@ public function addExportMainAction(GridDefinitionConverterEvent $event): void
4233
return;
4334
}
4435

45-
if (!$this->canBeExported($grid->getDriverConfiguration())) {
36+
if (!$this->exportableChecker->canBeExported($grid)) {
4637
return;
4738
}
4839

@@ -59,30 +50,4 @@ public function addExportMainAction(GridDefinitionConverterEvent $event): void
5950

6051
$actionGroup->addAction($action);
6152
}
62-
63-
private function canBeExported(array $driverConfiguration): bool
64-
{
65-
$resourceClass = $driverConfiguration['class'] ?? null;
66-
if (null === $resourceClass) {
67-
return false;
68-
}
69-
70-
$request = $this->requestStack->getMainRequest();
71-
if (!$request instanceof Request) {
72-
return false;
73-
}
74-
75-
if (!$request->attributes->has('_sylius')) {
76-
return false;
77-
}
78-
79-
$syliusAttributes = $request->attributes->all()['_sylius'];
80-
if (!in_array($syliusAttributes['section'] ?? null, $this->allowedSections)) {
81-
return false;
82-
}
83-
84-
$resourceMetadata = $this->resourceRegistry->getByClass($resourceClass);
85-
86-
return in_array($resourceMetadata->getAlias(), $this->allowedResources);
87-
}
8853
}

0 commit comments

Comments
 (0)