Failed Conditions
Pull Request — master (#236)
by
unknown
03:01
created

UpdateCustomerAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
B __invoke() 0 24 2
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\Model\ShopUserInterface;
11
use Sylius\ShopApiPlugin\Factory\CustomerViewFactoryInterface;
12
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
13
use Sylius\ShopApiPlugin\Request\UpdateCustomerRequest;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Validator\Validator\ValidatorInterface;
18
use Webmozart\Assert\Assert;
19
20
final class UpdateCustomerAction
21
{
22
    /** @var ViewHandlerInterface */
23
    private $viewHandler;
24
25
    /** @var ValidatorInterface */
26
    private $validator;
27
28
    /** @var CommandBus */
29
    private $bus;
30
31
    /** @var ValidationErrorViewFactoryInterface */
32
    private $validationErrorViewFactory;
33
34
    /** @var CustomerViewFactoryInterface */
35
    private $customerViewFactory;
36
37
    /** @var TokenStorageInterface */
38
    private $tokenStorage;
39
40
    public function __construct(
41
        ViewHandlerInterface $viewHandler,
42
        ValidatorInterface $validator,
43
        CommandBus $bus,
44
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
45
        CustomerViewFactoryInterface $customerViewFactory,
46
        TokenStorageInterface $tokenStorage
47
    )
48
    {
49
        $this->viewHandler = $viewHandler;
50
        $this->validator = $validator;
51
        $this->bus = $bus;
52
        $this->validationErrorViewFactory = $validationErrorViewFactory;
53
        $this->customerViewFactory = $customerViewFactory;
54
        $this->tokenStorage = $tokenStorage;
55
    }
56
57
    /**
58
     * @param Request $request
59
     *
60
     * @return Response
61
     */
62
    public function __invoke(Request $request): Response
63
    {
64
        /** @var ShopUserInterface $user */
65
        $user = $this->tokenStorage->getToken()->getUser();
66
67
        Assert::isInstanceOf($user, ShopUserInterface::class);
68
69
        $customer = $user->getCustomer();
70
        $updateCustomerRequest = new UpdateCustomerRequest($request);
71
72
        $validationResults = $this->validator->validate($updateCustomerRequest, null, 'sylius_customer_profile_update');
73
74
        if (0 !== count($validationResults)) {
75
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
76
        }
77
78
        $updateCustomerCommand = $updateCustomerRequest->getCommand();
79
        $this->bus->handle($updateCustomerCommand);
80
81
        return $this->viewHandler->handle(View::create(
82
            $this->customerViewFactory->create($customer),
83
            Response::HTTP_OK)
84
        );
85
    }
86
}
87