Passed
Push — master ( f20e6e...8d9d59 )
by Laurens
02:00
created

PassbookController::getUpdatedPassbook()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 23
cts 23
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 DateTimeImmutable;
8
use LauLamanApps\ApplePassbook\Build\Compiler;
9
use LauLamanApps\ApplePassbookBundle\Event\RetrieveUpdatedPassbookEvent;
10
use LogicException;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
18
/**
19
 * @Route("/v1/passes/{passTypeIdentifier}/{serialNumber}")
20
 */
21
class PassbookController extends AbstractController
22
{
23
    use AuthenticationToken;
24
25
    /**
26
     * @var EventDispatcherInterface
27
     */
28
    private $eventDispatcher;
29
30
    /**
31
     * @var Compiler
32
     */
33
    private $compiler;
34
35 5
    public function __construct(Compiler $compiler, EventDispatcherInterface $eventDispatcher)
36
    {
37 5
        $this->eventDispatcher = $eventDispatcher;
38 5
        $this->compiler = $compiler;
39 5
    }
40
41
    /**
42
     * @Route("", methods={"GET"})
43
     */
44 5
    public function getUpdatedPassbook(Request $request, string $passTypeIdentifier, string $serialNumber): Response
45
    {
46 5
        $event = new RetrieveUpdatedPassbookEvent(
47 5
            $passTypeIdentifier,
48 5
            $serialNumber,
49 5
            $this->getAuthenticationToken($request)
50
        );
51 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...
52
53 5
        if ($event->getStatus()->isUnhandled()) {
54 1
            throw new LogicException('RetrieveUpdatedPassbookEvent was not handled. Please implement a listener for this event.');
55
        }
56
57 4
        if ($event->getStatus()->isNotAuthorized()) {
58 1
            return new JsonResponse([], Response::HTTP_UNAUTHORIZED);
59
        }
60
61 3
        if ($event->getStatus()->isNotModified()) {
62 1
            return new JsonResponse([], Response::HTTP_NOT_MODIFIED);
63
        }
64
65 2
        if ($event->getStatus()->isSuccessful()) {
66 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...
67 1
            $lastModified = $event->getLastModified();
68 1
            $lastModified = $lastModified->setTimezone(new \DateTimeZone('GMT'));
69
70 1
            $response = new Response($data);
71 1
            $response->headers->set('Content-Description', 'File Transfer');
72 1
            $response->headers->set('Content-Type', 'application/vnd.apple.pkpass');
73 1
            $response->headers->set('Content-Disposition', 'filename="pass.pkpass"');
74 1
            $response->headers->set('Last-Modified', $lastModified->format('D, d M Y H:i:s \G\M\T'));
75
76 1
            return $response;
77
        }
78
79 1
        throw new LogicException('RetrieveUpdatedPassbookEvent was not handled correctly. Unexpected status was set.');
80
    }
81
}
82
83
84