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

SetDefaultAddressAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
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\ShopApiPlugin\Command\SetDefaultAddress;
11
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
12
use Sylius\ShopApiPlugin\Request\SetDefaultAddressRequest;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
16
use Symfony\Component\Validator\Validator\ValidatorInterface;
17
18 View Code Duplication
final class SetDefaultAddressAction
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
{
20
    /**
21
     * @var ViewHandlerInterface
22
     */
23
    private $viewHandler;
24
25
    /**
26
     * @var CommandBus
27
     */
28
    private $bus;
29
30
    /**
31
     * @var ValidatorInterface
32
     */
33
    private $validator;
34
35
    /**
36
     * @var ValidationErrorViewFactoryInterface
37
     */
38
    private $validationErrorViewFactory;
39
40
    /**
41
     * @var TokenStorageInterface
42
     */
43
    private $tokenStorage;
44
45
    /**
46
     * @param ViewHandlerInterface $viewHandler
47
     * @param CommandBus $bus
48
     * @param ValidatorInterface $validator
49
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
50
     * @param TokenStorageInterface $tokenStorage
51
     */
52
    public function __construct(
53
        ViewHandlerInterface $viewHandler,
54
        CommandBus $bus,
55
        ValidatorInterface $validator,
56
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
57
        TokenStorageInterface $tokenStorage
58
    ) {
59
        $this->viewHandler = $viewHandler;
60
        $this->bus = $bus;
61
        $this->validator = $validator;
62
        $this->validationErrorViewFactory = $validationErrorViewFactory;
63
        $this->tokenStorage = $tokenStorage;
64
    }
65
66
    /**
67
     * @param Request $request
68
     *
69
     * @return Response
70
     */
71
    public function __invoke(Request $request)
72
    {
73
        $setDefaultAddressRequest = new SetDefaultAddressRequest($request);
74
75
        $validationResults = $this->validator->validate($setDefaultAddressRequest);
76
77
        if (0 !== count($validationResults)) {
78
            return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
79
        }
80
81
        $this->bus->handle(new SetDefaultAddress(
82
            $request->attributes->get('id'),
83
            $this->tokenStorage->getToken()->getUser()->getEmail()
84
        ));
85
86
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
87
    }
88
}
89