diff --git a/src/Mailgun/Api/Attachment.php b/src/Mailgun/Api/Attachment.php new file mode 100644 index 0000000..008aef9 --- /dev/null +++ b/src/Mailgun/Api/Attachment.php @@ -0,0 +1,35 @@ + + */ +class Attachment extends HttpApi +{ + /** + * @param string $url + * + * @return Model + */ + public function show($url) + { + Assert::stringNotEmpty($url); + Assert::regex($url, '@https://.*mailgun\.(net|org)/v.+@'); + Assert::regex($url, '|/attachments/[0-9]+|'); + + $response = $this->httpGet($url); + + return $this->hydrateResponse($response, Model::class); + } +} diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 561f607..4bab3c7 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -326,6 +326,14 @@ class Mailgun return new Api\Stats($this->httpClient, $this->requestBuilder, $this->hydrator); } + /** + * @return Api\Attachment + */ + public function attachment() + { + return new Api\Attachment($this->httpClient, $this->requestBuilder, $this->hydrator); + } + /** * @return Api\Domain */ diff --git a/src/Mailgun/Model/Attachment/Attachment.php b/src/Mailgun/Model/Attachment/Attachment.php new file mode 100644 index 0000000..f9ebe7e --- /dev/null +++ b/src/Mailgun/Model/Attachment/Attachment.php @@ -0,0 +1,33 @@ + + */ +class Attachment implements ApiResponse +{ + private $data; + + public static function create(array $data) + { + $new = new self(); + $new->data = $data; + + return $new; + } + + public function getData() + { + return $this->data; + } +} diff --git a/tests/Api/AttachmentTest.php b/tests/Api/AttachmentTest.php new file mode 100644 index 0000000..7f1db98 --- /dev/null +++ b/tests/Api/AttachmentTest.php @@ -0,0 +1,47 @@ +setRequestMethod('GET'); + $this->setHydrateClass(Model::class); + $this->setRequestUri($uri); + + $api = $this->getApiInstance(); + $api->show($uri); + } + + public function testShowWrongUri() + { + $api = $this->getApiInstance(); + $this->expectException(InvalidArgumentException::class); + $api->show('https://api.mailgun.org/v2/domains/mydomain.com'); + } + + public function testShowNonMailgunUri() + { + $api = $this->getApiInstance(); + $this->expectException(InvalidArgumentException::class); + $api->show('https://example.com/v2/domains/mailgun.net?x=attachments/0'); + } +} diff --git a/tests/Api/TagTest.php b/tests/Api/TagTest.php index c6f1755..6fd83d8 100644 --- a/tests/Api/TagTest.php +++ b/tests/Api/TagTest.php @@ -7,7 +7,7 @@ * of the MIT license. See the LICENSE file for details. */ -namespace Mailgun\tests\Api; +namespace Mailgun\Tests\Api; use GuzzleHttp\Psr7\Response; use Mailgun\Api\Tag; diff --git a/tests/Model/BaseModelTest.php b/tests/Model/BaseModelTest.php index 5fa268d..5b73f0e 100644 --- a/tests/Model/BaseModelTest.php +++ b/tests/Model/BaseModelTest.php @@ -1,7 +1,5 @@