Changeset View
Changeset View
Standalone View
Standalone View
src/MailgunMessage.php
- This file was added.
<?php | |||||
namespace Keruald\Mailgun; | |||||
use GuzzleHttp\ClientInterface; | |||||
use InvalidArgumentException; | |||||
class MailgunMessage { | |||||
dereckson: Test me! | |||||
/// | |||||
/// Private properties | |||||
/// | |||||
/** | |||||
* @var stdClass | |||||
*/ | |||||
private $message = null; | |||||
/** | |||||
* @var \GuzzleHttp\ClientInterface | |||||
*/ | |||||
private $client; | |||||
/** | |||||
* @var string | |||||
*/ | |||||
private $url; | |||||
/** | |||||
* @var string | |||||
*/ | |||||
private $key; | |||||
/// | |||||
/// Constructor | |||||
/// | |||||
/** | |||||
* Initializes a new instance of the MailgunMessage object. | |||||
* | |||||
* @param \GuzzleHttp\Client $client HTTP client | |||||
* @param string $url The URL of the message to fetch. | |||||
* @param string $key The API key to use to fetch the message. | |||||
*/ | |||||
public function __construct (ClientInterface $client, $url, $key) { | |||||
$this->client = $client; | |||||
$this->url = $url; | |||||
$this->key = $key; | |||||
} | |||||
/** | |||||
* Initializes a new instance of the MailgunMessage object from a payload. | |||||
* | |||||
* @param \GuzzleHttp\Client $client HTTP client | |||||
* @param object $payload The payload fired by MailGun routing API | |||||
* @param string $key The API key to use to fetch the message. | |||||
*/ | |||||
public static function loadFromEventPayload (ClientInterface $client, $payload, $key) { | |||||
$url = self::extractUrlFromEventPayload($payload); | |||||
return new self($client, $url, $key); | |||||
} | |||||
/// | |||||
/// Public API methods | |||||
/// | |||||
/** | |||||
* Gets a JSON representation of a mail through Mailgun API. | |||||
* | |||||
* @return stdClass | |||||
*/ | |||||
public function get () { | |||||
if ($this->message === null) { | |||||
$this->fetch(); | |||||
} | |||||
return $this->message; | |||||
} | |||||
/// | |||||
/// Helper methods to fetch a message from Mailgun. | |||||
/// | |||||
/** | |||||
* Fetches the message through Mailgun API. | |||||
* | |||||
* If successful, fills the message property. | |||||
* | |||||
* @throws \RuntimeException when HTTP status code isn't 200 | |||||
*/ | |||||
private function fetch () { | |||||
$response = $this->client->request( | |||||
'GET', | |||||
$this->url, | |||||
$this->getHttpOptions() | |||||
); | |||||
$result = $response->getBody(); | |||||
$this->message = json_decode($result); | |||||
} | |||||
Not Done Inline ActionsNot needed, as Client::request will alreadt throw this exception. dereckson: Not needed, as Client::request will alreadt throw this exception. | |||||
/** | |||||
* @return array | |||||
*/ | |||||
private function getHttpOptions () { | |||||
return [ | |||||
'auth' => [ | |||||
'api', | |||||
$this->key | |||||
] | |||||
]; | |||||
} | |||||
/// | |||||
/// Helper methods to process payload | |||||
/// | |||||
/** | |||||
* Extracts the MailGun URL to retrieve a stored message. | |||||
* | |||||
* @return string | |||||
* @throw \InvalidArgumentException if payload doesn't contain URL where expected. | |||||
*/ | |||||
private static function extractUrlFromEventPayload ($payload) { | |||||
if (!isset($payload->storage->url)) { | |||||
throw new InvalidArgumentException("The payload should be an object with a storage.url property."); | |||||
} | |||||
return $payload->storage->url; | |||||
} | |||||
} |
Nasqueron DevCentral · If it had been much bigger the moon would have had a core of ice. · Powered by Phabricator
Test me!