-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNotification.php
More file actions
103 lines (86 loc) · 3.26 KB
/
Notification.php
File metadata and controls
103 lines (86 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php declare(strict_types=1);
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Webhook Module for PHP
*
* Copyright (c) 2021 Adyen N.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
*/
namespace Adyen\Webhook;
use Adyen\Webhook\Exception\InvalidDataException;
class Notification
{
const PROPERTY_EVENT_CODE = 'eventCode';
const PROPERTY_SUCCESS = 'success';
const ADDITIONAL_DATA ='additionalData';
const REQUIRED_PROPERTIES = [
self::PROPERTY_EVENT_CODE,
self::PROPERTY_SUCCESS
];
public $eventCode;
public $success;
public $additionalData;
/**
* @throws InvalidDataException
*/
public static function createItem(array $notificationData): Notification
{
self::validateNotificationData($notificationData);
$notification = new self();
$notification->eventCode = $notificationData[self::PROPERTY_EVENT_CODE];
$notification->success = $notificationData[self::PROPERTY_SUCCESS];
if (isset($notificationData[self::ADDITIONAL_DATA]) && is_array($notificationData[self::ADDITIONAL_DATA])) {
$notification->additionalData = $notificationData[self::ADDITIONAL_DATA];
}
return $notification;
}
public function getEventCode(): string
{
return $this->eventCode;
}
public function getAdditionalData(): array
{
return $this->additionalData ?? [];
}
public function isSuccess(): bool
{
return in_array($this->success, [true, "true"], true);
}
private static function validateNotificationData(array $data)
{
$missing = [];
$invalid = [];
$eventCodes = new \ReflectionClass(EventCodes::class);
foreach (self::REQUIRED_PROPERTIES as $property) {
// If required data is missing
if (!isset($data[$property])) {
$missing[] = $property;
}
// If an invalid event code is passed
if (isset($data[self::PROPERTY_EVENT_CODE]) &&
!in_array($data[self::PROPERTY_EVENT_CODE], $eventCodes->getConstants())) {
$invalid[self::PROPERTY_EVENT_CODE] = $data[self::PROPERTY_EVENT_CODE];
}
}
if (!empty($missing)) {
throw new InvalidDataException('Field(s) missing from notification data: ' . join(', ', $missing));
}
if (!empty($invalid)) {
throw new InvalidDataException('Invalid value for the field(s) with key(s): ' . join(', ', $invalid));
}
}
}