Completed
Push — master ( 92f6ed...8d4a88 )
by Joachim
04:27 queued 29s
created

RevalidateAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 12
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Setono\SyliusStockMovementPlugin\Controller\Action;
6
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Safe\Exceptions\StringsException;
9
use function Safe\sprintf;
10
use Setono\SyliusStockMovementPlugin\Model\ReportInterface;
11
use Setono\SyliusStockMovementPlugin\Repository\ReportRepositoryInterface;
12
use Setono\SyliusStockMovementPlugin\Validator\ReportValidatorInterface;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Routing\RouterInterface;
17
18
final class RevalidateAction
19
{
20
    /** @var RouterInterface */
21
    private $router;
22
23
    /** @var FlashBagInterface */
24
    private $flashBag;
25
26
    /** @var ReportRepositoryInterface */
27
    private $reportRepository;
28
29
    /** @var ObjectManager */
30
    private $reportManager;
31
32
    /** @var ReportValidatorInterface */
33
    private $reportValidator;
34
35
    public function __construct(
36
        RouterInterface $router,
37
        FlashBagInterface $flashBag,
38
        ReportRepositoryInterface $reportRepository,
39
        ObjectManager $reportManager,
40
        ReportValidatorInterface $reportValidator
41
    ) {
42
        $this->router = $router;
43
        $this->flashBag = $flashBag;
44
        $this->reportRepository = $reportRepository;
45
        $this->reportManager = $reportManager;
46
        $this->reportValidator = $reportValidator;
47
    }
48
49
    /**
50
     * @throws StringsException
51
     */
52
    public function __invoke($id)
53
    {
54
        /** @var ReportInterface|null $report */
55
        $report = $this->reportRepository->find($id);
56
        if (null === $report) {
57
            throw new NotFoundHttpException(sprintf('The report with id %s was not found', $id));
58
        }
59
60
        $this->reportValidator->validate($report);
61
        $this->reportManager->flush();
62
63
        if ($report->isSuccessful()) {
64
            $this->flashBag->add('success', 'setono_sylius_stock_movement.report_is_valid');
65
        } else {
66
            $this->flashBag->add('error', 'setono_sylius_stock_movement.report_is_invalid');
67
        }
68
69
        return new RedirectResponse($this->router->generate('setono_sylius_stock_movement_admin_report_show', ['id' => $id]));
70
    }
71
}
72