Skip to content

Commit

Permalink
Merge pull request #4 from renoki-co/refactor/methods
Browse files Browse the repository at this point in the history
[refactor] Refactored the way the methods are being called
  • Loading branch information
rennokki authored Oct 19, 2020
2 parents a323424 + 84e6020 commit 021ab4e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 196 deletions.
54 changes: 9 additions & 45 deletions src/Http/Controllers/CloudwatchWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
);
}
}
}
150 changes: 8 additions & 142 deletions src/Http/Controllers/SesWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
);
}
}
}
13 changes: 13 additions & 0 deletions tests/Controllers/CloudwatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
//
}
}
13 changes: 13 additions & 0 deletions tests/Controllers/SesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
//
}
}
23 changes: 14 additions & 9 deletions tests/SesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

namespace RenokiCo\AwsWebhooks\Test;

use RenokiCo\AwsWebhooks\Http\Controllers\SesWebhook;

class SesTest extends TestCase
{
public function test_callable_methods()
public function test_calling_existing_method()
{
$payload = $this->getSesMessage('Rendering Failure');

$this->withHeaders($this->getHeadersForMessage($payload))
->json('GET', route('ses', ['certificate' => static::$certificate]), $payload)
->assertSee('OK');
}

public function test_calling_inexisting_method()
{
foreach (SesWebhook::$eventTypesWithCalledMethod as $callableEventType => $methodToCall) {
$payload = $this->getSesMessage($callableEventType);
$payload = $this->getSesMessage('Bounce');

$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');
}
}

0 comments on commit 021ab4e

Please sign in to comment.