PassbookController::getUpdatedPassbook()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 22
cts 22
cp 1
rs 9.0168
c 0
b 0
f 0
cc 5
nc 5
nop 3
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\ApplePassbookBundle\Controller\V1\PassKit;
6
7
use LauLamanApps\ApplePassbook\Build\Compiler;
8
use LauLamanApps\ApplePassbookBundle\Event\RetrieveUpdatedPassbookEvent;
9
use LogicException;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
/**
18
 * @Route("/v1/passes/{passTypeIdentifier}/{serialNumber}")
19
 */
20
class PassbookController extends AbstractController
21
{
22
    use AuthenticationToken;
23
24
    /**
25
     * @var EventDispatcherInterface
26
     */
27
    private $eventDispatcher;
28
29
    /**
30
     * @var Compiler
31
     */
32
    private $compiler;
33
34 5
    public function __construct(Compiler $compiler, EventDispatcherInterface $eventDispatcher)
35
    {
36 5
        $this->eventDispatcher = $eventDispatcher;
37 5
        $this->compiler = $compiler;
38 5
    }
39
40
    /**
41
     * @Route("", methods={"GET"})
42
     */
43 5
    public function getUpdatedPassbook(Request $request, string $passTypeIdentifier, string $serialNumber): Response
44
    {
45 5
        $event = new RetrieveUpdatedPassbookEvent(
46 5
            $passTypeIdentifier,
47
            $serialNumber,
48 5
            $this->getAuthenticationToken($request)
49
        );
50 5
        $this->eventDispatcher->dispatch($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<LauLamanApps\Appl...veUpdatedPassbookEvent>, 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...
51
52 5
        if ($event->getStatus()->isUnhandled()) {
53 1
            throw new LogicException('RetrieveUpdatedPassbookEvent was not handled. Please implement a listener for this event.');
54
        }
55
56 4
        if ($event->getStatus()->isNotAuthorized()) {
57 1
            return new JsonResponse([], Response::HTTP_UNAUTHORIZED);
58
        }
59
60 3
        if ($event->getStatus()->isNotModified()) {
61 1
            return new JsonResponse([], Response::HTTP_NOT_MODIFIED);
62
        }
63
64 2
        if ($event->getStatus()->isSuccessful()) {
65 1
            $data = $this->compiler->compile($event->getPassbook());
0 ignored issues
show
Bug introduced by
It seems like $event->getPassbook() can be null; however, compile() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
66 1
            $lastModified = $event->getLastModified();
67 1
            $lastModified = $lastModified->setTimezone(new \DateTimeZone('GMT'));
68
69 1
            $response = new Response($data);
70 1
            $response->headers->set('Content-Description', 'File Transfer');
71 1
            $response->headers->set('Content-Type', 'application/vnd.apple.pkpass');
72 1
            $response->headers->set('Content-Disposition', 'filename="pass.pkpass"');
73 1
            $response->headers->set('Last-Modified', $lastModified->format('D, d M Y H:i:s \G\M\T'));
74
75 1
            return $response;
76
        }
77
78 1
        throw new LogicException('RetrieveUpdatedPassbookEvent was not handled correctly. Unexpected status was set.');
79
    }
80
}
81
82
83