1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusShippingExportPlugin\Controller; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusShippingExportPlugin\Event\ExportShipmentEvent; |
14
|
|
|
use BitBag\SyliusShippingExportPlugin\Repository\ShippingExportRepositoryInterface; |
15
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
16
|
|
|
use Sylius\Component\Resource\Model\ResourceInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Webmozart\Assert\Assert; |
20
|
|
|
|
21
|
|
|
final class ShippingExportController extends ResourceController |
22
|
|
|
{ |
23
|
|
|
/** @var ShippingExportRepositoryInterface */ |
24
|
|
|
protected $repository; |
25
|
|
|
|
26
|
|
|
public function exportAllNewShipmentsAction(Request $request): RedirectResponse |
27
|
|
|
{ |
28
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
29
|
|
|
|
30
|
|
|
$shippingExports = $this->repository->findAllWithNewOrPendingState(); |
31
|
|
|
|
32
|
|
|
if (0 === count($shippingExports)) { |
33
|
|
|
$this->addFlash('error', 'bitbag.ui.no_new_shipments_to_export'); |
34
|
|
|
|
35
|
|
|
return $this->redirectToReferer($request); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
foreach ($shippingExports as $shippingExport) { |
39
|
|
|
$this->eventDispatcher->dispatch( |
40
|
|
|
ExportShipmentEvent::SHORT_NAME, |
41
|
|
|
$configuration, |
42
|
|
|
$shippingExport |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->redirectToReferer($request); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function exportSingleShipmentAction(Request $request): RedirectResponse |
50
|
|
|
{ |
51
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
52
|
|
|
|
53
|
|
|
/** @var ResourceInterface|null $shippingExport */ |
54
|
|
|
$shippingExport = $this->repository->find($request->get('id')); |
55
|
|
|
Assert::notNull($shippingExport); |
56
|
|
|
|
57
|
|
|
$this->eventDispatcher->dispatch( |
58
|
|
|
ExportShipmentEvent::SHORT_NAME, |
59
|
|
|
$configuration, |
60
|
|
|
$shippingExport |
|
|
|
|
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return $this->redirectToReferer($request); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function redirectToReferer(Request $request): RedirectResponse |
67
|
|
|
{ |
68
|
|
|
$referer = $request->headers->get('referer'); |
69
|
|
|
if (null !== $referer) { |
|
|
|
|
70
|
|
|
return new RedirectResponse($referer); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->redirectToRoute($request->attributes->get('_route')); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|