Passed
Pull Request — master (#20)
by
unknown
07:09
created

ExportShipmentEvent::exportShipment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file has been created by the developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusShippingExportPlugin\Event;
14
15
use BitBag\SyliusShippingExportPlugin\Entity\ShippingExportInterface;
16
use Doctrine\ORM\EntityManager;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Symfony\Component\Filesystem\Filesystem;
19
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
20
use Symfony\Contracts\EventDispatcher\Event;
21
use Symfony\Contracts\Translation\TranslatorInterface;
22
use Webmozart\Assert\Assert;
23
24
/**
25
 * @deprecated The ExportShipmentEvent is deprecated since Sylius 1.8 and will be removed. Use \Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent instead
26
 */
27
class ExportShipmentEvent extends Event
28
{
29
    public const NAME = 'bitbag.shipping_export.export_shipment';
30
31
    public const SHORT_NAME = 'export_shipment';
32
33
    /** @var ShippingExportInterface */
34
    private $shippingExport;
35
36
    /** @var FlashBagInterface */
37
    private $flashBag;
38
39
    /** @var EntityManagerInterface|EntityManager */
40
    private $shippingExportManager;
41
42
    /** @var Filesystem */
43
    private $filesystem;
44
45
    /** @var TranslatorInterface */
46
    private $translator;
47
48
    /** @var string */
49
    private $shippingLabelsPath;
50
51
    public function __construct(
52
        ShippingExportInterface $shippingExport,
53
        FlashBagInterface $flashBag,
54
        EntityManagerInterface $shippingExportManager,
55
        Filesystem $filesystem,
56
        TranslatorInterface $translator,
57
        string $shippingLabelsPath
58
    ) {
59
        trigger_deprecation('', '', 'The ExportShipmentEvent is deprecated since Sylius 1.8 and will be removed. Use \Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent instead');
60
        $this->shippingExport = $shippingExport;
61
        $this->flashBag = $flashBag;
62
        $this->shippingExportManager = $shippingExportManager;
63
        $this->shippingLabelsPath = $shippingLabelsPath;
64
        $this->filesystem = $filesystem;
65
        $this->translator = $translator;
66
    }
67
68
    public function getShippingExport(): ?ShippingExportInterface
69
    {
70
        return $this->shippingExport;
71
    }
72
73
    public function addSuccessFlash(string $messageId = 'bitbag.ui.shipment_data_has_been_exported'): void
74
    {
75
        $message = $this->translator->trans($messageId);
76
77
        if (false === $this->flashBag->has('success')) {
78
            $this->flashBag->add('success', $message);
79
        }
80
    }
81
82
    public function addErrorFlash(string $messageId = 'bitbag.ui.shipping_export_error'): void
83
    {
84
        $message = $this->translator->trans($messageId);
85
86
        if (false === $this->flashBag->has('error')) {
87
            $this->flashBag->add('error', $message);
88
        }
89
    }
90
91
    public function saveShippingLabel(string $labelContent, string $labelExtension): void
92
    {
93
        $labelPath = $this->shippingLabelsPath
94
            . '/' . $this->getFilename()
95
            . '.' . $labelExtension;
96
97
        $this->filesystem->dumpFile($labelPath, $labelContent);
98
        $this->shippingExport->setLabelPath($labelPath);
99
100
        $this->shippingExportManager->persist($this->shippingExport);
101
        $this->shippingExportManager->flush();
102
    }
103
104
    public function exportShipment(): void
105
    {
106
        $this->shippingExport->setState(ShippingExportInterface::STATE_EXPORTED);
107
        $this->shippingExport->setExportedAt(new \DateTime());
108
109
        $this->shippingExportManager->persist($this->shippingExport);
110
        $this->shippingExportManager->flush();
111
    }
112
113
    private function getFilename(): string
114
    {
115
        $shipment = $this->shippingExport->getShipment();
116
        Assert::notNull($shipment);
117
        $order = $shipment->getOrder();
118
        Assert::notNull($order);
119
        $orderNumber = $order->getNumber();
120
        Assert::notNull($orderNumber);
121
        $shipmentId = $shipment->getId();
122
123
        return implode('_', [
124
            $shipmentId,
125
            preg_replace('~[^A-Za-z0-9]~', '', $orderNumber),
126
        ]);
127
    }
128
}
129