Completed
Pull Request — master (#290)
by
unknown
41:51
created

CompleteOrderAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 12 1
A provideUserEmail() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Controller\Checkout;
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\CompleteOrder;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15
16
final class CompleteOrderAction
17
{
18
    /**
19
     * @var ViewHandlerInterface
20
     */
21
    private $viewHandler;
22
23
    /**
24
     * @var CommandBus
25
     */
26
    private $bus;
27
28
    /**
29
     * @var TokenStorageInterface
30
     */
31
    private $tokenStorage;
32
33
    /**
34
     * @param ViewHandlerInterface $viewHandler
35
     * @param CommandBus $bus
36
     * @param TokenStorageInterface $tokenStorage
37
     */
38
    public function __construct(ViewHandlerInterface $viewHandler, CommandBus $bus, TokenStorageInterface $tokenStorage)
39
    {
40
        $this->viewHandler = $viewHandler;
41
        $this->bus = $bus;
42
        $this->tokenStorage = $tokenStorage;
43
    }
44
45
    /**
46
     * @param Request $request
47
     *
48
     * @return Response
49
     */
50
    public function __invoke(Request $request)
51
    {
52
        $email = $this->provideUserEmail($request);
53
54
        $this->bus->handle(new CompleteOrder(
55
            $request->attributes->get('token'),
56
            $email,
57
            $request->request->get('notes')
58
        ));
59
60
        return $this->viewHandler->handle(View::create(null, Response::HTTP_NO_CONTENT));
61
    }
62
63
    /**
64
     * @param Request $request
65
     *
66
     * @return string
67
     */
68
    private function provideUserEmail(Request $request)
69
    {
70
        $user = $this->tokenStorage->getToken()->getUser();
71
72
        if ($user instanceof ShopUserInterface) {
73
            return $user->getCustomer()->getEmail();
74
        }
75
76
        return $request->request->get('email');
77
    }
78
}
79