forked from webfox/laravel-xero-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebhookEvent.php
executable file
·97 lines (76 loc) · 2.45 KB
/
WebhookEvent.php
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
<?php
namespace Webfox\Xero;
use Illuminate\Support\Collection;
use XeroAPI\XeroPHP\Api\AccountingApi;
class WebhookEvent
{
/** @var Collection */
protected $properties;
/** @var AccountingApi */
protected $accountingApi;
/** @var OauthCredentialManager */
protected $credentialManager;
public function __construct(OauthCredentialManager $credentialManager, AccountingApi $accountingApi, $event)
{
$this->accountingApi = $accountingApi;
$this->properties = new Collection($event);
if (!$this->properties->has(['resourceUrl', 'resourceId', 'eventDateUtc', 'eventType', 'eventCategory', 'tenantId', 'tenantType',])) {
throw new \Exception("The event payload was malformed; missing required field");
}
$this->credentialManager = $credentialManager;
}
public function getResourceUrl()
{
return $this->properties->get('resourceUrl');
}
public function getResourceId()
{
return $this->properties->get('resourceId');
}
public function getEventDateUtc()
{
return $this->properties->get('eventDateUtc');
}
public function getEventDate()
{
return new \DateTime($this->getEventDateUtc());
}
public function getEventType()
{
return $this->properties->get('eventType');
}
public function getEventCategory()
{
return $this->properties->get('eventCategory');
}
public function getEventClass()
{
if ($this->getEventCategory() === 'INVOICE') {
return \XeroApi\XeroPHP\Models\Accounting\Invoice::class;
}
if ($this->getEventCategory() === 'CONTACT') {
return \XeroApi\XeroPHP\Models\Accounting\Contact::class;
}
}
public function getTenantId()
{
return $this->properties->get('tenantId');
}
public function getTenantType()
{
return $this->properties->get('tenantType');
}
public function getResource()
{
if ($this->getEventCategory() === 'INVOICE') {
return $this->accountingApi
->getInvoice($this->credentialManager->getTenantId(), $this->getResourceId())
->getInvoices()[0];
}
if ($this->getEventCategory() === 'CONTACT') {
return $this->accountingApi
->getContact($this->credentialManager->getTenantId(), $this->getResourceId())
->getContacts()[0];
}
}
}