Completed
Pull Request — master (#138)
by Łukasz
03:38
created

ResendVerificationTokenAction::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sylius\ShopApiPlugin\Controller\Customer;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandlerInterface;
9
use League\Tactician\CommandBus;
10
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
11
use Sylius\ShopApiPlugin\Command\PickupCart;
12
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
13
use Sylius\ShopApiPlugin\Request\PickupCartRequest;
14
use Sylius\ShopApiPlugin\Request\ResendVerificationTokenRequest;
15
use Sylius\ShopApiPlugin\View\ValidationErrorView;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
20
use Symfony\Component\Validator\ConstraintViolationInterface;
21
use Symfony\Component\Validator\Validator\ValidatorInterface;
22
23
final class ResendVerificationTokenAction
24
{
25
    /**
26
     * @var ViewHandlerInterface
27
     */
28
    private $viewHandler;
29
30
    /**
31
     * @var CommandBus
32
     */
33
    private $bus;
34
35
    /**
36
     * @var ValidatorInterface
37
     */
38
    private $validator;
39
40
    /**
41
     * @var ValidationErrorViewFactoryInterface
42
     */
43
    private $validationErrorViewFactory;
44
45
    public function __construct(
46
        ViewHandlerInterface $viewHandler,
47
        CommandBus $bus,
48
        ValidatorInterface $validator,
49
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
50
    ) {
51
        $this->viewHandler = $viewHandler;
52
        $this->bus = $bus;
53
        $this->validator = $validator;
54
        $this->validationErrorViewFactory = $validationErrorViewFactory;
55
    }
56
57
    public function __invoke(Request $request): Response
58
    {
59
        $resendVerificationTokenRequest = new ResendVerificationTokenRequest($request);
60
61
        $validationResults = $this->validator->validate($resendVerificationTokenRequest);
62
63
        if (0 !== count($validationResults)) {
64
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
65
        }
66
67
        $this->bus->handle($resendVerificationTokenRequest->getCommand());
68
69
        return $this->viewHandler->handle(View::create(null, Response::HTTP_CREATED));
70
    }
71
}
72