domain tracking implementation

This commit is contained in:
Artem Bondarenko 2020-10-10 00:43:27 +03:00 committed by David Garcia
parent 27d579fdf1
commit 2d8283ea29
16 changed files with 814 additions and 0 deletions

View File

@ -20,8 +20,12 @@ use Mailgun\Model\Domain\DeleteCredentialResponse;
use Mailgun\Model\Domain\DeleteResponse;
use Mailgun\Model\Domain\IndexResponse;
use Mailgun\Model\Domain\ShowResponse;
use Mailgun\Model\Domain\TrackingResponse;
use Mailgun\Model\Domain\UpdateClickTrackingResponse;
use Mailgun\Model\Domain\UpdateConnectionResponse;
use Mailgun\Model\Domain\UpdateCredentialResponse;
use Mailgun\Model\Domain\UpdateOpenTrackingResponse;
use Mailgun\Model\Domain\UpdateUnsubscribeTrackingResponse;
use Mailgun\Model\Domain\VerifyResponse;
use Psr\Http\Message\ResponseInterface;
@ -299,4 +303,80 @@ class Domain extends HttpApi
return $this->hydrateResponse($response, VerifyResponse::class);
}
/**
* Returns a domain tracking settings.
*
* @param string $domain name of the domain
*
* @return TrackingResponse|array|ResponseInterface
*/
public function tracking(string $domain)
{
Assert::stringNotEmpty($domain);
$response = $this->httpGet(sprintf('/v3/domains/%s/tracking', $domain));
return $this->hydrateResponse($response, TrackingResponse::class);
}
/**
* Updates a domain click tracking settings.
*
* @param string $domain name of the domain
*
* @param bool $active
* @return UpdateClickTrackingResponse|array|ResponseInterface
*/
public function updateClickTracking(string $domain, bool $active)
{
$params = [
'active' => $active ? 'true' : 'false',
];
$response = $this->httpPut(sprintf('/v3/domains/%s/tracking/click', $domain), $params);
return $this->hydrateResponse($response, UpdateClickTrackingResponse::class);
}
/**
* Updates a domain open tracking settings.
*
* @param string $domain name of the domain
* @param bool $active
*
* @return UpdateOpenTrackingResponse|array|ResponseInterface
*/
public function updateOpenTracking(string $domain, bool $active)
{
$params = [
'active' => $active ? 'true' : 'false',
];
$response = $this->httpPut(sprintf('/v3/domains/%s/tracking/open', $domain), $params);
return $this->hydrateResponse($response, UpdateOpenTrackingResponse::class);
}
/**
* Updates a domain unsubscribe tracking settings.
*
* @param string $domain name of the domain
* @param bool $active
* @param string $htmlFooter
* @param string $textFooter
* @return UpdateUnsubscribeTrackingResponse|array|ResponseInterface
*/
public function updateUnsubscribeTracking(string $domain, bool $active, string $htmlFooter, string $textFooter)
{
$params = [
'active' => $active ? 'true' : 'false',
'html_footer' => $htmlFooter,
'text_footer' => $textFooter,
];
$response = $this->httpPut(sprintf('/v3/domains/%s/tracking/unsubscribe', $domain), $params);
return $this->hydrateResponse($response, UpdateUnsubscribeTrackingResponse::class);
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\Domain;
/**
* Represents a single Click Tracking setting for a domain tracking.
*
* @author Artem Bondarenko <artem@uartema.com>
*/
final class ClickTracking
{
private $active;
public static function create(array $data): self
{
$model = new self();
$model->active = !!($data['active'] ?? null);
return $model;
}
private function __construct()
{
}
/**
* @return bool
*/
public function isActive(): bool
{
return $this->active;
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\Domain;
/**
* Represents a single Open Tracking setting for a domain tracking.
*
* @author Artem Bondarenko <artem@uartema.com>
*/
final class OpenTracking
{
private $active;
public static function create(array $data): self
{
$model = new self();
$model->active = !!($data['active'] ?? null);
return $model;
}
private function __construct()
{
}
/**
* @return bool
*/
public function isActive(): bool
{
return $this->active;
}
}

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\Domain;
use Mailgun\Model\ApiResponse;
/**
* @author Artem Bondarenko <artem@uartema.com>
*/
final class TrackingResponse implements ApiResponse
{
private $click;
private $open;
private $unsubscribe;
public static function create(array $data): ?self
{
if (!isset($data['tracking'])) {
return null;
}
$trackingSettings = $data['tracking'];
$model = new self();
$model->click = ClickTracking::create($trackingSettings['click'] ?? []);
$model->open = OpenTracking::create($trackingSettings['click'] ?? []);
$model->unsubscribe = UnsubscribeTracking::create($trackingSettings['unsubscribe'] ?? []);
return $model;
}
private function __construct()
{
}
/**
* @return ClickTracking
*/
public function getClick(): ClickTracking
{
return $this->click;
}
/**
* @return OpenTracking
*/
public function getOpen(): OpenTracking
{
return $this->open;
}
/**
* @return UnsubscribeTracking
*/
public function getUnsubscribe(): UnsubscribeTracking
{
return $this->unsubscribe;
}
}

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\Domain;
/**
* Represents a single Unsubscribe Tracking setting for a domain tracking.
*
* @author Artem Bondarenko <artem@uartema.com>
*/
final class UnsubscribeTracking
{
private $active;
private $htmlFooter;
private $textFooter;
public static function create(array $data): self
{
$model = new self();
$model->active = !!($data['active'] ?? null);
$model->htmlFooter = $data['html_footer'] ?? '';
$model->textFooter = $data['text_footer'] ?? '';
return $model;
}
private function __construct()
{
}
/**
* @return bool
*/
public function isActive(): bool
{
return $this->active;
}
/**
* @return string
*/
public function getHtmlFooter(): string
{
return $this->htmlFooter;
}
/**
* @return string
*/
public function getTextFooter(): string
{
return $this->textFooter;
}
}

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\Domain;
use Mailgun\Model\ApiResponse;
/**
* @author Artem Bondarenko <artem@uartema.com>
*/
final class UpdateClickTrackingResponse implements ApiResponse
{
private $message;
private $click;
public static function create(array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
$model->click = ClickTracking::create($data['click'] ?? []);
return $model;
}
private function __construct()
{
}
/**
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}
/**
* @return ClickTracking
*/
public function getClick(): ClickTracking
{
return $this->click;
}
}

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\Domain;
use Mailgun\Model\ApiResponse;
/**
* @author Artem Bondarenko <artem@uartema.com>
*/
final class UpdateOpenTrackingResponse implements ApiResponse
{
private $message;
private $open;
public static function create(array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
$model->open = OpenTracking::create($data['open'] ?? []);
return $model;
}
private function __construct()
{
}
/**
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}
/**
* @return OpenTracking
*/
public function getOpen(): OpenTracking
{
return $this->open;
}
}

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Model\Domain;
use Mailgun\Model\ApiResponse;
/**
* @author Artem Bondarenko <artem@uartema.com>
*/
final class UpdateUnsubscribeTrackingResponse implements ApiResponse
{
private $message;
private $unsubscribe;
public static function create(array $data): self
{
$model = new self();
$model->message = $data['message'] ?? null;
$model->unsubscribe = UnsubscribeTracking::create($data['unsubscribe'] ?? []);
return $model;
}
private function __construct()
{
}
/**
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}
/**
* @return UnsubscribeTracking
*/
public function getUnsubscribe(): UnsubscribeTracking
{
return $this->unsubscribe;
}
}

View File

@ -20,8 +20,12 @@ use Mailgun\Model\Domain\DeleteCredentialResponse;
use Mailgun\Model\Domain\DeleteResponse;
use Mailgun\Model\Domain\IndexResponse;
use Mailgun\Model\Domain\ShowResponse;
use Mailgun\Model\Domain\TrackingResponse;
use Mailgun\Model\Domain\UpdateClickTrackingResponse;
use Mailgun\Model\Domain\UpdateConnectionResponse;
use Mailgun\Model\Domain\UpdateCredentialResponse;
use Mailgun\Model\Domain\UpdateOpenTrackingResponse;
use Mailgun\Model\Domain\UpdateUnsubscribeTrackingResponse;
use Mailgun\Model\Domain\VerifyResponse;
class DomainTest extends TestCase
@ -256,4 +260,95 @@ JSON
$api = $this->getApiInstance();
$api->create('example.com', 'foo', null, null, null, ['127.0.0.1', '127.0.0.2']);
}
public function testTracking()
{
$this->setRequestMethod('GET');
$this->setRequestUri('/v3/domains/example.com/tracking');
$this->setHydrateClass(TrackingResponse::class);
/**
* @var $api Domain
*/
$api = $this->getApiInstance();
$api->tracking('example.com');
}
public function activeInactiveDataProvider(): array
{
return [
[true],
[false],
];
}
/**
* @dataProvider activeInactiveDataProvider
*/
public function testUpdateClickTracking(bool $isActive)
{
$this->setRequestMethod('PUT');
$this->setRequestUri('/v3/domains/example.com/tracking/click');
$this->setRequestBody([
'active' => $isActive ? 'true' : 'false',
]);
$this->setHydrateClass(UpdateClickTrackingResponse::class);
/**
* @var $api Domain
*/
$api = $this->getApiInstance();
$api->updateClickTracking('example.com', $isActive);
}
/**
* @dataProvider activeInactiveDataProvider
*/
public function testUpdateOpenTracking(bool $isActive)
{
$this->setRequestMethod('PUT');
$this->setRequestUri('/v3/domains/example.com/tracking/open');
$this->setRequestBody([
'active' => $isActive ? 'true' : 'false',
]);
$this->setHydrateClass(UpdateOpenTrackingResponse::class);
/**
* @var $api Domain
*/
$api = $this->getApiInstance();
$api->updateOpenTracking('example.com', $isActive);
}
public function unsubscribeDataProvider(): array
{
return [
[true, '<b>Test</b>', 'Test1'],
[false, '<s>Test</s>', 'Test2'],
];
}
/**
* @dataProvider unsubscribeDataProvider
* @param bool $isActive
* @param string $htmlFooter
* @param string $textFooter
*/
public function testUpdateUnsubscribeTracking(bool $isActive, string $htmlFooter, string $textFooter)
{
$this->setRequestMethod('PUT');
$this->setRequestUri('/v3/domains/example.com/tracking/unsubscribe');
$this->setRequestBody([
'active' => $isActive ? 'true' : 'false',
'html_footer' => $htmlFooter,
'text_footer' => $textFooter,
]);
$this->setHydrateClass(UpdateUnsubscribeTrackingResponse::class);
/**
* @var $api Domain
*/
$api = $this->getApiInstance();
$api->updateUnsubscribeTracking('example.com', $isActive, $htmlFooter, $textFooter);
}
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Domain;
use Mailgun\Model\Domain\ClickTracking;
use Mailgun\Tests\Model\BaseModelTest;
class ClickTrackingTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"active": true
}
JSON;
$model = ClickTracking::create(json_decode($json, true));
$this->assertTrue($model->isActive());
}
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Domain;
use Mailgun\Model\Domain\OpenTracking;
use Mailgun\Tests\Model\BaseModelTest;
class OpenTrackingTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"active": true
}
JSON;
$model = OpenTracking::create(json_decode($json, true));
$this->assertTrue($model->isActive());
}
}

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Domain;
use Mailgun\Model\Domain\ClickTracking;
use Mailgun\Model\Domain\OpenTracking;
use Mailgun\Model\Domain\TrackingResponse;
use Mailgun\Model\Domain\UnsubscribeTracking;
use Mailgun\Tests\Model\BaseModelTest;
class TrackingResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"tracking": {
"click": {
"active": true
},
"open": {
"active": true
},
"unsubscribe": {
"active": true,
"html_footer": "<s>Test<\/s>",
"text_footer": "Test"
}
}
}
JSON;
$model = TrackingResponse::create(json_decode($json, true));
$this->assertNotEmpty($model->getClick());
$this->assertInstanceOf(ClickTracking::class, $model->getClick());
$this->assertTrue($model->getClick()->isActive());
$this->assertNotEmpty($model->getOpen());
$this->assertInstanceOf(OpenTracking::class, $model->getOpen());
$this->assertTrue($model->getOpen()->isActive());
$this->assertNotEmpty($model->getUnsubscribe());
$this->assertInstanceOf(UnsubscribeTracking::class, $model->getUnsubscribe());
$this->assertTrue($model->getUnsubscribe()->isActive());
$this->assertEquals('<s>Test</s>', $model->getUnsubscribe()->getHtmlFooter());
$this->assertEquals('Test', $model->getUnsubscribe()->getTextFooter());
}
}

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Domain;
use Mailgun\Model\Domain\UnsubscribeTracking;
use Mailgun\Tests\Model\BaseModelTest;
class UnsubscribeTrackingTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"active": true,
"html_footer": "<s>Test<\/s>",
"text_footer": "Test"
}
JSON;
$model = UnsubscribeTracking::create(json_decode($json, true));
$this->assertTrue($model->isActive());
$this->assertEquals('<s>Test</s>', $model->getHtmlFooter());
$this->assertEquals('Test', $model->getTextFooter());
}
}

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Domain;
use Mailgun\Model\Domain\ClickTracking;
use Mailgun\Model\Domain\UpdateClickTrackingResponse;
use Mailgun\Tests\Model\BaseModelTest;
class UpdateClickTrackingResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"click": {
"active": true
},
"message": "Domain tracking settings have been updated"
}
JSON;
$model = UpdateClickTrackingResponse::create(json_decode($json, true));
$this->assertNotEmpty($model->getMessage());
$this->assertEquals('Domain tracking settings have been updated', $model->getMessage());
$this->assertNotEmpty($model->getClick());
$this->assertInstanceOf(ClickTracking::class, $model->getClick());
$this->assertTrue($model->getClick()->isActive());
}
}

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Domain;
use Mailgun\Model\Domain\OpenTracking;
use Mailgun\Model\Domain\UpdateOpenTrackingResponse;
use Mailgun\Tests\Model\BaseModelTest;
class UpdateOpenTrackingResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"open": {
"active": true
},
"message": "Domain tracking settings have been updated"
}
JSON;
$model = UpdateOpenTrackingResponse::create(json_decode($json, true));
$this->assertNotEmpty($model->getMessage());
$this->assertEquals('Domain tracking settings have been updated', $model->getMessage());
$this->assertNotEmpty($model->getOpen());
$this->assertInstanceOf(OpenTracking::class, $model->getOpen());
$this->assertTrue($model->getOpen()->isActive());
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace Mailgun\Tests\Model\Domain;
use Mailgun\Model\Domain\UnsubscribeTracking;
use Mailgun\Model\Domain\UpdateUnsubscribeTrackingResponse;
use Mailgun\Tests\Model\BaseModelTest;
class UpdateUnsubscribeTrackingResponseTest extends BaseModelTest
{
public function testCreate()
{
$json =
<<<'JSON'
{
"unsubscribe": {
"active": true,
"html_footer": "<b>test</b>",
"text_footer": "test"
},
"message": "Domain tracking settings have been updated"
}
JSON;
$model = UpdateUnsubscribeTrackingResponse::create(json_decode($json, true));
$this->assertNotEmpty($model->getMessage());
$this->assertEquals('Domain tracking settings have been updated', $model->getMessage());
$this->assertNotEmpty($model->getUnsubscribe());
$this->assertInstanceOf(UnsubscribeTracking::class, $model->getUnsubscribe());
$this->assertTrue($model->getUnsubscribe()->isActive());
$this->assertEquals('<b>test</b>', $model->getUnsubscribe()->getHtmlFooter());
$this->assertEquals('test', $model->getUnsubscribe()->getTextFooter());
}
}