CompleteOrderAction   A
last analyzed

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\ShopApiPlugin\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) {
0 ignored issues
show
Bug introduced by
The class Sylius\Component\Core\Model\ShopUserInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
            return $user->getCustomer()->getEmail();
74
        }
75
76
        return $request->request->get('email');
77
    }
78
}
79