ProcessReportConfigurationHandler::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 16
rs 10
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\Generator\ReportGeneratorInterface;
11
use Setono\SyliusStockMovementPlugin\Message\Command\ProcessReportConfiguration;
12
use Setono\SyliusStockMovementPlugin\Message\Command\SendReport;
13
use Setono\SyliusStockMovementPlugin\Model\ReportConfigurationInterface;
14
use Sylius\Component\Resource\Repository\RepositoryInterface;
15
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
16
use Symfony\Component\Messenger\MessageBusInterface;
17
18
class ProcessReportConfigurationHandler implements MessageHandlerInterface
19
{
20
    /** @var MessageBusInterface */
21
    private $commandBus;
22
23
    /** @var RepositoryInterface */
24
    private $reportConfigurationRepository;
25
26
    /** @var ReportGeneratorInterface */
27
    private $reportGenerator;
28
29
    public function __construct(
30
        MessageBusInterface $commandBus,
31
        RepositoryInterface $reportConfigurationRepository,
32
        ReportGeneratorInterface $reportGenerator
33
    ) {
34
        $this->commandBus = $commandBus;
35
        $this->reportConfigurationRepository = $reportConfigurationRepository;
36
        $this->reportGenerator = $reportGenerator;
37
    }
38
39
    /**
40
     * @throws StringsException
41
     */
42
    public function __invoke(ProcessReportConfiguration $message): void
43
    {
44
        /** @var ReportConfigurationInterface|null $reportConfiguration */
45
        $reportConfiguration = $this->reportConfigurationRepository->find($message->getReportConfigurationId());
46
47
        if (null === $reportConfiguration) {
48
            throw new InvalidArgumentException(sprintf('The report configuration with id %s was not found', $message->getReportConfigurationId()));
49
        }
50
51
        $report = $this->reportGenerator->generate($reportConfiguration);
52
53
        if (null === $report) {
54
            return;
55
        }
56
57
        $this->commandBus->dispatch(new SendReport($report));
58
    }
59
}
60