Completed
Push — master ( c9e675...0882fe )
by Paweł
02:38
created

PickupAction::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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\Component\Core\Repository\OrderRepositoryInterface;
9
use Sylius\ShopApiPlugin\Command\PickupCart;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
14
15
final class PickupAction extends Controller
16
{
17
    /**
18
     * @var OrderRepositoryInterface
19
     */
20
    private $cartRepository;
21
22
    /**
23
     * @var ViewHandlerInterface
24
     */
25
    private $viewHandler;
26
27
    /**
28
     * @var CommandBus
29
     */
30
    private $bus;
31
32
    /**
33
     * @param OrderRepositoryInterface $cartRepository
34
     * @param ViewHandlerInterface $viewHandler
35
     * @param CommandBus $bus
36
     */
37
    public function __construct(OrderRepositoryInterface $cartRepository, ViewHandlerInterface $viewHandler, CommandBus $bus)
38
    {
39
        $this->cartRepository = $cartRepository;
40
        $this->viewHandler = $viewHandler;
41
        $this->bus = $bus;
42
    }
43
44
    /**
45
     * @param Request $request
46
     *
47
     * @return Response
48
     */
49
    public function __invoke(Request $request)
50
    {
51
        if (null !== $this->cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')])) {
52
            throw new BadRequestHttpException('Cart with given token already exists');
53
        }
54
55
        $this->bus->handle(new PickupCart($request->attributes->get('token'), $request->request->get('channel')));
56
57
        return $this->viewHandler->handle(View::create(null, Response::HTTP_CREATED));
58
    }
59
60
}
61