22
33namespace CSlant \GitHubProject \Services ;
44
5+ use Github \Client ;
56use Illuminate \Http \JsonResponse ;
67use Symfony \Component \HttpFoundation \Request ;
78
89class WebhookService
910{
10- public function eventApproved (string $ event ): bool
11+ protected Client $ client ;
12+
13+ public function __construct (Client $ client )
1114 {
12- return str_contains ( $ event , ' project ' ) ;
15+ $ this -> client = $ client ;
1316 }
1417
1518 public function eventRequestApproved (Request $ request ): bool
@@ -19,42 +22,63 @@ public function eventRequestApproved(Request $request): bool
1922 return $ this ->eventApproved ((string ) $ event );
2023 }
2124
25+ protected function eventApproved (string $ event ): bool
26+ {
27+ return str_contains ($ event , 'project ' );
28+ }
29+
30+ /**
31+ * @param array<string, mixed> $payload
32+ */
33+ protected function isActionPresent (array $ payload ): bool
34+ {
35+ return isset ($ payload ['action ' ]);
36+ }
37+
38+ /**
39+ * @param array<string, mixed> $payload
40+ */
41+ protected function hasValidNodeAndFieldData (array $ payload ): bool
42+ {
43+ return isset ($ payload ['projects_v2_item ' ]['content_node_id ' ], $ payload ['changes ' ]['field_value ' ]);
44+ }
45+
46+ /**
47+ * @param array<string, mixed> $payload
48+ */
49+ protected function hasFieldTemplate (array $ payload ): bool
50+ {
51+ $ fieldType = $ payload ['changes ' ]['field_value ' ]['field_type ' ] ?? '' ;
52+
53+ return view ()->exists ('github-project::md.fields. ' .$ fieldType );
54+ }
55+
56+ protected function createErrorResponse (string $ message , int $ statusCode = 400 ): JsonResponse
57+ {
58+ return response ()->json (['message ' => __ ($ message )], $ statusCode );
59+ }
60+
2261 /**
2362 * @param array<string, mixed> $payload
2463 *
2564 * @return null|JsonResponse
2665 */
2766 public function validatePayload (array $ payload ): ?JsonResponse
2867 {
29- if (!isset ($ payload ['action ' ])) {
30- return response ()->json (
31- ['message ' => __ ('github-project::github-project.error.event.action_not_found ' )],
32- 404
33- );
68+ if (!$ this ->isActionPresent ($ payload )) {
69+ return $ this ->createErrorResponse ('github-project::github-project.error.event.action_not_found ' , 404 );
3470 }
3571
36- if (!$ this ->isStatusCommentEnabled ((string ) $ payload ['changes ' ]['field_value ' ]['field_name ' ])) {
37- return response ()->json (
38- ['message ' => __ ('github-project::github-project.error.event.status_comment_disabled ' )],
39- 400
40- );
72+ if (!$ this ->isStatusCommentEnabled ($ payload )) {
73+ return $ this ->createErrorResponse ('github-project::github-project.error.event.status_comment_disabled ' );
4174 }
4275
43- $ nodeId = $ payload ['projects_v2_item ' ]['content_node_id ' ] ?? null ;
44- $ fieldData = $ payload ['changes ' ]['field_value ' ] ?? null ;
45-
46- if (!$ nodeId || !$ fieldData ) {
47- return response ()->json (
48- ['message ' => __ ('github-project::github-project.error.event.missing_fields ' )],
49- 400
50- );
76+ if (!$ this ->hasValidNodeAndFieldData ($ payload )) {
77+ return $ this ->createErrorResponse ('github-project::github-project.error.event.missing_fields ' );
5178 }
5279
53- if (view ()->exists ('github-project::md.fields. ' .$ fieldData ['field_type ' ])) {
54- return response ()->json (
55- ['message ' => __ ('github-project::github-project.error.event.missing_field_template ' )],
56- 400
57- );
80+ if (!$ this ->hasFieldTemplate ($ payload )) {
81+ return $ this ->createErrorResponse ('github-project::github-project.error.event.missing_field_template ' );
5882 }
5983
6084 return null ;
@@ -63,13 +87,15 @@ public function validatePayload(array $payload): ?JsonResponse
6387 /**
6488 * Check if the field name is "Status" and if status comments are enabled.
6589 *
66- * @param string $fieldName
90+ * @param array< string, mixed> $payload
6791 *
6892 * @return bool
6993 */
70- public function isStatusCommentEnabled (string $ fieldName ): bool
94+ protected function isStatusCommentEnabled (array $ payload ): bool
7195 {
72- if ($ fieldName === 'Status ' && !config ('github-project.enable_status_comment ' )) {
96+ if ((string ) $ payload ['changes ' ]['field_value ' ]['field_name ' ] === 'Status '
97+ && !config ('github-project.enable_status_comment ' )
98+ ) {
7399 return false ;
74100 }
75101
0 commit comments