Failed Conditions
Pull Request — master (#229)
by
unknown
02:42
created

UpdateAddressAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 15
nc 1
nop 7
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\Repository\AddressRepositoryInterface;
11
use Sylius\ShopApiPlugin\Command\UpdateAddress;
12
use Sylius\ShopApiPlugin\Factory\AddressBookViewFactoryInterface;
13
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
14
use Sylius\ShopApiPlugin\Model\Address;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Validator\Validator\ValidatorInterface;
19
20
final class UpdateAddressAction
21
{
22
    /**
23
     * @var ViewHandlerInterface
24
     */
25
    private $viewHandler;
26
27
    /**
28
     * @var ValidatorInterface
29
     */
30
    private $validator;
31
32
    /**
33
     * @var CommandBus
34
     */
35
    private $bus;
36
37
    /**
38
     * @var ValidationErrorViewFactoryInterface
39
     */
40
    private $validationErrorViewFactory;
41
42
    /**
43
     * @var AddressBookViewFactoryInterface
44
     */
45
    private $addressBookViewFactory;
46
47
    /**
48
     * @var AddressRepositoryInterface
49
     */
50
    private $addressRepository;
51
52
    /**
53
     * @var TokenStorageInterface
54
     */
55
    private $tokenStorage;
56
57
    /**
58
     * @param ViewHandlerInterface $viewHandler
59
     * @param ValidatorInterface $validator
60
     * @param CommandBus $bus
61
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
62
     * @param AddressBookViewFactoryInterface $addressViewFactory
63
     * @param AddressRepositoryInterface $addressRepository
64
     * @param TokenStorageInterface $tokenStorage
65
     */
66
    public function __construct(
67
        ViewHandlerInterface $viewHandler,
68
        ValidatorInterface $validator,
69
        CommandBus $bus,
70
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
71
        AddressBookViewFactoryInterface $addressViewFactory,
72
        AddressRepositoryInterface $addressRepository,
73
        TokenStorageInterface $tokenStorage
74
    ) {
75
        $this->viewHandler = $viewHandler;
76
        $this->validator = $validator;
77
        $this->bus = $bus;
78
        $this->validationErrorViewFactory = $validationErrorViewFactory;
79
        $this->addressBookViewFactory = $addressViewFactory;
80
        $this->addressRepository = $addressRepository;
81
        $this->tokenStorage = $tokenStorage;
82
    }
83
84
    public function __invoke(Request $request, $id): Response
85
    {
86
        $addressModel = Address::createFromRequest($request);
87
88
        $validationResults = $this->validator->validate($addressModel, null, 'sylius_address_book_update');
89
90
        if (0 !== count($validationResults)) {
91
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
92
        }
93
94
        $this->bus->handle(new UpdateAddress($addressModel));
95
96
        $updatedAddress = $this->addressRepository->findOneBy(['id' => $id]);
97
        $customer = $this->tokenStorage->getToken()->getUser()->getCustomer();
98
99
        return $this->viewHandler->handle(View::create(
100
            $this->addressBookViewFactory->create($updatedAddress, $customer),
101
            Response::HTTP_OK)
102
        );
103
    }
104
}
105