SendReportAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 4
eloc 17
c 3
b 0
f 1
dl 0
loc 46
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 17 3
A __construct() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\Controller\Action;
6
7
use Safe\Exceptions\StringsException;
8
use function Safe\sprintf;
9
use Setono\SyliusStockMovementPlugin\Message\Command\SendReport;
10
use Setono\SyliusStockMovementPlugin\Model\ReportInterface;
11
use Setono\SyliusStockMovementPlugin\Repository\ReportRepositoryInterface;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
use Symfony\Component\Messenger\MessageBusInterface;
16
use Symfony\Component\Routing\RouterInterface;
17
18
final class SendReportAction
19
{
20
    /** @var MessageBusInterface */
21
    private $commandBus;
22
23
    /** @var RouterInterface */
24
    private $router;
25
26
    /** @var FlashBagInterface */
27
    private $flashBag;
28
29
    /** @var ReportRepositoryInterface */
30
    private $reportRepository;
31
32
    public function __construct(
33
        MessageBusInterface $commandBus,
34
        RouterInterface $router,
35
        FlashBagInterface $flashBag,
36
        ReportRepositoryInterface $reportRepository
37
    ) {
38
        $this->commandBus = $commandBus;
39
        $this->router = $router;
40
        $this->flashBag = $flashBag;
41
        $this->reportRepository = $reportRepository;
42
    }
43
44
    /**
45
     * @throws StringsException
46
     */
47
    public function __invoke($id): RedirectResponse
48
    {
49
        /** @var ReportInterface|null $report */
50
        $report = $this->reportRepository->find($id);
51
        if (null === $report) {
52
            throw new NotFoundHttpException(sprintf('The report with id %s was not found', $id));
53
        }
54
55
        if ($report->isSuccessful()) {
56
            $this->commandBus->dispatch(new SendReport($id));
57
58
            $this->flashBag->add('success', 'setono_sylius_stock_movement.report_sent');
59
        } else {
60
            $this->flashBag->add('error', 'setono_sylius_stock_movement.report_not_sent');
61
        }
62
63
        return new RedirectResponse($this->router->generate('setono_sylius_stock_movement_admin_report_show', ['id' => $id]));
64
    }
65
}
66