-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dekalee/add-create-resource-query
add create resource query
- Loading branch information
Showing
2 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace spec\Dekalee\Cdn77\Query; | ||
|
||
use Dekalee\Cdn77\Exception\QueryErrorException; | ||
use Dekalee\Cdn77\Query\CreateResourceQuery; | ||
use Dekalee\Cdn77\Query\QueryInterface; | ||
use GuzzleHttp\Client; | ||
use PhpSpec\ObjectBehavior; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
class CreateResourceQuerySpec extends ObjectBehavior | ||
{ | ||
function let(Client $client) | ||
{ | ||
$this->beConstructedWith('login', 'password', 'url', $client); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(CreateResourceQuery::CLASS); | ||
} | ||
|
||
function it_should_be_a_query() | ||
{ | ||
$this->shouldHaveType(QueryInterface::CLASS); | ||
} | ||
|
||
function it_should_return_all_cdn_ids( | ||
Client $client, | ||
ResponseInterface $response, | ||
StreamInterface $stream | ||
) { | ||
$response->getBody()->willReturn($stream); | ||
$stream->getContents()->willReturn(json_encode([ | ||
"status" => "ok", | ||
"description" => "Request was successful.", | ||
"cdnResource" => [ | ||
"id" => "123", | ||
"origin_scheme" => "http", | ||
"origin_url" => "hosted.adback.co", | ||
"cname" => "foo.bar", | ||
"cdn_url" => "123.rsc.cdn77.org", | ||
"cache_expiry" => 17280, | ||
"url_signing_on" => 0, | ||
"qs_status" => 0, | ||
"setcookie_status" => 0, | ||
"other_cnames" => [], | ||
"label" => "yourLabel", | ||
"storage_id" => null, | ||
] | ||
])); | ||
$client->post('url', [ | ||
'form_params' => [ | ||
'login' => 'login', | ||
'passwd' => 'password', | ||
'label' => 'foo.bar', | ||
'type' => 'standard', | ||
'origin_scheme' => 'http', | ||
'origin_url' => 'hosted.adback.co', | ||
'cname' => 'foo.bar', | ||
] | ||
])->shouldBeCalled()->willReturn($response); | ||
|
||
$this->execute('foo.bar')->shouldBeEqualTo('123.rsc.cdn77.org'); | ||
} | ||
|
||
function it_should_throw_exception_if_request_not_good( | ||
Client $client, | ||
ResponseInterface $response, | ||
StreamInterface $stream | ||
) { | ||
$response->getBody()->willReturn($stream); | ||
$stream->getContents()->willReturn(json_encode([ | ||
"status" => "nok", | ||
"description" => "Request was not successful.", | ||
"cdnResource" => [] | ||
])); | ||
$client->post('url', [ | ||
'form_params' => [ | ||
'login' => 'login', | ||
'passwd' => 'password', | ||
'label' => 'foo.bar', | ||
'type' => 'standard', | ||
'origin_scheme' => 'http', | ||
'origin_url' => 'hosted.adback.co', | ||
'cname' => 'foo.bar', | ||
] | ||
])->shouldBeCalled()->willReturn($response); | ||
|
||
$this->shouldThrow(QueryErrorException::CLASS)->duringExecute('foo.bar'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Dekalee\Cdn77\Query; | ||
|
||
use Dekalee\Cdn77\Exception\QueryErrorException; | ||
use GuzzleHttp\Client; | ||
|
||
/** | ||
* Class CreateResourceQuery | ||
*/ | ||
class CreateResourceQuery implements QueryInterface | ||
{ | ||
protected $url; | ||
protected $login; | ||
protected $client; | ||
protected $password; | ||
|
||
const URL = 'https://api.cdn77.com/v2.0/cdn-resource/create'; | ||
|
||
/** | ||
* @param string $login | ||
* @param string $password | ||
* @param string $url | ||
* @param Client|null $client | ||
*/ | ||
public function __construct($login, $password, $url = self::URL, Client $client = null) | ||
{ | ||
$this->url = $url; | ||
$this->login = $login; | ||
$this->client = $client?: new Client(); | ||
$this->password = $password; | ||
} | ||
|
||
/** | ||
* @param string|null $domain | ||
* | ||
* @return mixed|null | ||
* @throws QueryErrorException | ||
*/ | ||
public function execute($domain = null) | ||
{ | ||
$response = $this->client->post( | ||
$this->url, | ||
[ | ||
'form_params' => [ | ||
'login' => $this->login, | ||
'passwd' => $this->password, | ||
'label' => $domain, | ||
'type' => 'standard', | ||
'origin_scheme' => 'http', | ||
'origin_url' => 'hosted.adback.co', | ||
'cname' => $domain, | ||
] | ||
] | ||
); | ||
|
||
$data = json_decode($response->getBody()->getContents(), true); | ||
|
||
if ('ok' != $data['status']) { | ||
throw new QueryErrorException($data['description']); | ||
} | ||
|
||
return $data['cdnResource']['cdn_url']; | ||
} | ||
} |