mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-25 06:16:03 +03:00
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:
parent
0c70fa1f0e
commit
a4aba16061
35
src/Mailgun/Api/Attachment.php
Normal file
35
src/Mailgun/Api/Attachment.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
33
src/Mailgun/Model/Attachment/Attachment.php
Normal file
33
src/Mailgun/Model/Attachment/Attachment.php
Normal 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;
|
||||
}
|
||||
}
|
47
tests/Api/AttachmentTest.php
Normal file
47
tests/Api/AttachmentTest.php
Normal 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');
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
@ -1,7 +1,5 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Mailgun
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user