Skip to content

Commit 436e630

Browse files
committed
Add object, brand and user id getters to WebhookEvent, add tests for getters
1 parent 35760ae commit 436e630

File tree

4 files changed

+201
-4
lines changed

4 files changed

+201
-4
lines changed

src/Events/Version1EventFactory.php

+3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ public function getEvents(array $data): array
2626
$data['hook_code'],
2727
$data['hook_log_id'],
2828
$data['object_type'],
29+
$data['object_id'],
2930
$data['object'],
31+
$data['brand_id'],
3032
$data['brand'],
3133
$data['actor_type'],
34+
$data['actor_id'],
3235
$data['actor']
3336
),
3437
];

src/Events/WebhookEvent.php

+50-2
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,21 @@ class WebhookEvent implements JsonSerializable
4141
*/
4242
protected $objectType;
4343

44+
/**
45+
* @var string
46+
*/
47+
protected $objectId;
48+
4449
/**
4550
* @var array
4651
*/
4752
protected $objectData;
4853

54+
/**
55+
* @var string
56+
*/
57+
protected $brandId;
58+
4959
/**
5060
* @var array
5161
*/
@@ -56,6 +66,11 @@ class WebhookEvent implements JsonSerializable
5666
*/
5767
protected $actorType;
5868

69+
/**
70+
* @var string|null
71+
*/
72+
protected $actorId;
73+
5974
/**
6075
* @var array|null
6176
*/
@@ -68,9 +83,12 @@ public function __construct(
6883
string $hookCode,
6984
string $hookLogId,
7085
string $objectType,
86+
string $objectId,
7187
array $objectData,
88+
string $brandId,
7289
array $brandData,
7390
?string $actorType,
91+
?string $actorId,
7492
?array $actorData
7593
) {
7694
$this->id = $id;
@@ -79,9 +97,12 @@ public function __construct(
7997
$this->hookCode = $hookCode;
8098
$this->hookLogId = $hookLogId;
8199
$this->objectType = $objectType;
100+
$this->objectId = $objectId;
82101
$this->objectData = $objectData;
102+
$this->brandId = $brandId;
83103
$this->brandData = $brandData;
84104
$this->actorType = $actorType;
105+
$this->actorId = $actorId;
85106
$this->actorData = $actorData;
86107
}
87108

@@ -133,6 +154,14 @@ public function getObjectType(): string
133154
return $this->objectType;
134155
}
135156

157+
/**
158+
* Get the object ID for this event.
159+
*/
160+
public function getObjectId(): string
161+
{
162+
return $this->objectId;
163+
}
164+
136165
/**
137166
* Get the object data for this event.
138167
*/
@@ -142,21 +171,37 @@ public function getObjectData(): array
142171
}
143172

144173
/**
145-
* Get the actor type, if known.
174+
* Get the actor type, if applicable.
146175
*/
147176
public function getActorType(): ?string
148177
{
149178
return $this->actorType;
150179
}
151180

152181
/**
153-
* Get the actor data, if known.
182+
* Get the actor ID, if applicable.
183+
*/
184+
public function getActorId(): ?string
185+
{
186+
return $this->actorId;
187+
}
188+
189+
/**
190+
* Get the actor data, if applicable.
154191
*/
155192
public function getActorData(): ?array
156193
{
157194
return $this->actorData;
158195
}
159196

197+
/**
198+
* Get the brand ID for this event.
199+
*/
200+
public function getBrandId(): string
201+
{
202+
return $this->brandId;
203+
}
204+
160205
/**
161206
* Get the contextual brand data for this event.
162207
*/
@@ -177,9 +222,12 @@ public function toArray()
177222
'hook_code' => $this->getHookCode(),
178223
'hook_log_id' => $this->getHookLogId(),
179224
'object_type' => $this->getObjectType(),
225+
'object_id' => $this->getObjectId(),
180226
'object' => $this->getObjectData(),
181227
'actor_type' => $this->getActorType(),
228+
'actor_id' => $this->getActorId(),
182229
'actor' => $this->getActorData(),
230+
'brand_id' => $this->getBrandId(),
183231
'brand' => $this->getBrandData(),
184232
];
185233
}

tests/WebhookEventTest.php

+147-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,154 @@ public function serializes_to_json()
1717
$factory = new WebhookFactory();
1818
$webhook = $factory->fromString($this->getValidV1Payload());
1919

20-
$eventAsJson = '{"id":"37506382-d5e9-0313-95a5-124867794de5","datetime":"2023-08-07 20:59:37","category_code":"client","hook_code":"user_created_unverified_client_hook","hook_log_id":"d9465824-5730-9190-97eb-d128ed6e3d70","object_type":"client","object":{"id":"506382d5-e903-1365-6d6a-5124867794de","import_id":null,"staged_import":false,"external_id":null,"support_pin":null,"support_pin_expiry_datetime":null,"firstname":"Harry","lastname":"L","has_login":true,"email":"harry+customer@upmind.com","location_source":null,"location_town":null,"location_country_code":null,"location_ip":null,"user_id":"e5750263-4647-9ed1-26a2-1053288d79e9","upmind_org_user_id":null,"created_at":"2023-08-07 20:59:34","updated_at":"2023-08-07 20:59:34","reseller_account_id":null,"interface_language_id":"869d9865-7e5d-4021-e6c9-1837690e4323","document_language_id":"869d9865-7e5d-4021-e6c9-1837690e4323","deleted_at":null,"verified":false,"apply_credit":0,"credit":null,"consolidate_invoice":0,"consolidation_day":null,"bf_id":"none","ip_address":null,"enabled_2fa":false,"provider_2fa_id":null,"org_id":"e5750263-4647-9ed1-26a2-1053288d79e9","picture":0,"brand_id":"905d39e2-d364-0717-d0a4-12e598467803","fraud_policy":1,"fraud_status":1,"last_login":null,"failed_login_attempts":0,"is_guest":false,"notifications_disabled":false,"meta":null,"order_template_code":null,"invoice_consolidation_enabled":2,"invoice_consolidation_base_rule":null,"invoice_consolidation_base_rule_date_of_month_day":null,"invoice_consolidation_base_rule_day_of_week":null,"block_new_tickets_from_email":false,"interface_language_code":"en","document_language_code":"en","fullname":"Harry L","public_name":"Harry L.","secret_2fa_exists":false,"image_url":null,"topup_enabled":null,"upmind_org_user":null,"upmind_package_limits":[],"first_name":"Harry","last_name":"L","full_name":"Harry L","login_email":"harry+customer@upmind.com","notification_email":"harry+customer@upmind.com","twofa_enabled":false,"twofa_provider":null,"reg_hash":"3d43389efc170ff1dbe0b064a4ba8407ef04dfd9","reg_hash_expiry":"","avatar_src":null,"interface_language":"en","document_language":"en","has_password":false,"default_payment_details":null,"default_address":null,"default_email":{"id":"50263464-79ed-1278-760a-21053288d79e","client_id":"506382d5-e903-1365-6d6a-5124867794de","email":"harry+customer@upmind.com","reg_hash":"e21e8c4cfb95232c188940a7eb0347b198ddfeb2","reg_hash_expiry":"2024-08-07 20:59:34","type":"Personal","default":true,"verified":false,"created_at":"2023-08-07 20:59:34","updated_at":"2023-08-07 20:59:34"},"default_phone":null,"custom_field_values":[]},"actor_type":"user","actor":null,"brand":{"id":"905d39e2-d364-0717-d0a4-12e598467803","name":"ExampleHost","code":"abfya8btxnlb","prefix":"abfya8btxnlb","customer_portal_domain":"abfya8btxnlb.upmind.local","staff_portal_domain":"abfya8btxnlb.upmind.local","company_name":"ExampleHost","company_address":"123 Fake Street,\nFakeville,\nFA1 7KE,\nGB","company_phone":"01234 567890","company_email":"harry@upmind.com","country":"GB","language":"en","currency":"GBP","tax_type":0,"vat_number":"","payment_days_term":0,"create_invoice_term":0,"style":null,"logo_src":null,"created_at":"2023-08-07 20:58:21","updated_at":"2023-08-07 20:58:21","organisation":{"id":"e5750263-4647-9ed1-26a2-1053288d79e9","name":"ExampleHost","code":"abfya8btxnlb","verified":true,"completed":false,"status":null,"staff_portal_domain":"abfya8btxnlb.upmind.local","reg_hash":null,"reg_hash_expiry":null}}}';
20+
$eventAsJson = '{"id":"37506382-d5e9-0313-95a5-124867794de5","datetime":"2023-08-07 20:59:37","category_code":"client","hook_code":"user_created_unverified_client_hook","hook_log_id":"d9465824-5730-9190-97eb-d128ed6e3d70","object_type":"client","object_id":"506382d5-e903-1365-6d6a-5124867794de","object":{"id":"506382d5-e903-1365-6d6a-5124867794de","import_id":null,"staged_import":false,"external_id":null,"support_pin":null,"support_pin_expiry_datetime":null,"firstname":"Harry","lastname":"L","has_login":true,"email":"harry+customer@upmind.com","location_source":null,"location_town":null,"location_country_code":null,"location_ip":null,"user_id":"e5750263-4647-9ed1-26a2-1053288d79e9","upmind_org_user_id":null,"created_at":"2023-08-07 20:59:34","updated_at":"2023-08-07 20:59:34","reseller_account_id":null,"interface_language_id":"869d9865-7e5d-4021-e6c9-1837690e4323","document_language_id":"869d9865-7e5d-4021-e6c9-1837690e4323","deleted_at":null,"verified":false,"apply_credit":0,"credit":null,"consolidate_invoice":0,"consolidation_day":null,"bf_id":"none","ip_address":null,"enabled_2fa":false,"provider_2fa_id":null,"org_id":"e5750263-4647-9ed1-26a2-1053288d79e9","picture":0,"brand_id":"905d39e2-d364-0717-d0a4-12e598467803","fraud_policy":1,"fraud_status":1,"last_login":null,"failed_login_attempts":0,"is_guest":false,"notifications_disabled":false,"meta":null,"order_template_code":null,"invoice_consolidation_enabled":2,"invoice_consolidation_base_rule":null,"invoice_consolidation_base_rule_date_of_month_day":null,"invoice_consolidation_base_rule_day_of_week":null,"block_new_tickets_from_email":false,"interface_language_code":"en","document_language_code":"en","fullname":"Harry L","public_name":"Harry L.","secret_2fa_exists":false,"image_url":null,"topup_enabled":null,"upmind_org_user":null,"upmind_package_limits":[],"first_name":"Harry","last_name":"L","full_name":"Harry L","login_email":"harry+customer@upmind.com","notification_email":"harry+customer@upmind.com","twofa_enabled":false,"twofa_provider":null,"reg_hash":"3d43389efc170ff1dbe0b064a4ba8407ef04dfd9","reg_hash_expiry":"","avatar_src":null,"interface_language":"en","document_language":"en","has_password":false,"default_payment_details":null,"default_address":null,"default_email":{"id":"50263464-79ed-1278-760a-21053288d79e","client_id":"506382d5-e903-1365-6d6a-5124867794de","email":"harry+customer@upmind.com","reg_hash":"e21e8c4cfb95232c188940a7eb0347b198ddfeb2","reg_hash_expiry":"2024-08-07 20:59:34","type":"Personal","default":true,"verified":false,"created_at":"2023-08-07 20:59:34","updated_at":"2023-08-07 20:59:34"},"default_phone":null,"custom_field_values":[]},"actor_type":"user","actor_id":"e5750263-4647-9ed1-26a2-1053288d79e9","actor":null,"brand_id":"905d39e2-d364-0717-d0a4-12e598467803","brand":{"id":"905d39e2-d364-0717-d0a4-12e598467803","name":"ExampleHost","code":"abfya8btxnlb","prefix":"abfya8btxnlb","customer_portal_domain":"abfya8btxnlb.upmind.local","staff_portal_domain":"abfya8btxnlb.upmind.local","company_name":"ExampleHost","company_address":"123 Fake Street,\nFakeville,\nFA1 7KE,\nGB","company_phone":"01234 567890","company_email":"harry@upmind.com","country":"GB","language":"en","currency":"GBP","tax_type":0,"vat_number":"","payment_days_term":0,"create_invoice_term":0,"style":null,"logo_src":null,"created_at":"2023-08-07 20:58:21","updated_at":"2023-08-07 20:58:21","organisation":{"id":"e5750263-4647-9ed1-26a2-1053288d79e9","name":"ExampleHost","code":"abfya8btxnlb","verified":true,"completed":false,"status":null,"staff_portal_domain":"abfya8btxnlb.upmind.local","reg_hash":null,"reg_hash_expiry":null}}}';
2121

2222
$this->assertEquals($eventAsJson, json_encode($webhook->getEvents()[0]));
2323
}
24+
25+
/** @test */
26+
public function getters_return_correct_data()
27+
{
28+
$factory = new WebhookFactory();
29+
$webhook = $factory->fromString($this->getValidV1Payload());
30+
31+
$event = $webhook->getEvents()[0];
32+
33+
$objectData = [
34+
"id" => "506382d5-e903-1365-6d6a-5124867794de",
35+
"import_id" => null,
36+
"staged_import" => false,
37+
"external_id" => null,
38+
"support_pin" => null,
39+
"support_pin_expiry_datetime" => null,
40+
"firstname" => "Harry",
41+
"lastname" => "L",
42+
"has_login" => true,
43+
"email" => "harry+customer@upmind.com",
44+
"location_source" => null,
45+
"location_town" => null,
46+
"location_country_code" => null,
47+
"location_ip" => null,
48+
"user_id" => "e5750263-4647-9ed1-26a2-1053288d79e9",
49+
"upmind_org_user_id" => null,
50+
"created_at" => "2023-08-07 20:59:34",
51+
"updated_at" => "2023-08-07 20:59:34",
52+
"reseller_account_id" => null,
53+
"interface_language_id" => "869d9865-7e5d-4021-e6c9-1837690e4323",
54+
"document_language_id" => "869d9865-7e5d-4021-e6c9-1837690e4323",
55+
"deleted_at" => null,
56+
"verified" => false,
57+
"apply_credit" => 0,
58+
"credit" => null,
59+
"consolidate_invoice" => 0,
60+
"consolidation_day" => null,
61+
"bf_id" => "none",
62+
"ip_address" => null,
63+
"enabled_2fa" => false,
64+
"provider_2fa_id" => null,
65+
"org_id" => "e5750263-4647-9ed1-26a2-1053288d79e9",
66+
"picture" => 0,
67+
"brand_id" => "905d39e2-d364-0717-d0a4-12e598467803",
68+
"fraud_policy" => 1,
69+
"fraud_status" => 1,
70+
"last_login" => null,
71+
"failed_login_attempts" => 0,
72+
"is_guest" => false,
73+
"notifications_disabled" => false,
74+
"meta" => null,
75+
"order_template_code" => null,
76+
"invoice_consolidation_enabled" => 2,
77+
"invoice_consolidation_base_rule" => null,
78+
"invoice_consolidation_base_rule_date_of_month_day" => null,
79+
"invoice_consolidation_base_rule_day_of_week" => null,
80+
"block_new_tickets_from_email" => false,
81+
"interface_language_code" => "en",
82+
"document_language_code" => "en",
83+
"fullname" => "Harry L",
84+
"public_name" => "Harry L.",
85+
"secret_2fa_exists" => false,
86+
"image_url" => null,
87+
"topup_enabled" => null,
88+
"upmind_org_user" => null,
89+
"upmind_package_limits" => [],
90+
"first_name" => "Harry",
91+
"last_name" => "L",
92+
"full_name" => "Harry L",
93+
"login_email" => "harry+customer@upmind.com",
94+
"notification_email" => "harry+customer@upmind.com",
95+
"twofa_enabled" => false,
96+
"twofa_provider" => null,
97+
"reg_hash" => "3d43389efc170ff1dbe0b064a4ba8407ef04dfd9",
98+
"reg_hash_expiry" => "",
99+
"avatar_src" => null,
100+
"interface_language" => "en",
101+
"document_language" => "en",
102+
"has_password" => false,
103+
"default_payment_details" => null,
104+
"default_address" => null,
105+
"default_email" => [
106+
"id" => "50263464-79ed-1278-760a-21053288d79e",
107+
"client_id" => "506382d5-e903-1365-6d6a-5124867794de",
108+
"email" => "harry+customer@upmind.com",
109+
"reg_hash" => "e21e8c4cfb95232c188940a7eb0347b198ddfeb2",
110+
"reg_hash_expiry" => "2024-08-07 20:59:34",
111+
"type" => "Personal",
112+
"default" => true,
113+
"verified" => false,
114+
"created_at" => "2023-08-07 20:59:34",
115+
"updated_at" => "2023-08-07 20:59:34",
116+
],
117+
"default_phone" => null,
118+
"custom_field_values" => [],
119+
];
120+
121+
$brandData = [
122+
"id" => "905d39e2-d364-0717-d0a4-12e598467803",
123+
"name" => "ExampleHost",
124+
"code" => "abfya8btxnlb",
125+
"prefix" => "abfya8btxnlb",
126+
"customer_portal_domain" => "abfya8btxnlb.upmind.local",
127+
"staff_portal_domain" => "abfya8btxnlb.upmind.local",
128+
"company_name" => "ExampleHost",
129+
"company_address" => "123 Fake Street,\nFakeville,\nFA1 7KE,\nGB",
130+
"company_phone" => "01234 567890",
131+
"company_email" => "harry@upmind.com",
132+
"country" => "GB",
133+
"language" => "en",
134+
"currency" => "GBP",
135+
"tax_type" => 0,
136+
"vat_number" => "",
137+
"payment_days_term" => 0,
138+
"create_invoice_term" => 0,
139+
"style" => null,
140+
"logo_src" => null,
141+
"created_at" => "2023-08-07 20:58:21",
142+
"updated_at" => "2023-08-07 20:58:21",
143+
"organisation" => [
144+
"id" => "e5750263-4647-9ed1-26a2-1053288d79e9",
145+
"name" => "ExampleHost",
146+
"code" => "abfya8btxnlb",
147+
"verified" => true,
148+
"completed" => false,
149+
"status" => null,
150+
"staff_portal_domain" => "abfya8btxnlb.upmind.local",
151+
"reg_hash" => null,
152+
"reg_hash_expiry" => null,
153+
],
154+
];
155+
156+
$this->assertEquals('37506382-d5e9-0313-95a5-124867794de5', $event->getId());
157+
$this->assertEquals('2023-08-07 20:59:37', $event->getDatetime());
158+
$this->assertEquals('client', $event->getCategoryCode());
159+
$this->assertEquals('user_created_unverified_client_hook', $event->getHookCode());
160+
$this->assertEquals('d9465824-5730-9190-97eb-d128ed6e3d70', $event->getHookLogId());
161+
$this->assertEquals('client', $event->getObjectType());
162+
$this->assertEquals('506382d5-e903-1365-6d6a-5124867794de', $event->getObjectId());
163+
$this->assertEquals($objectData, $event->getObjectData());
164+
$this->assertEquals('905d39e2-d364-0717-d0a4-12e598467803', $event->getBrandId());
165+
$this->assertEquals($brandData, $event->getBrandData());
166+
$this->assertEquals('user', $event->getActorType());
167+
$this->assertEquals('e5750263-4647-9ed1-26a2-1053288d79e9', $event->getActorId());
168+
$this->assertEquals(null, $event->getActorData());
169+
}
24170
}

0 commit comments

Comments
 (0)