Skip to content

Commit 47af694

Browse files
author
Jiří Novák
committed
feat: AJDA-443 add additional info about flow and job for project notifications
1 parent 42baf28 commit 47af694

6 files changed

Lines changed: 206 additions & 2 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Requests\PostNotification;
6+
7+
class FlowInfo
8+
{
9+
private string $id;
10+
11+
private string $name;
12+
13+
private string $url;
14+
15+
public function __construct(string $id, string $name, string $url)
16+
{
17+
$this->id = $id;
18+
$this->name = $name;
19+
$this->url = $url;
20+
}
21+
22+
public function jsonSerialize(): array
23+
{
24+
return [
25+
'id' => $this->id,
26+
'name' => $this->name,
27+
'url' => $this->url,
28+
];
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\NotificationClient\Requests\PostNotification;
6+
7+
class JobInfo
8+
{
9+
private string $id;
10+
11+
private string $url;
12+
13+
public function __construct(string $id, string $url)
14+
{
15+
$this->id = $id;
16+
$this->url = $url;
17+
}
18+
19+
public function jsonSerialize(): array
20+
{
21+
return [
22+
'id' => $this->id,
23+
'url' => $this->url,
24+
];
25+
}
26+
}

src/Requests/PostNotification/ProjectEmail.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,30 @@ class ProjectEmail implements NotificationInterface
1515
private string $title;
1616
private string $message;
1717
private EmailRecipient $recipient;
18+
private ?FlowInfo $flow;
19+
private ?JobInfo $job;
1820

1921
public function __construct(
2022
EmailRecipient $recipient,
2123
string $projectId,
2224
string $projectName,
2325
string $title,
2426
string $message,
27+
?FlowInfo $flow = null,
28+
?JobInfo $job = null,
2529
) {
2630
$this->recipient = $recipient;
2731
$this->projectId = $projectId;
2832
$this->projectName = $projectName;
2933
$this->title = $title;
3034
$this->message = $message;
35+
$this->flow = $flow;
36+
$this->job = $job;
3137
}
3238

3339
public function jsonSerialize(): array
3440
{
35-
return [
41+
$notification = [
3642
'type' => self::TYPE,
3743
'recipient' => $this->recipient->jsonSerialize(),
3844
'data' => [
@@ -44,5 +50,15 @@ public function jsonSerialize(): array
4450
'message' => $this->message,
4551
],
4652
];
53+
54+
if ($this->flow !== null) {
55+
$notification['data']['flow'] = $this->flow->jsonSerialize();
56+
}
57+
58+
if ($this->job !== null) {
59+
$notification['data']['job'] = $this->job->jsonSerialize();
60+
}
61+
62+
return $notification;
4763
}
4864
}

src/Requests/PostNotification/ProjectWebhook.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,30 @@ class ProjectWebhook implements NotificationInterface
1515
private string $title;
1616
private ?string $message;
1717
private WebhookRecipient $recipient;
18+
private ?FlowInfo $flow;
19+
private ?JobInfo $job;
1820

1921
public function __construct(
2022
WebhookRecipient $recipient,
2123
string $projectId,
2224
string $projectName,
2325
string $title,
2426
?string $message,
27+
?FlowInfo $flow = null,
28+
?JobInfo $job = null,
2529
) {
2630
$this->recipient = $recipient;
2731
$this->projectId = $projectId;
2832
$this->projectName = $projectName;
2933
$this->title = $title;
3034
$this->message = $message;
35+
$this->flow = $flow;
36+
$this->job = $job;
3137
}
3238

3339
public function jsonSerialize(): array
3440
{
35-
return [
41+
$notification = [
3642
'type' => self::TYPE,
3743
'recipient' => $this->recipient->jsonSerialize(),
3844
'data' => [
@@ -44,5 +50,15 @@ public function jsonSerialize(): array
4450
'message' => $this->message,
4551
],
4652
];
53+
54+
if ($this->flow !== null) {
55+
$notification['data']['flow'] = $this->flow->jsonSerialize();
56+
}
57+
58+
if ($this->job !== null) {
59+
$notification['data']['job'] = $this->job->jsonSerialize();
60+
}
61+
62+
return $notification;
4763
}
4864
}

tests/Requests/PostNotification/ProjectEmailTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Keboola\NotificationClient\Tests\Requests\PostNotification;
66

7+
use Keboola\NotificationClient\Requests\PostNotification\FlowInfo;
8+
use Keboola\NotificationClient\Requests\PostNotification\JobInfo;
79
use Keboola\NotificationClient\Requests\PostNotification\ProjectEmail;
810
use Keboola\NotificationClient\Requests\PostSubscription\EmailRecipient;
911
use PHPUnit\Framework\TestCase;
@@ -45,4 +47,60 @@ public function testJsonSerialize(): void
4547
$projectEmail->jsonSerialize(),
4648
);
4749
}
50+
51+
public function testJsonSerializeWithFlowAndJob(): void
52+
{
53+
$recipient = new EmailRecipient('john.doe@example.com');
54+
$projectId = '12345';
55+
$projectName = 'Test Project';
56+
$title = 'Test Notification';
57+
$message = 'This is a test notification message';
58+
$flow = new FlowInfo(
59+
'flow-123',
60+
'My Test Flow',
61+
'https://connection.keboola.com/flows/123',
62+
);
63+
$job = new JobInfo(
64+
'job-123',
65+
'https://connection.keboola.com/jobs/123',
66+
);
67+
68+
$projectEmail = new ProjectEmail(
69+
$recipient,
70+
$projectId,
71+
$projectName,
72+
$title,
73+
$message,
74+
$flow,
75+
$job,
76+
);
77+
78+
self::assertSame(
79+
[
80+
'type' => 'direct-project-email',
81+
'recipient' => [
82+
'channel' => 'email',
83+
'address' => 'john.doe@example.com',
84+
],
85+
'data' => [
86+
'project' => [
87+
'id' => $projectId,
88+
'name' => $projectName,
89+
],
90+
'title' => $title,
91+
'message' => $message,
92+
'flow' => [
93+
'id' => 'flow-123',
94+
'name' => 'My Test Flow',
95+
'url' => 'https://connection.keboola.com/flows/123',
96+
],
97+
'job' => [
98+
'id' => 'job-123',
99+
'url' => 'https://connection.keboola.com/jobs/123',
100+
],
101+
],
102+
],
103+
$projectEmail->jsonSerialize(),
104+
);
105+
}
48106
}

tests/Requests/PostNotification/ProjectWebhookTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Keboola\NotificationClient\Tests\Requests\PostNotification;
66

7+
use Keboola\NotificationClient\Requests\PostNotification\FlowInfo;
8+
use Keboola\NotificationClient\Requests\PostNotification\JobInfo;
79
use Keboola\NotificationClient\Requests\PostNotification\ProjectWebhook;
810
use Keboola\NotificationClient\Requests\PostSubscription\WebhookRecipient;
911
use PHPUnit\Framework\TestCase;
@@ -81,4 +83,60 @@ public function testJsonSerializeWithNullMessage(): void
8183
$projectWebhook->jsonSerialize(),
8284
);
8385
}
86+
87+
public function testJsonSerializeWithFlowAndJob(): void
88+
{
89+
$recipient = new WebhookRecipient('https://example.com/webhook');
90+
$projectId = '12345';
91+
$projectName = 'Test Project';
92+
$title = 'Test Notification';
93+
$message = 'This is a test notification message';
94+
$flow = new FlowInfo(
95+
'flow-123',
96+
'My Test Flow',
97+
'https://connection.keboola.com/flows/123',
98+
);
99+
$job = new JobInfo(
100+
'job-123',
101+
'https://connection.keboola.com/jobs/123',
102+
);
103+
104+
$projectWebhook = new ProjectWebhook(
105+
$recipient,
106+
$projectId,
107+
$projectName,
108+
$title,
109+
$message,
110+
$flow,
111+
$job,
112+
);
113+
114+
self::assertSame(
115+
[
116+
'type' => 'direct-project-webhook',
117+
'recipient' => [
118+
'channel' => 'webhook',
119+
'url' => 'https://example.com/webhook',
120+
],
121+
'data' => [
122+
'project' => [
123+
'id' => $projectId,
124+
'name' => $projectName,
125+
],
126+
'title' => $title,
127+
'message' => $message,
128+
'flow' => [
129+
'id' => 'flow-123',
130+
'name' => 'My Test Flow',
131+
'url' => 'https://connection.keboola.com/flows/123',
132+
],
133+
'job' => [
134+
'id' => 'job-123',
135+
'url' => 'https://connection.keboola.com/jobs/123',
136+
],
137+
],
138+
],
139+
$projectWebhook->jsonSerialize(),
140+
);
141+
}
84142
}

0 commit comments

Comments
 (0)