Skip to content

Commit

Permalink
refactor methods and construction
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewaite committed May 12, 2017
1 parent 1baee3c commit d07a0ef
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -53,7 +53,7 @@ class SiteProblem extends Notification
public function toPagerDuty($notifiable)
{
return PagerDutyMessage::create()
->summary('There was an error with your site in the {$notifiable->service} component.');
->setSummary('There was an error with your site in the {$notifiable->service} component.');
}
}
```
Expand Down Expand Up @@ -81,15 +81,15 @@ The `Integration Key` listed for your new integration is what you need to set in
### Available Message methods

- `resolve()`: Sets the event type to `resolve` to resolve issues.
- `dedupKey('')`: Sets the `dedup_key` (required when resolving).
- `summary('')`: Sets a summary message on the event.
- `source('')`: Sets the event source; defaults to the `hostname`.
- `severity('')`: Sets the event severity; defaults to `critical`.
- `timestamp('')`: Sets the `timestamp` of the event.
- `component('')`: Sets the `component` of the event.
- `group('')`: Sets the `group` of the event.
- `setDedupKey('')`: Sets the `dedup_key` (required when resolving).
- `setSummary('')`: Sets a summary message on the event.
- `setSource('')`: Sets the event source; defaults to the `hostname`.
- `setSeverity('')`: Sets the event severity; defaults to `critical`.
- `setTimestamp('')`: Sets the `timestamp` of the event.
- `setComponent('')`: Sets the `component` of the event.
- `setGroup('')`: Sets the `group` of the event.
- `setClass('')`: Sets the `class`.
- `addCustomDetail('', '')`: Adds a key/value paid to the `custom_detail` of the event.
- `addCustomDetail('', '')`: Adds a key/value pair to the `custom_detail` of the event.

See the [PagerDuty v2 Events API documentation](https://v2.developer.pagerduty.com/docs/send-an-event-events-api-v2)
for more information about what these options will do.
Expand Down
2 changes: 1 addition & 1 deletion src/PagerDutyChannel.php
Expand Up @@ -35,7 +35,7 @@ public function send($notifiable, Notification $notification)

/** @var PagerDutyMessage $data */
$data = $notification->toPagerDuty($notifiable);
$data->routingKey($routing_key);
$data->setRoutingKey($routing_key);

try {
$response = $this->client->post('https://events.pagerduty.com/v2/enqueue', [
Expand Down
21 changes: 13 additions & 8 deletions src/PagerDutyMessage.php
Expand Up @@ -12,6 +12,11 @@ class PagerDutyMessage
protected $payload = [];
protected $meta = [];

public static function create()
{
return new static();
}

public function __construct()
{
Arr::set($this->meta, 'event_action', self::EVENT_TRIGGER);
Expand All @@ -20,7 +25,7 @@ public function __construct()
Arr::set($this->payload, 'severity', 'critical');
}

public function routingKey($value)
public function setRoutingKey($value)
{
return $this->setMeta('routing_key', $value);
}
Expand All @@ -30,37 +35,37 @@ public function resolve()
return $this->setMeta('event_action', self::EVENT_RESOLVE);
}

public function dedupKey($key)
public function setDedupKey($key)
{
return $this->setMeta('dedup_key', $key);
}

public function summary($value)
public function setSummary($value)
{
return $this->setPayload('summary', $value);
}

public function source($value)
public function setSource($value)
{
return $this->setPayload('source', $value);
}

public function severity($value)
public function setSeverity($value)
{
return $this->setPayload('severity', $value);
}

public function timestamp($value)
public function setTimestamp($value)
{
return $this->setPayload('timestamp', $value);
}

public function component($value)
public function setComponent($value)
{
return $this->setPayload('component', $value);
}

public function group($value)
public function setGroup($value)
{
return $this->setPayload('group', $value);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PagerDutyChannelTest.php
Expand Up @@ -160,7 +160,7 @@ class TestNotification extends Notification
{
public function toPagerDuty($notifiable)
{
return
(new PagerDutyMessage())->source('testSource');
return PagerDutyMessage::create()
->setSource('testSource');
}
}
38 changes: 19 additions & 19 deletions tests/PagerDutyMessageTest.php
Expand Up @@ -10,8 +10,8 @@ class PagerDutyMessageTest extends \PHPUnit_Framework_TestCase
public function basic_message_has_all_values()
{
$message = (new PagerDutyMessage())
->routingKey('testIntegration01')
->summary('This is a test message');
->setRoutingKey('testIntegration01')
->setSummary('This is a test message');

$body = $message->toArray();

Expand All @@ -29,9 +29,9 @@ public function basic_message_has_all_values()
public function test_message_renders()
{
$message = (new PagerDutyMessage())
->routingKey('testIntegration01')
->summary('This is a test message')
->source('testSource');
->setRoutingKey('testIntegration01')
->setSummary('This is a test message')
->setSource('testSource');

$this->assertEquals(
[
Expand All @@ -50,15 +50,15 @@ public function test_message_renders()
public function test_message_renders_optional_params()
{
$message = (new PagerDutyMessage())
->routingKey('testIntegration01')
->dedupKey('testMessage01')
->severity('error')
->timestamp('timestamp')
->component('nginx')
->group('app servers')
->setRoutingKey('testIntegration01')
->setDedupKey('testMessage01')
->setSeverity('error')
->setTimestamp('timestamp')
->setComponent('nginx')
->setGroup('app servers')
->setClass('ping failure')
->summary('This is a test message')
->source('testSource');
->setSummary('This is a test message')
->setSource('testSource');

$this->assertEquals(
[
Expand All @@ -82,9 +82,9 @@ public function test_message_renders_optional_params()
public function test_message_renders_custom_details()
{
$message = (new PagerDutyMessage())
->routingKey('testIntegration01')
->summary('This is a test message')
->source('testSource')
->setRoutingKey('testIntegration01')
->setSummary('This is a test message')
->setSource('testSource')
->addCustomDetail('ping time', '1500ms')
->addCustomDetail('load avg', '0.75');

Expand All @@ -109,9 +109,9 @@ public function test_message_renders_custom_details()
public function test_message_renders_resolve()
{
$message = (new PagerDutyMessage())
->routingKey('testIntegration01')
->source('testSource')
->dedupKey('testMessage01')
->setRoutingKey('testIntegration01')
->setSource('testSource')
->setDedupKey('testMessage01')
->resolve();

$this->assertEquals(
Expand Down

0 comments on commit d07a0ef

Please sign in to comment.