1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelPaddle\Events; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
abstract class Event |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $webhookData; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Illuminate\Http\Request |
17
|
|
|
*/ |
18
|
|
|
protected $request; |
19
|
|
|
|
20
|
|
|
public function __construct(array $webhookData, Request $request) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$this->webhookData = $webhookData; |
23
|
|
|
$this->request = $request; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Getter for all data. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function all(): array |
32
|
|
|
{ |
33
|
|
|
return $this->webhookData; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Getter for the request. |
38
|
|
|
* |
39
|
|
|
* @return \Illuminate\Http\Request |
40
|
|
|
*/ |
41
|
|
|
public function getRequest(): Request |
42
|
|
|
{ |
43
|
|
|
return $this->request; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Getter for the webhook data. |
48
|
|
|
* |
49
|
|
|
* @param string $key |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function __get(string $key) |
53
|
|
|
{ |
54
|
|
|
$value = $this->webhookData[$key]; |
55
|
|
|
|
56
|
|
|
if ($key === 'passthrough') { |
57
|
|
|
$result = json_decode($value, true); |
58
|
|
|
|
59
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Determine if an attribute exists on the webhook data. |
69
|
|
|
* |
70
|
|
|
* @param string $key |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function __isset($key) |
74
|
|
|
{ |
75
|
|
|
return isset($this->webhookData[$key]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Generates the event class name with the 'alert_name' attribute from |
80
|
|
|
* the data and fires the event with the data. |
81
|
|
|
* |
82
|
|
|
* @param array $data |
83
|
|
|
* @param \Illuminate\Http\Request $request |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public static function fire(array $data, Request $request) |
87
|
|
|
{ |
88
|
|
|
$event = Str::studly($data['alert_name'] ?? 'generic_webhook'); |
89
|
|
|
|
90
|
|
|
$eventClass = __NAMESPACE__ . '\\' . $event; |
91
|
|
|
|
92
|
|
|
return event(new $eventClass($data, $request)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|