14
14
use BitBag \SyliusShippingExportPlugin \Entity \ShippingExportInterface ;
15
15
use Doctrine \Persistence \ObjectManager ;
16
16
use DPD \Services \DPDService ;
17
- use http \ Exception \ InvalidArgumentException ;
17
+ use Exception ;
18
18
use Sylius \Bundle \ResourceBundle \Event \ResourceControllerEvent ;
19
19
use Symfony \Component \Filesystem \Filesystem ;
20
- use Symfony \Component \HttpFoundation \Session \Flash \FlashBagInterface ;
20
+ use Symfony \Component \HttpFoundation \RequestStack ;
21
+ use Symfony \Component \HttpFoundation \Session \Session ;
21
22
use Webmozart \Assert \Assert ;
22
23
23
24
final class ShippingExportEventListener
@@ -26,30 +27,25 @@ final class ShippingExportEventListener
26
27
27
28
public const BASE_LABEL_EXTENSION = 'pdf ' ;
28
29
29
- /** @var WebClientInterface */
30
- private $ webClient ;
30
+ private WebClientInterface $ webClient ;
31
31
32
- /** @var FlashBagInterface */
33
- private $ flashBag ;
32
+ private RequestStack $ requestStack ;
34
33
35
- /** @var Filesystem */
36
- private $ fileSystem ;
34
+ private Filesystem $ fileSystem ;
37
35
38
- /** @var ObjectManager */
39
- private $ shippingExportManager ;
36
+ private ObjectManager $ shippingExportManager ;
40
37
41
- /** @var string */
42
- private $ shippingLabelsPath ;
38
+ private string $ shippingLabelsPath ;
43
39
44
40
public function __construct (
45
41
WebClientInterface $ webClient ,
46
- FlashBagInterface $ flashBag ,
42
+ RequestStack $ requestStack ,
47
43
FileSystem $ fileSystem ,
48
44
ObjectManager $ shippingExportManager ,
49
45
string $ shippingLabelsPath
50
46
) {
51
47
$ this ->webClient = $ webClient ;
52
- $ this ->flashBag = $ flashBag ;
48
+ $ this ->requestStack = $ requestStack ;
53
49
$ this ->fileSystem = $ fileSystem ;
54
50
$ this ->shippingExportManager = $ shippingExportManager ;
55
51
$ this ->shippingLabelsPath = $ shippingLabelsPath ;
@@ -75,6 +71,8 @@ public function exportShipment(ResourceControllerEvent $exportShipmentEvent): vo
75
71
Assert::notNull ($ shipment );
76
72
77
73
$ this ->webClient ->setShipment ($ shipment );
74
+ /** @var Session $session */
75
+ $ session = $ this ->requestStack ->getSession ();
78
76
79
77
try {
80
78
$ dpd = new DPDService (
@@ -89,8 +87,8 @@ public function exportShipment(ResourceControllerEvent $exportShipmentEvent): vo
89
87
$ result = $ dpd ->sendPackage ($ this ->webClient ->getParcels (), $ this ->webClient ->getReceiver (), 'SENDER ' , $ this ->webClient ->getServices ());
90
88
91
89
$ speedLabel = $ dpd ->generateSpeedLabelsByPackageIds ([$ result ->packageId ], $ this ->webClient ->getPickupAddress ()); /** @phpstan-ignore-line */
92
- } catch (\ Exception $ exception ) {
93
- $ this -> flashBag ->add ('error ' , sprintf (
90
+ } catch (Exception $ exception ) {
91
+ $ session -> getFlashBag () ->add ('error ' , sprintf (
94
92
'DPD Web Service for #%s order: %s ' ,
95
93
$ shipment ->getOrder () !== null ? (string ) $ shipment ->getOrder ()->getNumber () : '' ,
96
94
$ exception ->getMessage ()
@@ -99,7 +97,7 @@ public function exportShipment(ResourceControllerEvent $exportShipmentEvent): vo
99
97
return ;
100
98
}
101
99
102
- $ this -> flashBag ->add ('success ' , 'bitbag.ui.shipment_data_has_been_exported ' );
100
+ $ session -> getFlashBag () ->add ('success ' , 'bitbag.ui.shipment_data_has_been_exported ' );
103
101
$ this ->saveShippingLabel ($ shippingExport , $ speedLabel ->filedata , self ::BASE_LABEL_EXTENSION ); /** @phpstan-ignore-line */
104
102
$ this ->markShipmentAsExported ($ shippingExport );
105
103
}
0 commit comments