UpdateUserAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A __invoke() 0 26 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\Controller\User;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\User\UpdateUser;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\GenericSuccessViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Factory\User\UserProfileViewFactoryInterface;
18
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface;
19
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface;
20
use FOS\RestBundle\View\View;
21
use FOS\RestBundle\View\ViewHandlerInterface;
22
use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
23
use Sylius\Component\Customer\Model\CustomerInterface;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\Messenger\MessageBusInterface;
27
28
final class UpdateUserAction
29
{
30
    /** @var RequestProcessorInterface */
31
    private $updateUserRequestProcessor;
32
33
    /** @var MessageBusInterface */
34
    private $bus;
35
36
    /** @var ViewHandlerInterface */
37
    private $viewHandler;
38
39
    /** @var ValidationErrorViewFactoryInterface */
40
    private $validationErrorViewFactory;
41
42
    /** @var CustomerRepositoryInterface */
43
    private $customerRepository;
44
45
    /** @var GenericSuccessViewFactoryInterface */
46
    private $genericSuccessViewFactory;
47
48
    /** @var UserProfileViewFactoryInterface */
49
    private $userProfileViewFactory;
50
51
    public function __construct(
52
        RequestProcessorInterface $updateUserRequestProcessor,
53
        MessageBusInterface $bus,
54
        ViewHandlerInterface $viewHandler,
55
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
56
        CustomerRepositoryInterface $customerRepository,
57
        GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
58
        UserProfileViewFactoryInterface $userProfileViewFactory
59
    ) {
60
        $this->updateUserRequestProcessor = $updateUserRequestProcessor;
61
        $this->bus = $bus;
62
        $this->viewHandler = $viewHandler;
63
        $this->validationErrorViewFactory = $validationErrorViewFactory;
64
        $this->customerRepository = $customerRepository;
65
        $this->genericSuccessViewFactory = $genericSuccessViewFactory;
66
        $this->userProfileViewFactory = $userProfileViewFactory;
67
    }
68
69
    public function __invoke(Request $request): Response
70
    {
71
        $validationResults = $this->updateUserRequestProcessor->validate($request);
72
73
        if (0 !== count($validationResults)) {
74
            return $this->viewHandler->handle(View::create(
75
                $this->validationErrorViewFactory->create($validationResults),
76
                Response::HTTP_BAD_REQUEST
77
            ));
78
        }
79
80
        /** @var UpdateUser $updateUserCommand */
81
        $updateUserCommand = $this->updateUserRequestProcessor->getCommand($request);
82
83
        $this->bus->dispatch($updateUserCommand);
84
85
        /** @var CustomerInterface $customer */
86
        $customer = $this->customerRepository->findOneBy(['email' => $updateUserCommand->customer()->email]);
87
88
        return $this->viewHandler->handle(View::create(
89
            $this->genericSuccessViewFactory->create(
90
                $this->userProfileViewFactory->create($customer)
91
            ),
92
            Response::HTTP_OK
93
        ));
94
    }
95
}
96