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); |
|
|
|
|
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()); |
|
|
|
|
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
|
|
|
|
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: