Passed
Push — master ( 6957cf...3bb145 )
by Joachim
04:59
created

SendReportHandler::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 17
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
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\Message\Command\SendReport;
11
use Setono\SyliusStockMovementPlugin\Model\ReportInterface;
12
use Setono\SyliusStockMovementPlugin\Repository\ReportRepositoryInterface;
13
use Setono\SyliusStockMovementPlugin\Sender\ReportSenderInterface;
14
use Setono\SyliusStockMovementPlugin\Writer\ReportWriterInterface;
15
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
16
17
class SendReportHandler implements MessageHandlerInterface
18
{
19
    /** @var ReportRepositoryInterface */
20
    private $reportRepository;
21
22
    /** @var ReportWriterInterface */
23
    private $reportWriter;
24
25
    /** @var ReportSenderInterface */
26
    private $reportSender;
27
28
    public function __construct(
29
        ReportRepositoryInterface $reportRepository,
30
        ReportWriterInterface $reportWriter,
31
        ReportSenderInterface $reportSender
32
    ) {
33
        $this->reportRepository = $reportRepository;
34
        $this->reportWriter = $reportWriter;
35
        $this->reportSender = $reportSender;
36
    }
37
38
    /**
39
     * @throws StringsException
40
     */
41
    public function __invoke(SendReport $message): void
42
    {
43
        /** @var ReportInterface|null $report */
44
        $report = $this->reportRepository->find($message->getReportId());
45
        if (null === $report) {
46
            throw new InvalidArgumentException(sprintf('The report with id %s was not found', $message->getReportId())); // todo better exception
47
        }
48
49
        $reportConfiguration = $report->getReportConfiguration();
50
51
        if (null === $reportConfiguration) {
52
            throw new InvalidArgumentException(sprintf('No report configuration associated with report %s', $report->getId())); // todo better exception
53
        }
54
55
        $file = $this->reportWriter->write($report);
56
57
        $this->reportSender->send($file, $report, $reportConfiguration);
58
    }
59
}
60