1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Setono\SyliusStockMovementPlugin\Message\Handler; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Safe\Exceptions\StringsException; |
9
|
|
|
use function Safe\sprintf; |
10
|
|
|
use Setono\SyliusStockMovementPlugin\Exception\UnexpectedStatusException; |
11
|
|
|
use Setono\SyliusStockMovementPlugin\Message\Command\SendReport; |
12
|
|
|
use Setono\SyliusStockMovementPlugin\Model\ReportInterface; |
13
|
|
|
use Setono\SyliusStockMovementPlugin\Repository\ReportRepositoryInterface; |
14
|
|
|
use Setono\SyliusStockMovementPlugin\Sender\ReportSenderInterface; |
15
|
|
|
use Setono\SyliusStockMovementPlugin\Writer\ReportWriterInterface; |
16
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
17
|
|
|
|
18
|
|
|
class SendReportHandler implements MessageHandlerInterface |
19
|
|
|
{ |
20
|
|
|
/** @var ReportRepositoryInterface */ |
21
|
|
|
private $reportRepository; |
22
|
|
|
|
23
|
|
|
/** @var ReportWriterInterface */ |
24
|
|
|
private $reportWriter; |
25
|
|
|
|
26
|
|
|
/** @var ReportSenderInterface */ |
27
|
|
|
private $reportSender; |
28
|
|
|
|
29
|
|
|
public function __construct( |
30
|
|
|
ReportRepositoryInterface $reportRepository, |
31
|
|
|
ReportWriterInterface $reportWriter, |
32
|
|
|
ReportSenderInterface $reportSender |
33
|
|
|
) { |
34
|
|
|
$this->reportRepository = $reportRepository; |
35
|
|
|
$this->reportWriter = $reportWriter; |
36
|
|
|
$this->reportSender = $reportSender; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws StringsException |
41
|
|
|
*/ |
42
|
|
|
public function __invoke(SendReport $message): void |
43
|
|
|
{ |
44
|
|
|
/** @var ReportInterface|null $report */ |
45
|
|
|
$report = $this->reportRepository->find($message->getReportId()); |
46
|
|
|
if (null === $report) { |
47
|
|
|
throw new InvalidArgumentException(sprintf('The report with id %s was not found', $message->getReportId())); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (!$report->isSuccessful()) { |
51
|
|
|
throw new UnexpectedStatusException($report->getStatus(), ReportInterface::STATUS_SUCCESS); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$reportConfiguration = $report->getReportConfiguration(); |
55
|
|
|
|
56
|
|
|
if (null === $reportConfiguration) { |
57
|
|
|
throw new InvalidArgumentException(sprintf('No report configuration associated with report %s', $report->getId())); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$file = $this->reportWriter->write($report); |
61
|
|
|
|
62
|
|
|
$this->reportSender->send($file, $report, $reportConfiguration); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|