DeviceController::getSerialNumbers()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 14
cts 14
cp 1
rs 9.1608
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\ApplePassbookBundle\Controller\V1\PassKit;
6
7
use DateTimeImmutable;
8
use LauLamanApps\ApplePassbookBundle\Event\DeviceRegisteredEvent;
9
use LauLamanApps\ApplePassbookBundle\Event\DeviceRequestUpdatedPassesEvent;
10
use LauLamanApps\ApplePassbookBundle\Event\DeviceUnregisteredEvent;
11
use LauLamanApps\ApplePassbookBundle\Event\Status;
12
use LogicException;
13
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\Routing\Annotation\Route;
19
20
/**
21
 * @Route("/v1/devices/{deviceLibraryIdentifier}/registrations/{passTypeIdentifier}")
22
 */
23
class DeviceController extends AbstractController
24
{
25
    use AuthenticationToken;
26
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    private $eventDispatcher;
31
32 14
    public function __construct(EventDispatcherInterface $eventDispatcher)
33
    {
34 14
        $this->eventDispatcher = $eventDispatcher;
35 14
    }
36
37
    /**
38
     * @Route("/{serialNumber}", methods={"POST"})
39
     */
40 6
    public function register(
41
        Request $request,
42
        string $deviceLibraryIdentifier,
43
        string $passTypeIdentifier,
44
        string $serialNumber
45
    ): JsonResponse {
46 6
        $event = new DeviceRegisteredEvent(
47 6
            $deviceLibraryIdentifier,
48
            $passTypeIdentifier,
49
            $serialNumber,
50 6
            $this->getAuthenticationToken($request),
51 6
            json_decode($request->getContent())->pushToken
52
        );
53
54
        /** @var Status $status */
55 6
        $status = $this->eventDispatcher->dispatch($event)->getStatus();
0 ignored issues
show
Documentation introduced by
$event is of type object<LauLamanApps\Appl...\DeviceRegisteredEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
57 6
        if ($status->isUnhandled()) {
58 1
            throw new LogicException('DeviceRegisteredEvent was not handled. Please implement a listener for this event.');
59
        }
60
61 5
        if ($status->isNotAuthorized()) {
62 1
            return new JsonResponse([], Response::HTTP_UNAUTHORIZED);
63
        }
64
65 4
        if ($status->isAlreadyRegistered()) {
66 1
            return new JsonResponse([], Response::HTTP_OK);
67
        }
68
69 3
        if ($status->isSuccessful()) {
70 2
            return new JsonResponse([], Response::HTTP_CREATED);
71
        }
72
73 1
        throw new LogicException('DeviceRegisteredEvent was not handled correctly. Unexpected status was set.');
74
    }
75
76
    /**
77
     * @Route("/{serialNumber}", methods={"DELETE"})
78
     */
79 5
    public function unregister(
80
        Request $request,
81
        string $deviceLibraryIdentifier,
82
        string $passTypeIdentifier,
83
        string $serialNumber
84
    ): JsonResponse {
85 5
        $event = new DeviceUnregisteredEvent(
86 5
            $deviceLibraryIdentifier,
87
            $passTypeIdentifier,
88
            $serialNumber,
89 5
            $this->getAuthenticationToken($request)
90
        );
91
92
        /** @var Status $status */
93 5
        $status = $this->eventDispatcher->dispatch($event)->getStatus();
0 ignored issues
show
Documentation introduced by
$event is of type object<LauLamanApps\Appl...eviceUnregisteredEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
95 5
        if ($status->isUnhandled()) {
96 1
            throw new LogicException('DeviceUnregisteredEvent was not handled. Please implement a listener for this event.');
97
        }
98
99 4
        if ($status->isNotAuthorized()) {
100 1
            return new JsonResponse([], Response::HTTP_UNAUTHORIZED);
101
        }
102
103 3
        if ($status->isSuccessful()) {
104 2
            return new JsonResponse([], Response::HTTP_OK);
105
        }
106
107 1
        throw new LogicException('DeviceUnregisteredEvent was not handled correctly. Unexpected status was set.');
108
    }
109
110
    /**
111
     * @Route("", methods={"GET"})
112
     */
113 6
    public function getSerialNumbers(
114
        string $deviceLibraryIdentifier,
115
        string $passTypeIdentifier
116
    ): JsonResponse {
117 6
        $event = new DeviceRequestUpdatedPassesEvent($deviceLibraryIdentifier, $passTypeIdentifier);
118 6
        $this->eventDispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<LauLamanApps\Appl...uestUpdatedPassesEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
120 6
        if ($event->getStatus()->isUnhandled()) {
121 1
            throw new LogicException('DeviceRequestUpdatedPassesEvent was not handled. Please implement a listener for this event.');
122
        }
123
124 5
        if ($event->getStatus()->isNotFound()) {
125 1
            return new JsonResponse([], Response::HTTP_NO_CONTENT);
126
        }
127
128 4
        if ($event->getStatus()->isNotModified()) {
129 1
            return new JsonResponse([], Response::HTTP_NOT_MODIFIED);
130
        }
131
132 3
        if ($event->getStatus()->isSuccessful()) {
133 2
            return new JsonResponse([
134 2
                'lastUpdated' => $event->getLastUpdated()->format(DateTimeImmutable::ATOM),
135 2
                'serialNumbers' => $event->getSerialNumbers()
136
            ]);
137
        }
138
139 1
        throw new LogicException('DeviceRequestUpdatedPassesEvent was not handled correctly. Unexpected status was set.');
140
    }
141
}
142