Completed
Pull Request — master (#229)
by
unknown
02:44
created

UpdateAddressAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A __invoke() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Controller\AddressBook;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandlerInterface;
9
use League\Tactician\CommandBus;
10
use Sylius\Component\Core\Model\AddressInterface;
11
use Sylius\Component\Core\Repository\AddressRepositoryInterface;
12
use Sylius\ShopApiPlugin\Command\UpdateAddress;
13
use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface;
14
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
15
use Sylius\ShopApiPlugin\Model\Address;
16
use Sylius\ShopApiPlugin\Provider\CurrentUserProviderInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
20
use Symfony\Component\Validator\Validator\ValidatorInterface;
21
22
final class UpdateAddressAction
23
{
24
    /**
25
     * @var ViewHandlerInterface
26
     */
27
    private $viewHandler;
28
29
    /**
30
     * @var ValidatorInterface
31
     */
32
    private $validator;
33
34
    /**
35
     * @var CommandBus
36
     */
37
    private $bus;
38
39
    /**
40
     * @var ValidationErrorViewFactoryInterface
41
     */
42
    private $validationErrorViewFactory;
43
44
    /**
45
     * @var AddressBookViewFactoryInterface
46
     */
47
    private $addressBookViewFactory;
48
49
    /**
50
     * @var AddressRepositoryInterface
51
     */
52
    private $addressRepository;
53
54
    /**
55
     * @var TokenStorageInterface
56
     */
57
    private $tokenStorage;
58
    /**
59
     * @var CurrentUserProviderInterface
60
     */
61
    private $currentUserProvider;
62
63
    /**
64
     * @param ViewHandlerInterface $viewHandler
65
     * @param ValidatorInterface $validator
66
     * @param CommandBus $bus
67
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
68
     * @param AddressBookViewFactoryInterface $addressViewFactory
69
     * @param AddressRepositoryInterface $addressRepository
70
     * @param TokenStorageInterface $tokenStorage
71
     * @param CurrentUserProviderInterface $currentUserProvider
72
     */
73
    public function __construct(
74
        ViewHandlerInterface $viewHandler,
75
        ValidatorInterface $validator,
76
        CommandBus $bus,
77
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
78
        AddressBookViewFactoryInterface $addressViewFactory,
79
        AddressRepositoryInterface $addressRepository,
80
        TokenStorageInterface $tokenStorage,
81
        CurrentUserProviderInterface $currentUserProvider
82
    ) {
83
        $this->viewHandler = $viewHandler;
84
        $this->validator = $validator;
85
        $this->bus = $bus;
86
        $this->validationErrorViewFactory = $validationErrorViewFactory;
87
        $this->addressBookViewFactory = $addressViewFactory;
88
        $this->addressRepository = $addressRepository;
89
        $this->tokenStorage = $tokenStorage;
90
        $this->currentUserProvider = $currentUserProvider;
91
    }
92
93
    public function __invoke(Request $request, $id): Response
94
    {
95
        $addressModel = Address::createFromRequest($request);
96
97
        $validationResults = $this->validator->validate($addressModel);
98
99
        if (0 !== count($validationResults)) {
100
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
101
        }
102
103
        $user = $this->currentUserProvider->provide();
104
105
        $this->bus->handle(new UpdateAddress($addressModel, $user->getEmail(), $id));
106
107
        /** @var AddressInterface $updatedAddress */
108
        $updatedAddress = $this->addressRepository->findOneBy(['id' => $id]);
109
110
        return $this->viewHandler->handle(View::create(
111
            $this->addressBookViewFactory->create($updatedAddress, $user->getCustomer()),
112
            Response::HTTP_OK)
113
        );
114
    }
115
}
116