mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2025-02-06 08:19:25 +03:00
Addition of campaigns, logs, lists, and routes
This commit is contained in:
parent
90f808df81
commit
0bd40490dd
93
src/Mailgun/Campaigns/Campaigns.php
Normal file
93
src/Mailgun/Campaigns/Campaigns.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?PHP
|
||||
|
||||
/*
|
||||
* Campaigns.php - Processing Campaigns.
|
||||
*/
|
||||
namespace Mailgun\Campaigns;
|
||||
|
||||
class Campaigns{
|
||||
|
||||
private $httpBroker;
|
||||
private $workingDomain;
|
||||
private $endpointUrl;
|
||||
|
||||
|
||||
public function __construct($httpBroker){
|
||||
$this->httpBroker = $httpBroker;
|
||||
$this->endpointUrl = $this->httpBroker->returnWorkingDomain() . "/campaigns";
|
||||
}
|
||||
|
||||
public function getCampaigns($limit, $skip){
|
||||
$response = $this->httpBroker->getRequest($this->endpointUrl, array($limit, $skip));
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getCampaign($campaignId){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId;
|
||||
$response = $this->httpBroker->getRequest($updatedUrl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function addCampaign($name, $id){
|
||||
if(isset($id) && strlen($id) > 64){
|
||||
throw new InvalidParameter("The message ID is too long. Limit is 64 characters.");
|
||||
}
|
||||
$postData = array('name' => $name, 'id' => $id);
|
||||
$response = $this->httpBroker->postRequest($this->endpointUrl, $postData);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function updateCampaign($campaignId, $name, $id){
|
||||
if(isset($id) && strlen($id) > 64){
|
||||
throw new InvalidParameter("The message ID is too long. Limit is 64 characters.");
|
||||
}
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId;
|
||||
$postData = array('name' => $name, 'id' => $id);
|
||||
$response = $this->httpBroker->putRequest($updatedUrl, $postData);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function deleteCampaign($campaignId){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId;
|
||||
$response = $this->httpBroker->deleteCampaign($updatedUrl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getCampaignEvents($campaignId, $filterParams = array()){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId . "/events";
|
||||
$response = $this->httpBroker->getRequest($updatedUrl, $filterParams);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getCampaignStats($campaignId, $filterParams = array()){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId . "/stats";
|
||||
$response = $this->httpBroker->getRequest($updatedUrl, $filterParams);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getCampaignClicks($campaignId, $filterParams = array()){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId . "/clicks";
|
||||
$response = $this->httpBroker->getRequest($updatedUrl, $filterParams);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getCampaignOpens($campaignId, $filterParams = array()){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId . "/opens";
|
||||
$response = $this->httpBroker->getRequest($updatedUrl, $filterParams);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getCampaignUnsubscribes($campaignId, $filterParams = array()){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId . "/unsubscribes";
|
||||
$response = $this->httpBroker->getRequest($updatedUrl, $filterParams);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getCampaignComplaints($campaignId, $filterParams = array()){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $campaignId . "/clicks";
|
||||
$response = $this->httpBroker->getRequest($updatedUrl, $filterParams);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
6
src/Mailgun/Campaigns/Exceptions/InvalidParameter.php
Normal file
6
src/Mailgun/Campaigns/Exceptions/InvalidParameter.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace Mailgun\Campaigns\Exceptions;
|
||||
|
||||
class InvalidParameter extends \Exception{}
|
||||
|
||||
?>
|
26
src/Mailgun/Lists/Lists.php
Normal file
26
src/Mailgun/Lists/Lists.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?PHP
|
||||
|
||||
/*
|
||||
* Lists.php - Processing LIsts.
|
||||
*/
|
||||
namespace Mailgun\Lists;
|
||||
|
||||
class Lists{
|
||||
|
||||
private $httpBroker;
|
||||
private $workingDomain;
|
||||
private $endpointUrl;
|
||||
|
||||
|
||||
public function __construct($httpBroker){
|
||||
$this->httpBroker = $httpBroker;
|
||||
$this->endpointUrl = $this->httpBroker->returnWorkingDomain() . "/lists";
|
||||
}
|
||||
|
||||
public function getLists($limit, $skip){
|
||||
$response = $this->httpBroker->getRequest($this->endpointUrl, array($limit, $skip));
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -21,4 +21,6 @@ class Logs{
|
||||
$response = $this->httpBroker->getRequest($this->endpointUrl, array($limit, $skip));
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -4,8 +4,11 @@ namespace Mailgun;
|
||||
|
||||
use Mailgun\Logs\Logs;
|
||||
use Mailgun\Stats\Stats;
|
||||
use Mailgun\Lists\Lists;
|
||||
use Mailgun\Routes\Routes;
|
||||
use Mailgun\Bounces\Bounces;
|
||||
use Mailgun\Messages\Messages;
|
||||
use Mailgun\Campaigns\Campaigns;
|
||||
use Mailgun\Complaints\Complaints;
|
||||
use Mailgun\Connection\HttpBroker;
|
||||
use Mailgun\Unsubscribes\Unsubscribes;
|
||||
@ -48,7 +51,19 @@ class MailgunClient{
|
||||
|
||||
public function Logs(){
|
||||
return new Logs($this->httpBroker);
|
||||
}
|
||||
}
|
||||
|
||||
public function Routes(){
|
||||
return new Routes($this->httpBroker);
|
||||
}
|
||||
|
||||
public function Campaigns(){
|
||||
return new Campaigns($this->httpBroker);
|
||||
}
|
||||
|
||||
public function Lists(){
|
||||
return new Lists($this->httpBroker);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
6
src/Mailgun/Routes/Exceptions/InvalidParameter.php
Normal file
6
src/Mailgun/Routes/Exceptions/InvalidParameter.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace Mailgun\Routes\Exceptions;
|
||||
|
||||
class InvalidParameter extends \Exception{}
|
||||
|
||||
?>
|
86
src/Mailgun/Routes/Routes.php
Normal file
86
src/Mailgun/Routes/Routes.php
Normal file
@ -0,0 +1,86 @@
|
||||
<?PHP
|
||||
|
||||
/*
|
||||
* Routes.php - Processing Routes.
|
||||
*/
|
||||
namespace Mailgun\Routes;
|
||||
|
||||
use Mailgun\Routes\Exceptions\InvalidParameter;
|
||||
|
||||
class Routes{
|
||||
|
||||
private $httpBroker;
|
||||
private $workingDomain;
|
||||
private $endpointUrl;
|
||||
|
||||
public function __construct($httpBroker){
|
||||
$this->httpBroker = $httpBroker;
|
||||
$this->endpointUrl = $this->httpBroker->returnWorkingDomain() . "/routes";
|
||||
}
|
||||
|
||||
public function getRoutes($limit, $skip){
|
||||
$response = $this->httpBroker->getRequest($this->endpointUrl, array($limit, $skip));
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getRoute($routeId){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $routeId;
|
||||
$response = $this->httpBroker->getRequest($updatedUrl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function addRoute($priority, $description, $expression, $action){
|
||||
|
||||
if(!is_int($priority) || $priority < 0){
|
||||
throw new InvalidParameter("The priority is not a positive integer.");
|
||||
}
|
||||
|
||||
if(!isset($description)){
|
||||
throw new InvalidParameter("The description seems to be missing.");
|
||||
}
|
||||
|
||||
if(!isset($expression)){
|
||||
throw new InvalidParameter("The expression seems to be missing.");
|
||||
}
|
||||
if(!isset($action)){
|
||||
throw new InvalidParameter("The action seems to be missing.");
|
||||
}
|
||||
|
||||
$postData = array('priority' => $priority, 'description' => $description, 'expression' => $expression, 'action' => $action);
|
||||
|
||||
$response = $this->httpBroker->postRequest($this->endpointUrl, $postData);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function updateRoute($routeId, $priority, $description, $expression, $action){
|
||||
if(!is_int($priority) || $priority < 0){
|
||||
throw new InvalidParameter("The priority is not a positive integer.");
|
||||
}
|
||||
|
||||
if(!isset($description)){
|
||||
throw new InvalidParameter("The description seems to be missing.");
|
||||
}
|
||||
|
||||
if(!isset($expression)){
|
||||
throw new InvalidParameter("The expression seems to be missing.");
|
||||
}
|
||||
if(!isset($action)){
|
||||
throw new InvalidParameter("The action seems to be missing.");
|
||||
}
|
||||
|
||||
$postData = array('priority' => $priority, 'description' => $description, 'expression' => $expression, 'action' => $action);
|
||||
|
||||
$updatedUrl = $this->endpointUrl . "/" . $routeId;
|
||||
|
||||
$response = $this->httpBroker->putRequest($updatedUrl, $postData);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function deleteRoute($routeId){
|
||||
$updatedUrl = $this->endpointUrl . "/" . $routeId;
|
||||
$response = $this->httpBroker->deleteRequest($updatedUrl);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
78
tests/Mailgun/Tests/Campaigns/CampaignsTest.php
Normal file
78
tests/Mailgun/Tests/Campaigns/CampaignsTest.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?PHP
|
||||
|
||||
namespace Mailgun\Tests\Campaigns;
|
||||
|
||||
use Mailgun\Tests\MailgunClientTest;
|
||||
|
||||
class CampaignsTest extends \Mailgun\Tests\MailgunTestCase{
|
||||
|
||||
private $client;
|
||||
|
||||
public function setUp(){
|
||||
$this->client = new MailgunClientTest("My-Super-Awesome-API-Key", "samples.mailgun.org", false);
|
||||
|
||||
}
|
||||
|
||||
public function testGetCampaigns(){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->getCampaigns("1", "30");
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
public function testGetCampaign(){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->getCampaign(12345);
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
public function testAddCampaign(){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->addCampaign("MyCampaign", "TheID");
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
public function testUpdateCampaign(){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->updateCampaign(12345, "MyCampaign", "TheID");
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
public function testDeleteCampaign(){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->deleteCampaign(12345);
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
public function testGetCampaignEvents(12345, array()){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->getCampaignEvents(12345);
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
public function testGetCampaignStats(12345, array()){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->getCampaignStats(12345);
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
public function testGetCampaignStats(12345, array()){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->getCampaignClicks(12345);
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
public function testGetCampaignStats(12345, array()){
|
||||
$client = $this->client->Campaigns();
|
||||
$response = $client->getCampaignOpens(12345);
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
}
|
46
tests/Mailgun/Tests/Routes/RoutesTest.php
Normal file
46
tests/Mailgun/Tests/Routes/RoutesTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?PHP
|
||||
|
||||
namespace Mailgun\Tests\Routes;
|
||||
|
||||
use Mailgun\Tests\MailgunClientTest;
|
||||
|
||||
class RoutesTest extends \Mailgun\Tests\MailgunTestCase{
|
||||
|
||||
private $client;
|
||||
|
||||
public function setUp(){
|
||||
$this->client = new MailgunClientTest("My-Super-Awesome-API-Key", "samples.mailgun.org", false);
|
||||
|
||||
}
|
||||
public function testAddRoute(){
|
||||
$client = $this->client->Routes();
|
||||
$response = $client->addRoute(10, "This is the description", "match_recipient('.*@gmail.com')", "forward('alex@mailgun.net')");
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
public function testDeleteRoute(){
|
||||
$client = $this->client->Routes();
|
||||
$response = $client->deleteRoute(12345);
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
public function testGetRoute(){
|
||||
$client = $this->client->Routes();
|
||||
$response = $client->getRoute(12345);
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
public function testGetRoutes(){
|
||||
$client = $this->client->Routes();
|
||||
$response = $client->getRoutes("5", "10");
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
public function testUpdateRoute(){
|
||||
$client = $this->client->Routes();
|
||||
$response = $client->updateRoute(12345, 10, "This is the description", "match_recipient('.*@gmail.com')", "forward('alex@mailgun.net')");
|
||||
$httpCode = $response->http_response_code;
|
||||
$this->assertEquals(200, $httpCode);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user