Completed
Pull Request — master (#19)
by
unknown
01:25
created

ExportShipmentEvent::getFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
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
class ExportShipmentEvent extends Event
25
{
26
    public const NAME = 'bitbag.shipping_export.export_shipment';
27
28
    public const SHORT_NAME = 'export_shipment';
29
30
    /** @var ShippingExportInterface */
31
    private $shippingExport;
32
33
    /** @var FlashBagInterface */
34
    private $flashBag;
35
36
    /** @var EntityManagerInterface|EntityManager */
37
    private $shippingExportManager;
38
39
    /** @var Filesystem */
40
    private $filesystem;
41
42
    /** @var TranslatorInterface */
43
    private $translator;
44
45
    /** @var string */
46
    private $shippingLabelsPath;
47
48
    public function __construct(
49
        ShippingExportInterface $shippingExport,
50
        FlashBagInterface $flashBag,
51
        EntityManagerInterface $shippingExportManager,
52
        Filesystem $filesystem,
53
        TranslatorInterface $translator,
54
        string $shippingLabelsPath
55
    ) {
56
        $this->shippingExport = $shippingExport;
57
        $this->flashBag = $flashBag;
58
        $this->shippingExportManager = $shippingExportManager;
59
        $this->shippingLabelsPath = $shippingLabelsPath;
60
        $this->filesystem = $filesystem;
61
        $this->translator = $translator;
62
    }
63
64
    public function getShippingExport(): ?ShippingExportInterface
65
    {
66
        return $this->shippingExport;
67
    }
68
69
    public function addSuccessFlash(string $messageId = 'bitbag.ui.shipment_data_has_been_exported'): void
70
    {
71
        $message = $this->translator->trans($messageId);
72
73
        if (false === $this->flashBag->has('success')) {
74
            $this->flashBag->add('success', $message);
75
        }
76
    }
77
78
    public function addErrorFlash(string $messageId = 'bitbag.ui.shipping_export_error'): void
79
    {
80
        $message = $this->translator->trans($messageId);
81
82
        if (false === $this->flashBag->has('error')) {
83
            $this->flashBag->add('error', $message);
84
        }
85
    }
86
87
    public function saveShippingLabel(string $labelContent, string $labelExtension): void
88
    {
89
        $labelPath = $this->shippingLabelsPath
90
            . '/' . $this->getFilename()
91
            . '.' . $labelExtension;
92
93
        $this->filesystem->dumpFile($labelPath, $labelContent);
94
        $this->shippingExport->setLabelPath($labelPath);
95
96
        $this->shippingExportManager->persist($this->shippingExport);
97
        $this->shippingExportManager->flush();
98
    }
99
100
    public function exportShipment(): void
101
    {
102
        $this->shippingExport->setState(ShippingExportInterface::STATE_EXPORTED);
103
        $this->shippingExport->setExportedAt(new \DateTime());
104
105
        $this->shippingExportManager->persist($this->shippingExport);
106
        $this->shippingExportManager->flush();
107
    }
108
109
    private function getFilename(): string
110
    {
111
        $shipment = $this->shippingExport->getShipment();
112
        Assert::notNull($shipment);
113
        $order = $shipment->getOrder();
114
        Assert::notNull($order);
115
        $orderNumber = $order->getNumber();
116
        Assert::notNull($orderNumber);
117
        $shipmentId = $shipment->getId();
118
119
        return implode('_', [
120
            $shipmentId,
121
            preg_replace('~[^A-Za-z0-9]~', '', $orderNumber),
122
        ]);
123
    }
124
}
125