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

CreateAddressAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

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