Completed
Pull Request — master (#132)
by Łukasz
03:15
created

PutItemToCartAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 11
dl 0
loc 82
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 0 14 2
B provideCommandRequest() 0 16 7
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Controller\Cart;
4
5
use FOS\RestBundle\View\View;
6
use FOS\RestBundle\View\ViewHandlerInterface;
7
use League\Tactician\CommandBus;
8
use Sylius\ShopApiPlugin\Factory\ValidationErrorViewFactoryInterface;
9
use Sylius\ShopApiPlugin\Request\PutOptionBasedConfigurableItemToCartRequest;
10
use Sylius\ShopApiPlugin\Request\PutSimpleItemToCartRequest;
11
use Sylius\ShopApiPlugin\Request\PutVariantBasedConfigurableItemToCartRequest;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
use Symfony\Component\Validator\Validator\ValidatorInterface;
16
17
final class PutItemToCartAction
18
{
19
    /**
20
     * @var ViewHandlerInterface
21
     */
22
    private $viewHandler;
23
24
    /**
25
     * @var CommandBus
26
     */
27
    private $bus;
28
29
    /**
30
     * @var ValidatorInterface
31
     */
32
    private $validator;
33
34
    /**
35
     * @var ValidationErrorViewFactoryInterface
36
     */
37
    private $validationErrorViewFactory;
38
39
    /**
40
     * @param ViewHandlerInterface $viewHandler
41
     * @param CommandBus $bus
42
     * @param ValidatorInterface $validator
43
     * @param ValidationErrorViewFactoryInterface $validationErrorViewFactory
44
     */
45
    public function __construct(
46
        ViewHandlerInterface $viewHandler,
47
        CommandBus $bus,
48
        ValidatorInterface $validator,
49
        ValidationErrorViewFactoryInterface $validationErrorViewFactory
50
    ) {
51
        $this->viewHandler = $viewHandler;
52
        $this->bus = $bus;
53
        $this->validator = $validator;
54
        $this->validationErrorViewFactory = $validationErrorViewFactory;
55
    }
56
57
    /**
58
     * @param Request $request
59
     *
60
     * @return Response
61
     */
62
    public function __invoke(Request $request)
63
    {
64
        $commandRequest = $this->provideCommandRequest($request);
65
66
        $validationResults = $this->validator->validate($commandRequest);
67
68
        if (0 === count($validationResults)) {
69
            $this->bus->handle($commandRequest->getCommand());
70
71
            return $this->viewHandler->handle(View::create(null, Response::HTTP_CREATED));
72
        }
73
74
        return $this->viewHandler->handle(View::create($this->validationErrorViewFactory->create($validationResults), Response::HTTP_BAD_REQUEST));
75
    }
76
77
    /**
78
     * @param Request $request
79
     *
80
     * @return PutOptionBasedConfigurableItemToCartRequest|PutSimpleItemToCartRequest|PutVariantBasedConfigurableItemToCartRequest
81
     */
82
    private function provideCommandRequest(Request $request)
83
    {
84
        if (!$request->request->has('variantCode') && !$request->request->has('options')) {
85
            return new PutSimpleItemToCartRequest($request);
86
        }
87
88
        if ($request->request->has('variantCode') && !$request->request->has('options')) {
89
            return new PutVariantBasedConfigurableItemToCartRequest($request);
90
        }
91
92
        if (!$request->request->has('variantCode') && $request->request->has('options')) {
93
            return new PutOptionBasedConfigurableItemToCartRequest($request);
94
        }
95
96
        throw new NotFoundHttpException('Variant not found for given configuration');
97
    }
98
}
99