Skip to content

Commit

Permalink
add more v2 APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
vantt committed Oct 12, 2015
1 parent 7f65a7c commit 5a7ab6c
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/v1/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Email {

private $email;
protected $email;

/**
* MailHogEmail constructor.
Expand Down
11 changes: 6 additions & 5 deletions src/v1/MailHogAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ public function getAllEmails() {
/**
* Get last email sent
*
* @return FALSE|MailHogEmailData
* @return FALSE|Email
*/
public function getLastEmail() {
return reset($this->getAllEmails());
}


/**
* @param string $id
* @param $id
*
* @return Email|FALSE
* @return Email|null
*/
public function getEmailById($id) {
$email = FALSE;
$email = NULL;
$response = $this->client->get('messages/' . $id);

if (200 == $response->getStatusCode()) {
Expand All @@ -82,7 +83,7 @@ public function deleteEmail($id) {
/**
* Delete all emails
*
* @return mixed
* @return bool
*/
public function deleteAllEmails() {
$response = $this->client->delete('messages');
Expand Down
8 changes: 8 additions & 0 deletions src/v2/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace MailHog\v2;

use MailHog\v1\Email as EmailV1;

class Email extends EmailV1 {

}
202 changes: 202 additions & 0 deletions src/v2/MailHogAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php
namespace MailHog\v2;

use GuzzleHttp\Client;
use stdClass;

class MailHogAPI {

/**
* @var Client
*/
private $client;

/**
* MailHogAPIv2 constructor.
*
* @param string $host
* @param int $port
*
* @internal param Client $client
*/
public function __construct($host = '127.0.0.1', $port = 8025) {
$this->client = new Client(['base_uri' => 'http://' . $host . ':' . $port . '/api/v2/']);
}


/**
* Get all the emails
*
* @param int $start
* @param int $limit
*
* @return array
*/
public function getAllEmails($start = 0, $limit = 0) {
$emails = array();
$query = array();

if ($start) {
$query['start'] = $start;
}

if ($limit) {
$query['limit'] = $limit;
}

$response = $this->client->get('messages', ['query' => $query]);

if (200 == $response->getStatusCode()) {
$result = json_decode($response->getBody());

foreach ($result->items as $item) {
$emails[] = $this->buildEmail($item);
}
}

return $emails;
}

/**
* Get last email sent
*
* @return Email
*/
public function getLastEmail() {
return reset($this->getAllEmails());
}

/**
* @param string $keyword
* @param int $start
* @param int $limit
*
* @return array
*/
public function searchInFrom($keyword, $start = 0, $limit = 0) {
$emails = array();
$query = array('kind' => 'from', 'query' => $keyword);

if ($start) {
$query['start'] = $start;
}

if ($limit) {
$query['limit'] = $limit;
}

$response = $this->client->get('search', ['query' => $query]);

if (200 == $response->getStatusCode()) {
$result = json_decode($response->getBody());

foreach ($result->items as $item) {
$emails[] = $this->buildEmail($item);
}
}

return $emails;
}

/**
* @param string $keyword
* @param int $start
* @param int $limit
*
* @return array
*/
public function searchInTo($keyword, $start = 0, $limit = 0) {
$emails = array();
$query = array('kind' => 'to', 'query' => $keyword);

if ($start) {
$query['start'] = $start;
}

if ($limit) {
$query['limit'] = $limit;
}

$response = $this->client->get('search', ['query' => $query]);

if (200 == $response->getStatusCode()) {
$result = json_decode($response->getBody());

foreach ($result->items as $item) {
$emails[] = $this->buildEmail($item);
}
}

return $emails;
}

/**
* @param string $keyword
* @param int $start
* @param int $limit
*
* @return array
*/
public function searchContaining($keyword, $start = 0, $limit = 0) {
$emails = array();
$query = array('kind' => 'containing', 'query' => $keyword);

if ($start) {
$query['start'] = $start;
}

if ($limit) {
$query['limit'] = $limit;
}

$response = $this->client->get('search', ['query' => $query]);

if (200 == $response->getStatusCode()) {
$result = json_decode($response->getBody());

foreach ($result->items as $item) {
$emails[] = $this->buildEmail($item);
}
}

return $emails;
}

/**
* @param string $id
*
* @return Email|FALSE
*/
public function getEmailById($id) {
throw new \BadFunctionCallException('This service has not be implemented.');
}

/**
* @param string $id
*
* @return bool
*/
public function deleteEmail($id) {
throw new \BadFunctionCallException('This service has not be implemented.');
}


/**
* Delete all emails
*
* @return mixed
*/
public function deleteAllEmails() {
throw new \BadFunctionCallException('This service has not be implemented.');
}


/**
* @param stdClass $email_data
*
* @return Email
*/
protected function buildEmail(stdClass $email_data) {
return new Email($email_data);
}
}

0 comments on commit 5a7ab6c

Please sign in to comment.