Added a way to get attachments (#476)

* Added a way to get attachments

* Added some tests

* cs

* minor

* Removed declare_strict

* Typos

* Bugfixes

* Fixed bugs
This commit is contained in:
Tobias Nyholm 2018-08-09 18:59:38 +02:00 committed by GitHub
parent 0c70fa1f0e
commit a4aba16061
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 124 additions and 31 deletions

View File

@ -0,0 +1,35 @@
<?php
/*
* 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\Api;
use Mailgun\Assert;
use Mailgun\Model\Attachment\Attachment as Model;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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);
}
}

View File

@ -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
*/

View File

@ -0,0 +1,33 @@
<?php
/*
* 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\Attachment;
use Mailgun\Model\ApiResponse;
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
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;
}
}

View File

@ -0,0 +1,47 @@
<?php
/*
* 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\Api;
use Mailgun\Api\Attachment;
use Mailgun\Exception\InvalidArgumentException;
use Mailgun\Model\Attachment\Attachment as Model;
class AttachmentTest extends TestCase
{
protected function getApiClass()
{
return Attachment::class;
}
public function testShow()
{
$uri = 'https://api.mailgun.org/v2/domains/mydomain.com/messages/WyJhOTM2NDk1ODA3Iiw/attachments/0';
$this->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');
}
}

View File

@ -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;

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*

View File

@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2013 Mailgun
*