1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Speicher210\FastbillBundle\Controller\Notification; |
4
|
|
|
|
5
|
|
|
use Speicher210\Fastbill\Api\Model\Notification\NotificationPayloadInterface; |
6
|
|
|
use Speicher210\FastbillBundle\Event\NotificationEventInterface; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
11
|
|
|
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Abstract controller for notification controllers. |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Handle a notification. |
20
|
|
|
* |
21
|
|
|
* @param Request $request The incoming request. |
22
|
|
|
* @param NotificationEventInterface $event The event to raise when handling the notification. |
23
|
|
|
* @param string $payloadDataClass The class for the payload. |
24
|
|
|
* @return Response |
25
|
|
|
*/ |
26
|
54 |
|
protected function handleNotification(Request $request, NotificationEventInterface $event, $payloadDataClass) |
27
|
|
|
{ |
28
|
54 |
|
$payloadData = $this->getPayload($request, $payloadDataClass); |
29
|
|
|
|
30
|
42 |
|
if ($event->getNotificationType() !== $payloadData->getType()) { |
31
|
6 |
|
$message = 'Invalid type in hook call. Expected hook type "%s", got "%s"'; |
32
|
6 |
|
throw new BadRequestHttpException(sprintf($message, $event->getNotificationType(), $payloadData->getType())); |
33
|
|
|
} |
34
|
|
|
|
35
|
36 |
|
$event->setPayloadData($payloadData); |
36
|
|
|
|
37
|
36 |
|
$eventName = 'speicher210_fastbill.notification.incoming.' . $event->getNotificationType(); |
38
|
36 |
|
$this->get('event_dispatcher')->dispatch($eventName, $event); |
39
|
|
|
|
40
|
36 |
|
if ($event->isPropagationStopped()) { |
41
|
|
|
return new Response(null, Response::HTTP_INTERNAL_SERVER_ERROR); |
42
|
|
|
} else { |
43
|
36 |
|
return new Response(null, Response::HTTP_NO_CONTENT); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the notification payload data. |
49
|
|
|
* |
50
|
|
|
* @param Request $request The request. |
51
|
|
|
* @param string $payloadDataClass The deserialization class. |
52
|
|
|
* @return NotificationPayloadInterface |
53
|
|
|
*/ |
54
|
54 |
|
protected function getPayload(Request $request, $payloadDataClass) |
55
|
|
|
{ |
56
|
54 |
|
if ('json' !== $request->getContentType()) { |
57
|
6 |
|
throw new UnsupportedMediaTypeHttpException(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
try { |
61
|
48 |
|
return $this |
62
|
48 |
|
->get('speicher210_fastbill.serializer') |
63
|
48 |
|
->deserialize( |
64
|
48 |
|
$request->getContent(), |
65
|
48 |
|
$payloadDataClass, |
66
|
|
|
'json' |
67
|
48 |
|
); |
68
|
6 |
|
} catch (\Exception $e) { |
69
|
6 |
|
throw new BadRequestHttpException('Invalid hook payload.', $e); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|