Passed
Pull Request — master (#6)
by Joachim
05:35
created

RevalidateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 20
dl 0
loc 52
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A __invoke() 0 18 3
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