-
Notifications
You must be signed in to change notification settings - Fork 11
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 #18 from crypto-scythe/master
Implement delete action
- Loading branch information
Showing
10 changed files
with
208 additions
and
15 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ vendor/ | |
data/ | ||
.env | ||
composer.lock | ||
phpunit.xml | ||
phpunit.xml | ||
.phpunit.result.cache |
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,72 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Phagrancy\Action\Api\Scope\Box\Delete | ||
*/ | ||
|
||
namespace Phagrancy\Action\Api\Scope\Box; | ||
|
||
use Phagrancy\Http\Response; | ||
use Phagrancy\Model\Input; | ||
use Phagrancy\Model\Repository; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* Action for deleting a box from the server | ||
* | ||
* @package Phagrancy\Action\Api\Scope\Box | ||
*/ | ||
class Delete | ||
{ | ||
/** | ||
* @var Repository\Box | ||
*/ | ||
private $boxes; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $storagePath; | ||
|
||
/** | ||
* @var Input\BoxDelete | ||
*/ | ||
private $input; | ||
|
||
public function __construct(Repository\Box $boxes, Input\BoxDelete $input, $storagePath) | ||
{ | ||
$this->boxes = $boxes; | ||
$this->input = $input; | ||
$this->storagePath = $storagePath; | ||
} | ||
|
||
public function __invoke(ServerRequestInterface $request) | ||
{ | ||
/** | ||
* The route controls these params, and they are validated so safe | ||
* | ||
* @var string $name | ||
* @var string $scope | ||
* @var string $version | ||
* @var string $provider | ||
*/ | ||
$params = $this->input->validate($request->getAttribute('route')->getArguments()); | ||
if (!$params) { | ||
return new Response\NotFound(); | ||
} | ||
|
||
extract($params); | ||
$box = $this->boxes->ofNameInScope($name, $scope); | ||
|
||
if ($box) { | ||
$path = "{$this->storagePath}/{$box->path()}/{$version}/{$provider}.box"; | ||
|
||
if (file_exists($path) && unlink($path)) { | ||
return new Response\Json([]); | ||
} | ||
} | ||
|
||
return new Response\NotFound(); | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Phagrancy\Model\Input\BoxDelete | ||
*/ | ||
|
||
namespace Phagrancy\Model\Input; | ||
|
||
use Validator\LIVR; | ||
|
||
/** | ||
* Input for the BoxDelete | ||
* | ||
* Validates the scope, name, version, provider | ||
* | ||
* @package Phagrancy\Model\Input | ||
*/ | ||
class BoxDelete | ||
{ | ||
use IsValidator, ValidatesScope, ValidatesBoxName, ValidatesVersion; | ||
|
||
public function __construct() | ||
{ | ||
LIVR::registerDefaultRules( | ||
[ | ||
'scope' => [$this, 'validateScope'], | ||
'name' => [$this, 'validateBoxName'], | ||
'version' => [$this, 'validateVersion'], | ||
'provider' => [$this, 'validateProvider'] | ||
]); | ||
} | ||
|
||
public function validate($params) | ||
{ | ||
return $this->perform( | ||
$params, | ||
[ | ||
'scope' => self::$SCOPE_RULE, | ||
'name' => self::$BOX_NAME_RULE, | ||
'version' => self::$VERSION_RULE, | ||
'provider' => ['required', 'trim', 'to_lc'] | ||
]); | ||
} | ||
} |
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
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
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,49 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Phagrancy\Action\Api\Scope\Box\UploadTest | ||
*/ | ||
|
||
namespace Phagrancy\Action\Api\Scope\Box; | ||
|
||
use Phagrancy\Http\Response\Json; | ||
use Phagrancy\Model\Input\BoxDelete; | ||
use Phagrancy\Model\Repository\Box; | ||
use Phagrancy\TestCase\Scope as ScopeTestCase; | ||
|
||
class DeleteTest | ||
extends ScopeTestCase | ||
{ | ||
public function testReturnsOkAndRemovesExistingBox() | ||
{ | ||
$request = $this->buildRequest(); | ||
$request->getAttribute('route') | ||
->setArguments( | ||
[ | ||
'scope' => 'test', | ||
'name' => 'delete', | ||
'version' => '1.0.0', | ||
'provider' => 'test' | ||
]); | ||
|
||
// build the action itself | ||
$action = new Delete( | ||
new Box($this->fs->url()), | ||
new BoxDelete(), | ||
$this->fs->url() | ||
); | ||
|
||
// box file exists before action | ||
self::assertTrue(file_exists($this->fs->url() . '/test/delete/1.0.0/test.box')); | ||
|
||
$response = $action($request); | ||
|
||
// file was removed | ||
self::assertFalse(file_exists($this->fs->url() . '/test/delete/1.0.0/test.box')); | ||
self::assertInstanceOf(Json::class, $response); | ||
self::assertResponseHasStatus($response, 200); | ||
|
||
$response->getBody()->close(); | ||
} | ||
} |