Exception treatments

This commit is contained in:
weryques 2017-12-12 09:59:50 -02:00
parent 48a79a24dd
commit c4f47882b4
14 changed files with 1242 additions and 60 deletions

1149
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -34,11 +34,12 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
'args' => $this->get_item_schema()
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array($this, 'create_item'),
//'permission_callback' => array($this, 'create_item_permissions_check'),
'permission_callback' => array($this, 'create_item_permissions_check'),
'args' => $this->get_endpoint_args_for_item_schema(WP_REST_Server::CREATABLE),
),
));
@ -118,9 +119,11 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return json_encode($collections_as_json);
}
else {
elseif(!empty($item)){
return $item->__toJSON();
}
return $item;
}
/**
@ -154,13 +157,11 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
public function create_item( $request ) {
$request = json_decode($request->get_body(), true);
if(empty($request)) {
return new WP_Error('rest_empty', __('Empty request.'), array('status' => 204));
} elseif (!empty($request['collection_id'])){
return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
}
try {
$prepared_post = $this->prepare_item_for_database( $request );
} catch (\Error $exception){
return new WP_REST_Response($exception->getMessage(), 400);
}
if($prepared_post->validate()) {
$collection = $this->collections_repository->insert( $prepared_post );
@ -179,13 +180,9 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return bool|WP_Error
*/
public function create_item_permissions_check( $request ) {
if(current_user_can('edit_posts')){
return true;
}
return false;
}
/**
* Prepare collection for insertion on database
*
@ -194,8 +191,11 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return object|Entities\Collection|WP_Error
*/
public function prepare_item_for_database( $request ) {
$this->collection->set_name($request['name']);
$this->collection->set_description($request['description']);
foreach ($request as $key => $value){
$set_ = 'set_' . $key;
$this->collection->$set_($value);
}
return $this->collection;
}
@ -251,13 +251,9 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
* @return bool|WP_Error
*/
public function update_item_permissions_check( $request ) {
if(current_user_can('edit_posts')){
return true;
}
return false;
}
public function get_collection_params() {
$query_params = $this->collections_repository->get_map();
@ -274,6 +270,12 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $args, $this->collection->get_post_type());
}
public function get_item_schema() {
$args = $this->collections_repository->get_map();
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $args, $this->collection->get_post_type());
}
}
?>

View File

@ -141,8 +141,13 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
* @return object|Entities\Item|WP_Error
*/
public function prepare_item_for_database( $request ) {
$this->item->set_title($request[0]['title']);
$this->item->set_description($request[0]['description']);
$item_as_array = $request[0];
foreach ($item_as_array as $key => $value){
$set_ = 'set_' . $key;
$this->item->$set_($value);
}
$collection = new Entities\Collection($request[1]);
@ -176,7 +181,11 @@ class TAINACAN_REST_Items_Controller extends WP_REST_Controller {
$collection_id = $request['collection_id'];
$item = json_decode($request->get_body(), true);
try {
$metadata = $this->prepare_item_for_database( [ $item, $collection_id ] );
} catch (\Error $exception){
return new WP_REST_Response($exception->getMessage(), 400);
}
if($this->item->validate()) {
$item = $this->items_repository->insert($this->item );

View File

@ -5,7 +5,6 @@ use Tainacan\Repositories;
class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
private $metadata;
private $item_metadata;
private $metadata_repository;
private $item_metadata_repository;
private $item_repository;
@ -65,23 +64,38 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
);
}
/**
* @param WP_REST_Request $request
*
* @return object|void|WP_Error
*/
public function prepare_item_for_database( $request ) {
$meta = json_decode($request[0]->get_body(), true);
$this->metadata->set_name($meta['name']);
$this->metadata->set_description($meta['description']);
$this->metadata->set_field_type($meta['field_type']);
foreach ($meta as $key => $value){
$set_ = 'set_' . $key;
$this->metadata->$set_($value);
}
$collection = new Entities\Collection($request[1]);
$this->metadata->set_collection($collection);
}
/**
* @param WP_REST_Request $request
*
* @return WP_Error|WP_REST_Response
*/
public function create_item( $request ) {
if(!empty($request['collection_id'])){
$collection_id = $request['collection_id'];
try {
$this->prepare_item_for_database( [ $request, $collection_id ] );
} catch (\Error $exception){
return new WP_REST_Response($exception->getMessage(), 400);
}
if($this->metadata->validate()) {
$this->metadata_repository->insert( $this->metadata );
@ -98,10 +112,15 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
$metadata_added = $this->item_metadata_repository->insert($item_meta);
}
}
return new WP_REST_Response($metadata_added->get_metadata()->__toJSON(), 201);
}
else {
return new WP_REST_Response($this->metadata->__toJSON(), 201);
}
} else {
return new WP_REST_Response($this->metadata->get_errors(), 200);
}
} elseif (!empty($request['item_id']) && !empty($request->get_body())){
$body = json_decode($request->get_body(), true);
@ -119,6 +138,8 @@ class TAINACAN_REST_Metadata_Controller extends WP_REST_Controller {
$metadata_updated = $this->item_metadata_repository->insert( $item_metadata );
return new WP_REST_Response( $metadata_updated->get_metadata()->__toJSON(), 201 );
} else {
return new WP_REST_Response( $item_metadata->get_errors(), 200);
}
} else {
return new WP_REST_Response($request->get_body(), 400);

View File

@ -16,8 +16,7 @@ class Log extends Entity {
*/
protected $repository = 'Tainacan_Logs';
public function __construct($which=0)
{
public function __construct($which=0) {
parent::__construct($which);
if( is_int($which) && $which == 0) {

View File

@ -162,8 +162,13 @@ class Collections extends Repository {
}
/**
* @param $args ( is a array like [post_id, [is_permanently => bool]] )
*
* @return mixed|Collection
*/
public function delete($args){
if($args[1]['is_permanently'] === true){
if(!empty($args[1]) && $args[1]['is_permanently'] === true){
return new Entities\Collection(wp_delete_post($args[0], $args[1]['is_permanently']));
}

View File

@ -200,8 +200,13 @@ class Items extends Repository {
}
/**
* @param $args ( is a array like [post_id, [is_permanently => bool]] )
*
* @return mixed|Entities\Item
*/
public function delete($args){
if($args[1]['is_permanently'] === true){
if(!empty($args[1]) && $args[1]['is_permanently'] === true){
return new Entities\Item(wp_delete_post($args[0], $args[1]['is_permanently']));
}

View File

@ -5,6 +5,7 @@ Plugin Name: Tainacan
Plugin URI: https://github.com/tainacan/tainacan
Description: Transforme seu site Wordpress em um repositório digital
Author: Media Lab / UFG
Author URI: https://www.medialab.ufg.br
Version: 1.0
*/

View File

@ -26,7 +26,7 @@ class Entity_Factory {
try {
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
throw new \InvalidArgumentException(__('The type can\'t be empty', 'tainacan'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {
@ -62,7 +62,7 @@ class Entity_Factory {
if ($this->entity->validate()) {
$this->entity = $this->repository->insert($this->entity);
} else {
throw new \ErrorException( __( 'The entity wasn\'t validated.' ) );
throw new \ErrorException( __( 'The entity wasn\'t validated.', 'tainacan' ) );
}
} elseif (!empty($args) && !$is_validated_and_in_db){
@ -99,7 +99,7 @@ class Entity_Factory {
if ($this->entity->validate()) {
$this->entity = $this->repository->insert( $this->entity );
} else {
throw new \ErrorException( __( 'The entity wasn\'t validated.' ) );
throw new \ErrorException( __( 'The entity wasn\'t validated.', 'tainacan' ) );
}
} else {

View File

@ -8,7 +8,7 @@ class Field_Factory {
public function create_field($type, $primitive_type = []){
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
throw new \InvalidArgumentException(__('The type can\'t be empty', 'tainacan'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {

View File

@ -8,7 +8,7 @@ class Filter_Factory {
public function create_filter($type, $supported_types = []){
if(empty($type)){
throw new \InvalidArgumentException(__('The type can\'t be empty'));
throw new \InvalidArgumentException(__('The type can\'t be empty', 'tainacan'));
} elseif(!strrchr($type, '_')){
$type = ucfirst(strtolower($type));
} else {

View File

@ -13,8 +13,7 @@ use Tainacan\Repositories\Repository;
* @group architecture
*/
class Objects extends TAINACAN_UnitTestCase {
function test_object_transformation()
{
function test_object_transformation() {
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(