Skip to content

Commit

Permalink
Merge pull request #3 from dekalee/feature/add-delete-query
Browse files Browse the repository at this point in the history
Feature/add delete query
  • Loading branch information
nicolasThal committed Apr 11, 2018
2 parents 2cee2ca + ba7fda9 commit 465f06f
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 40 deletions.
67 changes: 67 additions & 0 deletions spec/Dekalee/Cdn77/Query/DeleteResourceQuerySpec.php
@@ -0,0 +1,67 @@
<?php

namespace spec\Dekalee\Cdn77\Query;

use Dekalee\Cdn77\Exception\QueryErrorException;
use Dekalee\Cdn77\Query\DeleteResourceQuery;
use Dekalee\Cdn77\Query\ListResourcesQuery;
use Dekalee\Cdn77\Query\QueryInterface;
use GuzzleHttp\Client;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class DeleteResourceQuerySpec extends ObjectBehavior
{
function let(ListResourcesQuery $listResourcesQuery, Client $client)
{
$listResourcesQuery->execute()->willReturn([['id' => 1, 'cname' => 'foo.bar.co'], ['id' => 2, 'cname' => 'foo.bar.co'], ['id' => 3, 'cname' => 'example.test.com']]);
$this->beConstructedWith($listResourcesQuery, 'login', 'password', 'url', $client);
}

function it_is_initializable()
{
$this->shouldHaveType(DeleteResourceQuery::CLASS);
}

function it_should_be_a_query()
{
$this->shouldHaveType(QueryInterface::CLASS);
}

function it_should_delete_one_resource(
Client $client,
ResponseInterface $response,
StreamInterface $stream
) {
$client->post('url', [
'form_params' => [
'id' => 3,
'login' => 'login',
'passwd' => 'password',
]
])->shouldBeCalled()->willReturn($response);
$response->getBody()->willReturn($stream);
$stream->getContents()->willReturn(json_encode([
'status' => 'ok',
'description' => "Request was successful.",
]));

$this->execute('example.test.com');
}

function it_should_throw_exception_when_deleting_multiple_resources(Client $client)
{
$client->post(Argument::any())->shouldNotBeCalled();

$this->shouldThrow(QueryErrorException::CLASS)->during('execute', ['foo.bar.co']);
}

function it_should_throw_exception_when_no_resources_given(Client $client)
{
$client->post(Argument::any())->shouldNotBeCalled();

$this->shouldThrow(QueryErrorException::CLASS)->during('execute');
}
}
41 changes: 3 additions & 38 deletions src/Cdn77/Query/AbstractPurgeQuery.php
Expand Up @@ -2,46 +2,11 @@

namespace Dekalee\Cdn77\Query;

use GuzzleHttp\Client;

/**
* Class AbstractPurgeQuery
*
* @deprecated will be removed in 2.0.0 use AbstractResourcesAwareQuery
*/
abstract class AbstractPurgeQuery
abstract class AbstractPurgeQuery extends AbstractResourcesAwareQuery
{
protected $url;
protected $login;
protected $client;
protected $password;
protected $listResourcesQuery;

const URL = '';

protected $cdnResources;

/**
* @param ListResourcesQuery $listResourcesQuery
* @param string $login
* @param string $password
* @param string $url
* @param Client|null $client
*/
public function __construct(ListResourcesQuery $listResourcesQuery, $login, $password, $url = null, Client $client = null)
{
$this->url = null === $url?static::URL: $url;
$this->login = $login;
$this->client = $client?: new Client();
$this->password = $password;
$this->listResourcesQuery = $listResourcesQuery;
}

/**
* Populate CDN resources
*/
protected function populateCDNResources()
{
if (null === $this->cdnResources) {
$this->cdnResources = $this->listResourcesQuery->execute();
}
}
}
47 changes: 47 additions & 0 deletions src/Cdn77/Query/AbstractResourcesAwareQuery.php
@@ -0,0 +1,47 @@
<?php

namespace Dekalee\Cdn77\Query;

use GuzzleHttp\Client;

/**
* Class AbstractResourcesAwareQuery
*/
abstract class AbstractResourcesAwareQuery
{
protected $url;
protected $login;
protected $client;
protected $password;
protected $listResourcesQuery;

const URL = '';

protected $cdnResources;

/**
* @param ListResourcesQuery $listResourcesQuery
* @param string $login
* @param string $password
* @param string $url
* @param Client|null $client
*/
public function __construct(ListResourcesQuery $listResourcesQuery, $login, $password, $url = null, Client $client = null)
{
$this->url = null === $url?static::URL: $url;
$this->login = $login;
$this->client = $client?: new Client();
$this->password = $password;
$this->listResourcesQuery = $listResourcesQuery;
}

/**
* Populate CDN resources
*/
protected function populateCDNResources()
{
if (null === $this->cdnResources) {
$this->cdnResources = $this->listResourcesQuery->execute();
}
}
}
53 changes: 53 additions & 0 deletions src/Cdn77/Query/DeleteResourceQuery.php
@@ -0,0 +1,53 @@
<?php

namespace Dekalee\Cdn77\Query;

use Dekalee\Cdn77\Exception\QueryErrorException;

/**
* Class DeleteResourceQuery
*/
class DeleteResourceQuery extends AbstractResourcesAwareQuery implements QueryInterface
{
const URL = 'https://api.cdn77.com/v2.0/cdn-resource/delete';

/**
* @param null $resource
*
* @return mixed|null
* @throws QueryErrorException
*/
public function execute($resource = null)
{
if (null === $resource) {
throw new QueryErrorException('Could not delete no resource');
}

$this->populateCDNResources();

$cdnResources = array_filter($this->cdnResources, function($value, $key) use ($resource) {
return $value['cname'] == $resource;
}, ARRAY_FILTER_USE_BOTH);

if (count($cdnResources) > 1) {
throw new QueryErrorException('Could not delete more than one resource');
}

foreach ($cdnResources as $cdnResource) {
$response = $this->client->post($this->url, [
'form_params' => [
'id' => $cdnResource['id'],
'login' => $this->login,
'passwd' => $this->password,
]
]);
$data = json_decode($response->getBody()->getContents(), true);

if ('ok' !== $data['status']) {
throw new QueryErrorException(json_encode($data));
}
}

return;
}
}
2 changes: 1 addition & 1 deletion src/Cdn77/Query/PurgeAllQuery.php
Expand Up @@ -5,7 +5,7 @@
/**
* Class PurgeAllQuery
*/
class PurgeAllQuery extends AbstractPurgeQuery implements QueryInterface
class PurgeAllQuery extends AbstractResourcesAwareQuery implements QueryInterface
{
const URL = 'https://api.cdn77.com/v2.0/data/purge';

Expand Down
2 changes: 1 addition & 1 deletion src/Cdn77/Query/PurgeFileQuery.php
Expand Up @@ -8,7 +8,7 @@
/**
* Class PurgeFileQuery
*/
class PurgeFileQuery extends AbstractPurgeQuery implements QueryInterface
class PurgeFileQuery extends AbstractResourcesAwareQuery implements QueryInterface
{
const URL = 'https://api.cdn77.com/v2.0/data/purge';

Expand Down

0 comments on commit 465f06f

Please sign in to comment.