From 25c3c247a4939efc0e758195cb83854d7667c1fa Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Tue, 20 Oct 2020 00:41:28 +0300 Subject: [PATCH 1/2] Refactored the way the methods are being called --- src/Http/Controllers/CloudwatchWebhook.php | 54 ++------ src/Http/Controllers/SesWebhook.php | 150 ++------------------- tests/Controllers/CloudwatchController.php | 13 ++ tests/Controllers/SesController.php | 13 ++ tests/SesTest.php | 21 ++- 5 files changed, 57 insertions(+), 194 deletions(-) diff --git a/src/Http/Controllers/CloudwatchWebhook.php b/src/Http/Controllers/CloudwatchWebhook.php index e557c5d..6e962c5 100644 --- a/src/Http/Controllers/CloudwatchWebhook.php +++ b/src/Http/Controllers/CloudwatchWebhook.php @@ -3,6 +3,7 @@ namespace RenokiCo\AwsWebhooks\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Str; use Rennokki\LaravelSnsEvents\Http\Controllers\SnsController; class CloudwatchWebhook extends SnsController @@ -18,52 +19,15 @@ protected function onNotification(array $snsMessage, Request $request): void { $decodedMessage = json_decode($snsMessage['Message'], true); - $state = $decodedMessage['NewStateValue'] ?? null; + $state = Str::studly($decodedMessage['NewStateValue'] ?? null); - switch ($state) { - case 'OK': $this->onOkState($decodedMessage, $snsMessage, $request); break; - case 'ALARM': $this->onAlarmState($decodedMessage, $snsMessage, $request); break; - case 'INSUFFICIENT_DATA': $this->onInsufficientData($decodedMessage, $snsMessage, $request); break; - default: break; - } - } + $methodToCall = "on{$state}State"; - /** - * Handle the event when an alarm transitioned to OK. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onOkState(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the event when an alarm transitioned to ALARM. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onAlarmState(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the event when an alarm transitioned to INSUFFICIENT_DATA. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onInsufficientData(array $message, array $originalMessage, Request $request) - { - // + if (method_exists($this, $methodToCall)) { + call_user_func( + [$this, $methodToCall], + $decodedMessage, $snsMessage, $request + ); + } } } diff --git a/src/Http/Controllers/SesWebhook.php b/src/Http/Controllers/SesWebhook.php index 70fc780..72d3987 100644 --- a/src/Http/Controllers/SesWebhook.php +++ b/src/Http/Controllers/SesWebhook.php @@ -3,28 +3,11 @@ namespace RenokiCo\AwsWebhooks\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Str; use Rennokki\LaravelSnsEvents\Http\Controllers\SnsController; class SesWebhook extends SnsController { - /** - * Associate each SNS `eventType` value - * with a callable method from this class. - * - * @var array - */ - public static $eventTypesWithCalledMethod = [ - 'Bounce' => 'onBounce', - 'Complaint' => 'onComplaint', - 'Delivery' => 'onDelivery', - 'Send' => 'onSend', - 'Reject' => 'onReject', - 'Open' => 'onOpen', - 'Click' => 'onClick', - 'Rendering Failure' => 'onRenderingFailure', - 'DeliveryDelay' => 'onDeliveryDelay', - ]; - /** * Handle logic at the controller level on notification. * @@ -38,130 +21,13 @@ protected function onNotification(array $snsMessage, Request $request): void $eventType = $decodedMessage['eventType'] ?? null; - foreach (static::$eventTypesWithCalledMethod as $callableEventType => $methodToCall) { - if ($eventType === $callableEventType) { - call_user_func( - [$this, $methodToCall], - $decodedMessage, $snsMessage, $request - ); - } - } - } - - /** - * Handle the Bounce event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onBounce(array $message, array $originalMessage, Request $request) - { - // - } + $methodToCall = 'on'.Str::studly($eventType); - /** - * Handle the Complaint event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onComplaint(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the Delivery event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onDelivery(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the Send event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onSend(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the Reject event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onReject(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the Open event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onOpen(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the Click event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onClick(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the Rendering Failure event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onRenderingFailure(array $message, array $originalMessage, Request $request) - { - // - } - - /** - * Handle the DeliveryDelay event. - * - * @param array $message - * @param array $originalMessage - * @param \Illuminate\Http\Request $request - * @return void - */ - protected function onDeliveryDelay(array $message, array $originalMessage, Request $request) - { - // + if (method_exists($this, $methodToCall)) { + call_user_func( + [$this, $methodToCall], + $decodedMessage, $snsMessage, $request + ); + } } } diff --git a/tests/Controllers/CloudwatchController.php b/tests/Controllers/CloudwatchController.php index 99217d0..ed2fab3 100644 --- a/tests/Controllers/CloudwatchController.php +++ b/tests/Controllers/CloudwatchController.php @@ -20,4 +20,17 @@ protected function getMessageValidator(Request $request) return $request->certificate ?: $url; }); } + + /** + * Handle the event when an alarm transitioned to OK. + * + * @param array $message + * @param array $originalMessage + * @param \Illuminate\Http\Request $request + * @return void + */ + protected function onOkState(array $message, array $originalMessage, Request $request) + { + // + } } diff --git a/tests/Controllers/SesController.php b/tests/Controllers/SesController.php index fec1aaf..3297547 100644 --- a/tests/Controllers/SesController.php +++ b/tests/Controllers/SesController.php @@ -20,4 +20,17 @@ protected function getMessageValidator(Request $request) return $request->certificate ?: $url; }); } + + /** + * Handle the Rendering Failure event. + * + * @param array $message + * @param array $originalMessage + * @param \Illuminate\Http\Request $request + * @return void + */ + protected function onRenderingFailure(array $message, array $originalMessage, Request $request) + { + // + } } diff --git a/tests/SesTest.php b/tests/SesTest.php index 8cdf247..7b11a35 100644 --- a/tests/SesTest.php +++ b/tests/SesTest.php @@ -6,14 +6,21 @@ class SesTest extends TestCase { - public function test_callable_methods() + public function test_calling_existing_method() { - foreach (SesWebhook::$eventTypesWithCalledMethod as $callableEventType => $methodToCall) { - $payload = $this->getSesMessage($callableEventType); + $payload = $this->getSesMessage('Rendering Failure'); - $this->withHeaders($this->getHeadersForMessage($payload)) - ->json('GET', route('ses', ['certificate' => static::$certificate]), $payload) - ->assertSee('OK'); - } + $this->withHeaders($this->getHeadersForMessage($payload)) + ->json('GET', route('ses', ['certificate' => static::$certificate]), $payload) + ->assertSee('OK'); + } + + public function test_calling_inexisting_method() + { + $payload = $this->getSesMessage('Bounce'); + + $this->withHeaders($this->getHeadersForMessage($payload)) + ->json('GET', route('ses', ['certificate' => static::$certificate]), $payload) + ->assertSee('OK'); } } From 84e6020b7daa5d14be8a5524b740390488b96c15 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Tue, 20 Oct 2020 00:42:44 +0300 Subject: [PATCH 2/2] csfixing --- tests/SesTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/SesTest.php b/tests/SesTest.php index 7b11a35..0e263bd 100644 --- a/tests/SesTest.php +++ b/tests/SesTest.php @@ -2,8 +2,6 @@ namespace RenokiCo\AwsWebhooks\Test; -use RenokiCo\AwsWebhooks\Http\Controllers\SesWebhook; - class SesTest extends TestCase { public function test_calling_existing_method()