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\Event; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusShippingExportPlugin\Entity\ShippingExportInterface; |
14
|
|
|
use Doctrine\ORM\EntityManager; |
15
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
18
|
|
|
use Symfony\Contracts\EventDispatcher\Event; |
19
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
20
|
|
|
use Webmozart\Assert\Assert; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @deprecated The ExportShipmentEvent is deprecated since Sylius 1.8 and will be removed. Use \Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent instead |
24
|
|
|
*/ |
25
|
|
|
class ExportShipmentEvent extends Event |
26
|
|
|
{ |
27
|
|
|
public const NAME = 'bitbag.shipping_export.export_shipment'; |
28
|
|
|
|
29
|
|
|
public const SHORT_NAME = 'export_shipment'; |
30
|
|
|
|
31
|
|
|
/** @var ShippingExportInterface */ |
32
|
|
|
private $shippingExport; |
33
|
|
|
|
34
|
|
|
/** @var FlashBagInterface */ |
35
|
|
|
private $flashBag; |
36
|
|
|
|
37
|
|
|
/** @var EntityManagerInterface|EntityManager */ |
38
|
|
|
private $shippingExportManager; |
39
|
|
|
|
40
|
|
|
/** @var Filesystem */ |
41
|
|
|
private $filesystem; |
42
|
|
|
|
43
|
|
|
/** @var TranslatorInterface */ |
44
|
|
|
private $translator; |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
private $shippingLabelsPath; |
48
|
|
|
|
49
|
|
|
public function __construct( |
50
|
|
|
ShippingExportInterface $shippingExport, |
51
|
|
|
FlashBagInterface $flashBag, |
52
|
|
|
EntityManagerInterface $shippingExportManager, |
53
|
|
|
Filesystem $filesystem, |
54
|
|
|
TranslatorInterface $translator, |
55
|
|
|
string $shippingLabelsPath |
56
|
|
|
) { |
57
|
|
|
trigger_deprecation('', '', 'The ExportShipmentEvent is deprecated since Sylius 1.8 and will be removed. Use \Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent instead'); |
58
|
|
|
$this->shippingExport = $shippingExport; |
59
|
|
|
$this->flashBag = $flashBag; |
60
|
|
|
$this->shippingExportManager = $shippingExportManager; |
61
|
|
|
$this->shippingLabelsPath = $shippingLabelsPath; |
62
|
|
|
$this->filesystem = $filesystem; |
63
|
|
|
$this->translator = $translator; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getShippingExport(): ?ShippingExportInterface |
67
|
|
|
{ |
68
|
|
|
return $this->shippingExport; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function addSuccessFlash(string $messageId = 'bitbag.ui.shipment_data_has_been_exported'): void |
72
|
|
|
{ |
73
|
|
|
$message = $this->translator->trans($messageId); |
74
|
|
|
|
75
|
|
|
if (false === $this->flashBag->has('success')) { |
76
|
|
|
$this->flashBag->add('success', $message); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function addErrorFlash(string $messageId = 'bitbag.ui.shipping_export_error'): void |
81
|
|
|
{ |
82
|
|
|
$message = $this->translator->trans($messageId); |
83
|
|
|
|
84
|
|
|
if (false === $this->flashBag->has('error')) { |
85
|
|
|
$this->flashBag->add('error', $message); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function saveShippingLabel(string $labelContent, string $labelExtension): void |
90
|
|
|
{ |
91
|
|
|
$labelPath = $this->shippingLabelsPath |
92
|
|
|
. '/' . $this->getFilename() |
93
|
|
|
. '.' . $labelExtension; |
94
|
|
|
|
95
|
|
|
$this->filesystem->dumpFile($labelPath, $labelContent); |
96
|
|
|
$this->shippingExport->setLabelPath($labelPath); |
97
|
|
|
|
98
|
|
|
$this->shippingExportManager->persist($this->shippingExport); |
99
|
|
|
$this->shippingExportManager->flush(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function exportShipment(): void |
103
|
|
|
{ |
104
|
|
|
$this->shippingExport->setState(ShippingExportInterface::STATE_EXPORTED); |
105
|
|
|
$this->shippingExport->setExportedAt(new \DateTime()); |
106
|
|
|
|
107
|
|
|
$this->shippingExportManager->persist($this->shippingExport); |
108
|
|
|
$this->shippingExportManager->flush(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function getFilename(): string |
112
|
|
|
{ |
113
|
|
|
$shipment = $this->shippingExport->getShipment(); |
114
|
|
|
Assert::notNull($shipment); |
115
|
|
|
$order = $shipment->getOrder(); |
116
|
|
|
Assert::notNull($order); |
117
|
|
|
$orderNumber = $order->getNumber(); |
118
|
|
|
Assert::notNull($orderNumber); |
119
|
|
|
$shipmentId = $shipment->getId(); |
120
|
|
|
|
121
|
|
|
return implode('_', [ |
122
|
|
|
$shipmentId, |
123
|
|
|
preg_replace('~[^A-Za-z0-9]~', '', $orderNumber), |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|