SendReportHandler::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 10
dl 0
loc 21
rs 9.9332
c 2
b 0
f 1
cc 4
nc 4
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\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);
0 ignored issues
show
Bug introduced by
It seems like $report->getStatus() can also be of type null; however, parameter $actualStatus of Setono\SyliusStockMoveme...xception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            throw new UnexpectedStatusException(/** @scrutinizer ignore-type */ $report->getStatus(), ReportInterface::STATUS_SUCCESS);
Loading history...
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