-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathInvoiceCreator.php
More file actions
69 lines (56 loc) · 2.36 KB
/
InvoiceCreator.php
File metadata and controls
69 lines (56 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\InvoicingPlugin\Creator;
use Webmozart\Assert\Assert;
use Doctrine\ORM\ORMException;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
use Sylius\InvoicingPlugin\Exception\InvoiceAlreadyGenerated;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\InvoicingPlugin\Generator\InvoiceGeneratorInterface;
use Sylius\InvoicingPlugin\Manager\InvoiceFileManagerInterface;
use Sylius\InvoicingPlugin\Doctrine\ORM\InvoiceRepositoryInterface;
use Sylius\InvoicingPlugin\Generator\InvoicePdfFileGeneratorInterface;
final class InvoiceCreator implements InvoiceCreatorInterface
{
public function __construct(
private InvoiceRepositoryInterface $invoiceRepository,
private OrderRepositoryInterface $orderRepository,
private InvoiceGeneratorInterface $invoiceGenerator,
private InvoicePdfFileGeneratorInterface $invoicePdfFileGenerator,
private InvoiceFileManagerInterface $invoiceFileManager,
private bool $hasEnabledPdfFileGenerator = true
) {
}
public function __invoke(string $orderNumber, \DateTimeInterface $dateTime): void
{
/** @var OrderInterface $order */
$order = $this->orderRepository->findOneBy(['number' => $orderNumber]);
Assert::notNull($order, sprintf('Order with number "%s" does not exist.', $orderNumber));
/** @var InvoiceInterface|null $invoice */
$invoice = $this->invoiceRepository->findOneByOrder($order);
if (null !== $invoice) {
throw InvoiceAlreadyGenerated::withOrderNumber($orderNumber);
}
$invoice = $this->invoiceGenerator->generateForOrder($order, $dateTime);
if (!$this->hasEnabledPdfFileGenerator) {
$this->invoiceRepository->add($invoice);
return;
}
$invoicePdf = $this->invoicePdfFileGenerator->generate($invoice);
$this->invoiceFileManager->save($invoicePdf);
try {
$this->invoiceRepository->add($invoice);
} catch (ORMException $exception) {
$this->invoiceFileManager->remove($invoicePdf);
}
}
}