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 #3 from dekalee/feature/add-delete-query
Feature/add delete query
- Loading branch information
Showing
6 changed files
with
172 additions
and
40 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,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'); | ||
} | ||
} |
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
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,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(); | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} |
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
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