Skip to content

Commit

Permalink
Merge pull request #1 from dekalee/add-create-resource-query
Browse files Browse the repository at this point in the history
add create resource query
  • Loading branch information
nicolasThal committed Mar 23, 2017
2 parents ea3f7c7 + 7a23ac4 commit e350eba
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
94 changes: 94 additions & 0 deletions spec/Dekalee/Cdn77/Query/CreateResourceQuerySpec.php
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');
}
}
65 changes: 65 additions & 0 deletions src/Cdn77/Query/CreateResourceQuery.php
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'];
}
}

0 comments on commit e350eba

Please sign in to comment.