Completed
Push — master ( 82aaff...06b207 )
by Kamil
14s
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\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
11
use Sylius\ShopApiPlugin\Request\ResendVerificationTokenRequest;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
final class ResendVerificationTokenAction
17
{
18
    /**
19
     * @var ViewHandlerInterface
20
     */
21
    private $viewHandler;
22
23
    /**
24
     * @var CommandBus
25
     */
26
    private $bus;
27
28
    /**
29
     * @var ValidatorInterface
30
     */
31
    private $validator;
32
33
    /**
34
     * @var ValidationErrorViewFactoryInterface
35
     */
36
    private $validationErrorViewFactory;
37
38
    public function __construct(
39
        ViewHandlerInterface $viewHandler,
40
        CommandBus $bus,
41
        ValidatorInterface $validator,
42
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
43
    ) {
44
        $this->viewHandler = $viewHandler;
45
        $this->bus = $bus;
46
        $this->validator = $validator;
47
        $this->validationErrorViewFactory = $validationErrorViewFactory;
48
    }
49
50
    public function __invoke(Request $request): Response
51
    {
52
        $resendVerificationTokenRequest = new ResendVerificationTokenRequest($request);
53
54
        $validationResults = $this->validator->validate($resendVerificationTokenRequest);
55
56
        if (0 !== count($validationResults)) {
57
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
58
        }
59
60
        $this->bus->handle($resendVerificationTokenRequest->getCommand());
61
62
        return $this->viewHandler->handle(View::create(null, Response::HTTP_CREATED));
63
    }
64
}
65