Skip to content

Commit f9e8a38

Browse files
committed
refactor: webhook service
1 parent c8a452f commit f9e8a38

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

src/Services/WebhookService.php

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,42 @@ public function __construct(Client $client)
1515
$this->client = $client;
1616
}
1717

18-
public function eventApproved(string $event): bool
18+
public function eventRequestApproved(Request $request): bool
19+
{
20+
$event = $request->server->get('HTTP_X_GITHUB_EVENT');
21+
return $this->eventApproved((string) $event);
22+
}
23+
24+
protected function eventApproved(string $event): bool
1925
{
2026
return str_contains($event, 'project');
2127
}
2228

23-
public function eventRequestApproved(Request $request): bool
29+
protected function isActionPresent(array $payload): bool
2430
{
25-
$event = $request->server->get('HTTP_X_GITHUB_EVENT');
31+
return isset($payload['action']);
32+
}
2633

27-
return $this->eventApproved((string) $event);
34+
/**
35+
* @param array<string, mixed> $payload
36+
*/
37+
protected function hasValidNodeAndFieldData(array $payload): bool
38+
{
39+
return isset($payload['projects_v2_item']['content_node_id'], $payload['changes']['field_value']);
40+
}
41+
42+
/**
43+
* @param array<string, mixed> $payload
44+
*/
45+
protected function hasFieldTemplate(array $payload): bool
46+
{
47+
$fieldType = $payload['changes']['field_value']['field_type'] ?? '';
48+
return view()->exists('github-project::md.fields.'.$fieldType);
49+
}
50+
51+
protected function createErrorResponse(string $message, ?int $statusCode = 400): JsonResponse
52+
{
53+
return response()->json(['message' => __($message)], $statusCode);
2854
}
2955

3056
/**
@@ -34,35 +60,20 @@ public function eventRequestApproved(Request $request): bool
3460
*/
3561
public function validatePayload(array $payload): ?JsonResponse
3662
{
37-
if (!isset($payload['action'])) {
38-
return response()->json(
39-
['message' => __('github-project::github-project.error.event.action_not_found')],
40-
404
41-
);
63+
if (!$this->isActionPresent($payload)) {
64+
return $this->createErrorResponse('github-project::github-project.error.event.action_not_found', 404);
4265
}
4366

44-
if (!$this->isStatusCommentEnabled((string) $payload['changes']['field_value']['field_name'])) {
45-
return response()->json(
46-
['message' => __('github-project::github-project.error.event.status_comment_disabled')],
47-
400
48-
);
67+
if (!$this->isStatusCommentEnabled($payload)) {
68+
return $this->createErrorResponse('github-project::github-project.error.event.status_comment_disabled');
4969
}
5070

51-
$nodeId = $payload['projects_v2_item']['content_node_id'] ?? null;
52-
$fieldData = $payload['changes']['field_value'] ?? null;
53-
54-
if (!$nodeId || !$fieldData) {
55-
return response()->json(
56-
['message' => __('github-project::github-project.error.event.missing_fields')],
57-
400
58-
);
71+
if (!$this->hasValidNodeAndFieldData($payload)) {
72+
return $this->createErrorResponse('github-project::github-project.error.event.missing_fields');
5973
}
6074

61-
if (view()->exists('github-project::md.fields.'.$fieldData['field_type'])) {
62-
return response()->json(
63-
['message' => __('github-project::github-project.error.event.missing_field_template')],
64-
400
65-
);
75+
if (!$this->hasFieldTemplate($payload)) {
76+
return $this->createErrorResponse('github-project::github-project.error.event.missing_field_template');
6677
}
6778

6879
return null;
@@ -71,12 +82,13 @@ public function validatePayload(array $payload): ?JsonResponse
7182
/**
7283
* Check if the field name is "Status" and if status comments are enabled.
7384
*
74-
* @param string $fieldName
85+
* @param array<string, mixed> $payload
7586
*
7687
* @return bool
7788
*/
78-
public function isStatusCommentEnabled(string $fieldName): bool
89+
protected function isStatusCommentEnabled(array $payload): bool
7990
{
91+
$fieldName = (string) $payload['changes']['field_value']['field_name'] ?? '';
8092
if ($fieldName === 'Status' && !config('github-project.enable_status_comment')) {
8193
return false;
8294
}

0 commit comments

Comments
 (0)