Cleanup API

This commit is contained in:
Romain Neutron
2014-02-24 23:38:20 +01:00
parent 2df0f9d659
commit 08efd8ff72
43 changed files with 2676 additions and 5127 deletions

View File

@@ -276,12 +276,11 @@ class API_OAuth2_Token
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
if ( ! $row)
if (!$row) {
throw new NotFoundHttpException('Account not found');
}
$account = new API_OAuth2_Account($app, $row['api_account_id']);
return new self($app['phraseanet.appbox'], $account);
return new self($app['phraseanet.appbox'], new API_OAuth2_Account($app, $row['api_account_id']));
}
/**

View File

@@ -1,15 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
abstract class API_V1_Abstract implements API_V1_Interface
{
}

View File

@@ -1,309 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Model\Entities\Basket;
use Alchemy\Phrasea\Model\Entities\User;
use Symfony\Component\HttpFoundation\Request;
use Silex\Application;
interface API_V1_Interface
{
public function get_version();
/**
* Route : /databoxes/list/FORMAT/
*
* Method : GET
*
* Parameters :
*
*/
public function get_databoxes(Request $request);
/**
* Route /databoxes/DATABOX_ID/collections/FORMAT/
*
* Method : GET
*
* Parameters ;
* DATABOX_ID : required INT
*/
public function get_databox_collections(Request $request, $databox_id);
/**
* Route /databoxes/DATABOX_ID/status/FORMAT/
*
* Method : GET
*
* Parameters ;
* DATABOX_ID : required INT
*/
public function get_databox_status(Request $request, $databox_id);
/**
* Route /databoxes/DATABOX_ID/metadatas/FORMAT/
*
* Method : GET
*
* Parameters ;
* DATABOX_ID : required INT
*/
public function get_databox_metadatas(Request $request, $databox_id);
/**
* Route /databoxes/DATABOX_ID/termsOfUse/FORMAT/
*
* Method : GET
*
* Parameters ;
* DATABOX_ID : required INT
*/
public function get_databox_terms(Request $request, $databox_id);
/**
* Route : /records/search/FORMAT/
*
* Method : GET or POST
*
* Parameters :
* bases[] : array
* status[] : array
* fields[] : array
* record_type : boolean
* media_type : string
*
* Response :
* Array of record objects
*
*/
public function search_records(Request $request);
/**
* Route : /records/DATABOX_ID/RECORD_ID/related/FORMAT/
*
* Method : GET
*
* Parameters :
* DATABOX_ID : required INT
* RECORD_ID : required INT
*
*/
public function get_record_related(Request $request, $databox_id, $record_id);
/**
* Route : /records/DATABOX_ID/RECORD_ID/metadatas/FORMAT/
*
* Method : GET
*
* Parameters :
* DATABOX_ID : required INT
* RECORD_ID : required INT
*
*/
public function get_record_metadatas(Request $request, $databox_id, $record_id);
/**
* Route : /records/DATABOX_ID/RECORD_ID/status/FORMAT/
*
* Method : GET
*
* Parameters :
* DATABOX_ID : required INT
* RECORD_ID : required INT
*
*/
public function get_record_status(Request $request, $databox_id, $record_id);
/**
* Route : /records/DATABOX_ID/RECORD_ID/embed/FORMAT/
*
* Method : GET
*
* Parameters :
* DATABOX_ID : required INT
* RECORD_ID : required INT
*
*/
public function get_record_embed(Request $request, $databox_id, $record_id);
/**
* Route : /records/DATABOX_ID/RECORD_ID/setmetadatas/FORMAT/
*
* Method : POST
*
* Parameters :
* DATABOX_ID : required INT
* RECORD_ID : required INT
*
*/
public function set_record_metadatas(Request $request, $databox_id, $record_id);
/**
* Route : /records/DATABOX_ID/RECORD_ID/setstatus/FORMAT/
*
* Method : POST
*
* Parameters :
* DATABOX_ID : required INT
* RECORD_ID : required INT
*
*/
public function set_record_status(Request $request, $databox_id, $record_id);
/**
* Route : /records/DATABOX_ID/RECORD_ID/setcollection/FORMAT/
*
* Method : POST
*
* Parameters :
* DATABOX_ID : required INT
* RECORD_ID : required INT
*
*/
public function set_record_collection(Request $request, $databox_id, $record_id);
/**
* Route : /baskets/list/FORMAT/
*
* Method : POST
*
* Parameters :
*
*/
public function search_baskets(Request $request);
/**
* Route : /baskets/add/FORMAT/
*
* Method : POST
*
* Parameters :
*
*/
public function create_basket(Request $request);
/**
* Route : /baskets/BASKET_ID/delete/FORMAT/
*
* Method : POST
*
* Parameters :
* BASKET_ID : required INT
*
*/
public function delete_basket(Request $request, Basket $basket);
/**
* Route : /baskets/BASKET_ID/content/FORMAT/
*
* Method : POST
*
* Parameters :
* BASKET_ID : required INT
*
*/
public function get_basket(Request $request, Basket $basket);
/**
* Route : /baskets/BASKET_ID/title/FORMAT/
*
* Method : GET
*
* Parameters :
* BASKET_ID : required INT
*
*/
public function set_basket_title(Request $request, Basket $basket);
/**
* Route : /baskets/BASKET_ID/description/FORMAT/
*
* Method : POST
*
* Parameters :
* BASKET_ID : required INT
*
*/
public function set_basket_description(Request $request, Basket $basket);
/**
* Route : /publications/list/FORMAT/
*
* Method : POST
*
* Parameters :
*
*/
public function search_publications(Request $request, User $user);
/**
* Route : /publications/PUBLICATION_ID/remove/FORMAT/
*
* Method : GET
*
* Parameters :
* PUBLICATION_ID : required INT
*
*/
public function remove_publications(Request $request, $publication_id);
/**
* Route : /publications/PUBLICATION_ID/content/FORMAT/
*
* Method : GET
*
* Parameters :
* PUBLICATION_ID : required INT
*
*/
public function get_publication(Request $request, $publication_id, User $user);
public function get_publications(Request $request, User $user);
public function get_feed_entry(Request $request, $entry, User $user);
/**
* Route : /users/search/FORMAT/
*
* Method : POST-GET
*
* Parameters :
*
*/
public function search_users(Request $request);
/**
* Route : /users/USER_ID/access/FORMAT/
*
* Method : GET
*
* Parameters :
* USER_ID : required INT
*
*/
public function get_user_acces(Request $request, $usr_id);
public function add_record(Application $app, Request $request);
/**
* Route : /users/add/FORMAT/
*
* Method : POST
*
* Parameters :
*
*/
public function add_user(Request $request);
public function get_error_message(Request $request, $error, $message);
public function get_error_code(Request $request, $code);
}

View File

@@ -1,391 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Application;
class API_V1_Log
{
const DATABOXES_RESSOURCE = 'databoxes';
const RECORDS_RESSOURCE = 'record';
const BASKETS_RESSOURCE = 'baskets';
const FEEDS_RESSOURCE = 'feeds';
/**
*
* @var int
*/
protected $id;
/**
*
* @var int
*/
protected $account_id;
/**
*
* @var DateTime
*/
protected $date;
/**
*
* @var int
*/
protected $status_code;
/**
*
* @var string
*/
protected $format;
/**
*
* @var string
*/
protected $ressource;
/**
*
* @var string
*/
protected $general;
/**
*
* @var string
*/
protected $aspect;
/**
*
* @var string
*/
protected $action;
/**
*
* @var API_OAuth2_Account
*/
protected $account;
/**
*
* @var Application
*/
protected $app;
/**
*
* @param Application $app
* @param integer $log_id
*/
public function __construct(Application $app, $log_id)
{
$this->app = $app;
$this->id = (int) $log_id;
$sql = '
SELECT
api_log_id,
api_account_id,
api_log_route,
api_log_date,
api_log_status_code,
api_log_format,
api_log_ressource,
api_log_general,
api_log_aspect,
api_log_action
FROM
api_logs
WHERE
api_log_id = :log_id';
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute([':log_id' => $this->id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
$this->account_id = $row['api_account_id'];
$this->account = new API_OAuth2_Account($this->app, (int) $row['api_account_id']);
$this->aspect = $row['api_log_aspect'];
$this->date = new DateTime($row['api_log_date']);
$this->format = $row['api_log_format'];
$this->general = $row['api_log_general'];
$this->ressource = $row['api_log_ressource'];
$this->status_code = (int) $row['api_log_status_code'];
return $this;
}
public function get_account_id()
{
return $this->account_id;
}
public function set_account_id($account_id)
{
$this->account_id = $account_id;
$sql = 'UPDATE api_log
SET api_account_id = :account_id
WHERE api_log_id = :log_id';
$params = [
':api_account_id' => $this->account_id
, ':log_id' => $this->id
];
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
return $this;
}
public function get_date()
{
return $this->date;
}
public function set_date(DateTime $date)
{
$this->date = $date;
$sql = 'UPDATE api_log
SET api_log_date = :date
WHERE api_log_id = :log_id';
$params = [
':date' => $this->date->format("Y-m-d H:i:s")
, ':log_id' => $this->id
];
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
return $this;
}
public function get_status_code()
{
return $this->status_code;
}
public function set_status_code($status_code)
{
$this->status_code = (int) $status_code;
$sql = 'UPDATE api_log
SET api_log_status_code = :code
WHERE api_log_id = :log_id';
$params = [
':code' => $this->status_code
, ':log_id' => $this->id
];
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
return $this;
}
public function get_format()
{
return $this->format;
}
public function set_format($format)
{
if ( ! in_array($format, ['json', 'jsonp', 'yaml', 'unknow']))
throw new Exception_InvalidArgument();
$this->format = $format;
$sql = 'UPDATE api_log
SET api_log_format = :format
WHERE api_log_id = :log_id';
$params = [
':format' => $this->format
, ':log_id' => $this->id
];
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
return $this;
}
public function get_ressource()
{
return $this->ressource;
}
public function set_ressource($ressource)
{
if ( ! in_array($format, [self::DATABOXES_RESSOURCE, self::BASKETS_RESSOURCE, self::FEEDS_RESSOURCE, self::RECORDS_RESSOURCE]))
throw new Exception_InvalidArgument();
$this->ressource = $ressource;
$sql = 'UPDATE api_log
SET api_log_ressource = :ressource
WHERE api_log_id = :log_id';
$params = [
':ressource' => $this->ressource
, ':log_id' => $this->id
];
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
return $this;
}
public function get_general()
{
return $this->general;
}
public function set_general($general)
{
$this->general = $general;
$sql = 'UPDATE api_log
SET api_log_general = :general
WHERE api_log_id = :log_id';
$params = [
':general' => $this->general
, ':log_id' => $this->id
];
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
return $this;
}
public function get_aspect()
{
return $this->aspect;
}
public function set_aspect($aspect)
{
$this->aspect = $aspect;
$sql = 'UPDATE api_log
SET api_log_aspect = :aspect
WHERE api_log_id = :log_id';
$params = [
':aspect' => $this->aspect
, ':log_id' => $this->id
];
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
return $this;
}
public function get_action()
{
return $this->action;
}
public function set_action($action)
{
$this->action = $action;
$sql = 'UPDATE api_log
SET api_log_action = :action
WHERE api_log_id = :log_id';
$params = [
':action' => $this->action
, ':log_id' => $this->id
];
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
return $this;
}
public function get_account()
{
return $this->account;
}
public static function create(Application $app, API_OAuth2_Account $account, $route, $status_code, $format, $ressource, $general = null, $aspect = null, $action = null)
{
$sql = '
INSERT INTO
api_logs (
api_log_id,
api_account_id,
api_log_route,
api_log_date,
api_log_status_code,
api_log_format,
api_log_ressource,
api_log_general,
api_log_aspect,
api_log_action
)
VALUES (
null,
:account_id,
:route,
NOW(),
:status_code,
:format,
:ressource,
:general,
:aspect,
:action
)';
$params = [
':account_id' => $account->get_id(),
':route' => $route,
':status_code' => $status_code,
':format' => $format,
':ressource' => $ressource,
':general' => $general,
':aspect' => $aspect,
':action' => $action
];
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$stmt->closeCursor();
$log_id = $app['phraseanet.appbox']->get_connection()->lastInsertId();
return new self($app, $log_id);
}
}

View File

@@ -1,61 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Silex\Application;
use Silex\ServiceProviderInterface;
use Alchemy\Phrasea\Core\PhraseaEvents;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\KernelEvents;
class API_V1_Timer implements ServiceProviderInterface
{
private $starting;
public function __construct()
{
$this->starting = microtime(true);
}
public function register(Application $app)
{
$app['api.timers'] = new ArrayCollection();
$app['api.timers.start'] = $this->starting;
$callback = function (Event $event) use ($app) {
$name = $event->getName();
$n = 1;
while (isset($app['api.timers']->{$name})) {
$n++;
$name = $event->getName() . '#' . $n;
}
$app['api.timers']->add([
'name' => $name,
'memory' => memory_get_usage(),
'time' => microtime(true) - $app['api.timers.start'],
]);
};
$app['dispatcher']->addListener(KernelEvents::CONTROLLER, $callback, -999999);
$app['dispatcher']->addListener(KernelEvents::REQUEST, $callback, 999999);
$app['dispatcher']->addListener(KernelEvents::REQUEST, $callback, -999999);
$app['dispatcher']->addListener(KernelEvents::RESPONSE, $callback, -999999);
$app['dispatcher']->addListener(KernelEvents::EXCEPTION, $callback, 999999);
$app['dispatcher']->addListener(PhraseaEvents::API_OAUTH2_START, $callback);
$app['dispatcher']->addListener(PhraseaEvents::API_OAUTH2_END, $callback);
$app['dispatcher']->addListener(PhraseaEvents::API_LOAD_END, $callback);
$app['dispatcher']->addListener(PhraseaEvents::API_RESULT, $callback);
}
public function boot(Application $app)
{
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,20 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
abstract class API_V1_exception_abstract extends Exception
{
protected static $details;
public static function get_details()
{
return static::$details;
}
}

View File

@@ -1,16 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class API_V1_exception_badrequest extends API_V1_exception_abstract
{
protected static $details = 'Parameter is invalid or missing';
}

View File

@@ -1,16 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class API_V1_exception_forbidden extends API_V1_exception_abstract
{
protected static $details = 'Access to the requested ressource is forbidden';
}

View File

@@ -1,16 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class API_V1_exception_internalservererror extends API_V1_exception_abstract
{
protected static $details = 'Internal Server Error';
}

View File

@@ -1,15 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class API_V1_exception_maintenance extends API_V1_exception_abstract
{
protected static $details = 'Server is offline for maintenance, try again soon.';
}

View File

@@ -1,16 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class API_V1_exception_methodnotallowed extends API_V1_exception_abstract
{
protected static $details = 'Attempting to use POST with a GET-only endpoint, or vice-versa';
}

View File

@@ -1,16 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class API_V1_exception_notfound extends API_V1_exception_abstract
{
protected static $details = 'Requested ressource is not found';
}

View File

@@ -1,16 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class API_V1_exception_unauthorized extends API_V1_exception_abstract
{
protected static $details = 'The OAuth token was provided but was invalid.';
}

View File

@@ -1,393 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Alchemy\Phrasea\Core\Event\ApiResultEvent;
use Alchemy\Phrasea\Core\PhraseaEvents;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class API_V1_result
{
protected $app;
/**
*
* @var string
*/
protected $api_version;
/**
*
* @var string
*/
protected $response_time;
/**
*
* @var int
*/
protected $http_code = 200;
/**
*
* @var string
*/
protected $error_type;
/**
*
* @var string
*/
protected $error_message;
/**
*
* @var string
*/
protected $error_details;
/**
*
* @var string
*/
protected $request;
/**
*
* @var mixed
*/
protected $response;
/**
*
* @var string
*/
protected $response_type;
/**
* Constant for responsetype json
*/
const FORMAT_JSON = 'json';
/**
* Constant for responsetype yaml
*/
const FORMAT_YAML = 'yaml';
/**
* Constant for responsetype jsonp
*/
const FORMAT_JSONP = 'jsonp';
const ERROR_BAD_REQUEST = 'Bad Request';
const ERROR_UNAUTHORIZED = 'Unauthorized';
const ERROR_FORBIDDEN = 'Forbidden';
const ERROR_NOTFOUND = 'Not Found';
const ERROR_MAINTENANCE = 'Service Temporarily Unavailable';
const ERROR_METHODNOTALLOWED = 'Method Not Allowed';
const ERROR_INTERNALSERVERERROR = 'Internal Server Error';
/**
* API v1 Result constructor
*
* @param Application $app
* @param Request $request
* @param API_V1_adapter $api
*
* @return API_V1_result
*/
public function __construct(Application $app, Request $request, API_V1_adapter $api)
{
$date = new DateTime();
$this->app = $app;
$this->request = $request;
$this->api_version = $api->get_version();
$this->response_time = $date->format(DATE_ATOM);
$this->response = new stdClass();
$this->parse_response_type();
return $this;
}
protected function parse_response_type()
{
if (trim($this->request->get('callback')) !== '') {
return $this->response_type = self::FORMAT_JSONP;
}
$accept = $this->request->getAcceptableContentTypes();
$response_types = [];
foreach ($accept as $key => $app_type) {
$response_types[strtolower($app_type)] = true;
}
if (array_key_exists('application/json', $response_types)) {
return $this->response_type = self::FORMAT_JSON;
}
if (array_key_exists('application/yaml', $response_types)) {
return $this->response_type = self::FORMAT_YAML;
}
if (array_key_exists('text/yaml', $response_types)) {
return $this->response_type = self::FORMAT_YAML;
}
return $this->response_type = self::FORMAT_JSON;
}
/**
* Set datas to the response
* If no datas provided (aka empty array), a stdClass if set,
* so the serialized datas will be objects
*
* @param array $datas
* @return API_V1_result
*/
public function set_datas(array $datas)
{
if (count($datas) === 0)
$datas = new stdClass ();
$this->response = $datas;
return $this;
}
/**
* Return response data
*
* @return array
*/
public function get_datas()
{
return (array) $this->response;
}
/**
* Format the data and return serialized string
*
* @return string
*/
public function format()
{
$request_uri = sprintf('%s %s'
, $this->request->getMethod()
, $this->request->getBasePath()
. $this->request->getPathInfo()
);
$ret = [
'meta' => [
'api_version' => $this->api_version
, 'request' => $request_uri
, 'response_time' => $this->response_time
, 'http_code' => $this->http_code
, 'error_type' => $this->error_type
, 'error_message' => $this->error_message
, 'error_details' => $this->error_details
, 'charset' => 'UTF-8'
]
, 'response' => $this->response
];
$this->app['dispatcher']->dispatch(PhraseaEvents::API_RESULT, new ApiResultEvent());
if ($this->app['conf']->get(['main', 'api-timers'], false)) {
$ret['timers'] = $this->app['api.timers']->toArray();
}
switch ($this->response_type) {
case self::FORMAT_JSON:
default:
$return_value = p4string::jsonencode($ret);
break;
case self::FORMAT_YAML:
if ($ret['response'] instanceof stdClass)
$ret['response'] = [];
$dumper = new Symfony\Component\Yaml\Dumper();
$return_value = $dumper->dump($ret, 8);
break;
case self::FORMAT_JSONP:
$callback = trim($this->request->get('callback'));
$return_value = $callback . '(' . p4string::jsonencode($ret) . ')';
break;
}
return $return_value;
}
/**
* Return serailized datas content type
*
* @return string
*/
public function get_content_type()
{
switch ($this->response_type) {
case self::FORMAT_JSON:
default:
$return_value = 'application/json';
break;
case self::FORMAT_YAML:
$return_value = 'application/yaml';
break;
case self::FORMAT_JSONP:
$return_value = 'text/javascript';
break;
}
return $return_value;
}
/**
* Set the API_V1_result http_code, error_type, error_message and error_details
* with the appropriate datas
*
* @param string $const
* @param string $message
*
* @return API_V1_result
*/
public function set_error_message($const, $message)
{
$this->error_details = $message;
switch ($const) {
case self::ERROR_BAD_REQUEST:
$this->http_code = 400;
$this->error_type = $const;
$this->error_message = API_V1_exception_badrequest::get_details();
break;
case self::ERROR_UNAUTHORIZED:
$this->http_code = 401;
$this->error_type = $const;
$this->error_message = API_V1_exception_unauthorized::get_details();
break;
case self::ERROR_FORBIDDEN:
$this->http_code = 403;
$this->error_type = $const;
$this->error_message = API_V1_exception_forbidden::get_details();
break;
case self::ERROR_NOTFOUND:
$this->http_code = 404;
$this->error_type = $const;
$this->error_message = API_V1_exception_notfound::get_details();
break;
case self::ERROR_METHODNOTALLOWED:
$this->http_code = 405;
$this->error_type = $const;
$this->error_message = API_V1_exception_methodnotallowed::get_details();
break;
case self::ERROR_INTERNALSERVERERROR:
$this->http_code = 500;
$this->error_type = $const;
$this->error_message = API_V1_exception_internalservererror::get_details();
break;
case self::ERROR_MAINTENANCE:
$this->http_code = 503;
$this->error_type = $const;
$this->error_message = API_V1_exception_maintenance::get_details();
break;
case OAUTH2_ERROR_INVALID_REQUEST:
$this->error_type = $const;
break;
}
return $this;
}
/**
* Set the API_V1_result http_code, error_message and error_details
* with the appropriate datas
*
* @param integer $code
*
* @return API_V1_result
*/
public function set_error_code($code)
{
switch ($code = (int) $code) {
case 400:
$this->http_code = $code;
$this->error_type = self::ERROR_BAD_REQUEST;
$this->error_message = API_V1_exception_badrequest::get_details();
break;
case 401:
$this->http_code = $code;
$this->error_type = self::ERROR_UNAUTHORIZED;
$this->error_message = API_V1_exception_unauthorized::get_details();
break;
case 403:
$this->http_code = $code;
$this->error_type = self::ERROR_FORBIDDEN;
$this->error_message = API_V1_exception_forbidden::get_details();
break;
case 404:
$this->http_code = $code;
$this->error_type = self::ERROR_NOTFOUND;
$this->error_message = API_V1_exception_notfound::get_details();
break;
case 405:
$this->http_code = $code;
$this->error_type = self::ERROR_METHODNOTALLOWED;
$this->error_message = API_V1_exception_methodnotallowed::get_details();
break;
case 500:
$this->http_code = $code;
$this->error_type = self::ERROR_INTERNALSERVERERROR;
$this->error_message = API_V1_exception_internalservererror::get_details();
break;
}
return $this;
}
/**
* Returns the correct http code depending on the errors
*
* @return int
*/
public function get_http_code()
{
if ($this->response_type == self::FORMAT_JSONP && $this->http_code != 500) {
return 200;
} else {
return $this->http_code;
}
}
/**
*
* @param int $code
*/
public function set_http_code($code)
{
$this->http_code = (int) $code;
}
/**
* Return a Symfony Response
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function get_response()
{
$response = new Response(
$this->format(),
$this->get_http_code(),
['Content-Type' => $this->get_content_type()]
);
$response->setCharset('UTF-8');
return $response;
}
}