1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-21 04:11:42 +02:00

refactor(admin-plugin): fix code standard for ApiController #347

This commit is contained in:
Awilum
2020-01-12 20:33:12 +03:00
parent 99484f57e4
commit f9ea4a780b

View File

@@ -4,12 +4,16 @@ declare(strict_types=1);
namespace Flextype;
use Ramsey\Uuid\Uuid;
use function Flextype\Component\I18n\__;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Session\Session;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Ramsey\Uuid\Uuid;
use function bin2hex;
use function date;
use function Flextype\Component\I18n\__;
use function random_bytes;
use function time;
class ApiController extends Controller
{
@@ -18,8 +22,6 @@ class ApiController extends Controller
*
* @param Request $request PSR7 request
* @param Response $response PSR7 response
*
* @return Response
*/
public function index(Request $request, Response $response) : Response
{
@@ -35,7 +37,7 @@ class ApiController extends Controller
'title' => __('admin_api'),
'attributes' => ['class' => 'navbar-item active'],
],
]
],
]
);
}
@@ -45,8 +47,6 @@ class ApiController extends Controller
*
* @param Request $request PSR7 request
* @param Response $response PSR7 response
*
* @return Response
*/
public function tokensIndex(Request $request, Response $response) : Response
{
@@ -67,14 +67,14 @@ class ApiController extends Controller
],
'api_tokens' => [
'link' => $this->router->pathFor('admin.api_tokens.index') . '?api=' . $api,
'title' => __('admin_'.$api),
'title' => __('admin_' . $api),
'attributes' => ['class' => 'navbar-item active'],
]
],
],
'buttons' => [
'api_tokens_add' => [
'link' => $this->router->pathFor('admin.api_tokens.add') . '?api=' . $api,
'title' => __('admin_create_new_'.$api.'_token'),
'title' => __('admin_create_new_' . $api . '_token'),
'attributes' => ['class' => 'float-right btn'],
],
],
@@ -87,8 +87,6 @@ class ApiController extends Controller
*
* @param Request $request PSR7 request
* @param Response $response PSR7 response
*
* @return Response
*/
public function add(Request $request, Response $response) : Response
{
@@ -108,14 +106,14 @@ class ApiController extends Controller
],
'api_tokens' => [
'link' => $this->router->pathFor('admin.api_tokens.index') . '?api=' . $api,
'title' => __('admin_'.$api),
'title' => __('admin_' . $api),
'attributes' => ['class' => 'navbar-item'],
],
'api_tokens_add' => [
'link' => $this->router->pathFor('admin.api_tokens.add') . '?api=' . $api,
'title' => __('admin_create_new_'.$api.'_token'),
'title' => __('admin_create_new_' . $api . '_token'),
'attributes' => ['class' => 'navbar-item active'],
]
],
],
]
);
@@ -126,8 +124,6 @@ class ApiController extends Controller
*
* @param Request $request PSR7 request
* @param Response $response PSR7 response
*
* @return Response
*/
public function addProcess(Request $request, Response $response) : Response
{
@@ -141,7 +137,6 @@ class ApiController extends Controller
$api_token_file_path = $api_token_dir_path . '/' . 'token.yaml';
if (! Filesystem::has($api_token_dir_path)) {
// Generate UUID
$uuid = Uuid::uuid4()->toString();
@@ -153,26 +148,26 @@ class ApiController extends Controller
// Create API Token account
if (Filesystem::write(
$api_token_file_path,
$this->parser->encode([
'title' => $post_data['title'],
'icon' => $post_data['icon'],
'limit_calls' => (int)$post_data['limit_calls'],
'limit_rate' => $post_data['limit_rate'],
'state' => $post_data['state'],
'uuid' => $uuid,
'created_by' => Session::get('uuid'),
'created_at' => $time,
'updated_by' => Session::get('uuid'),
'updated_at' => $time
], 'yaml')
)) {
$this->flash->addMessage('success', __('admin_message_'.$post_data['api'].'_api_token_created'));
$api_token_file_path,
$this->parser->encode([
'title' => $post_data['title'],
'icon' => $post_data['icon'],
'limit_calls' => (int) $post_data['limit_calls'],
'limit_rate' => $post_data['limit_rate'],
'state' => $post_data['state'],
'uuid' => $uuid,
'created_by' => Session::get('uuid'),
'created_at' => $time,
'updated_by' => Session::get('uuid'),
'updated_at' => $time,
], 'yaml')
)) {
$this->flash->addMessage('success', __('admin_message_' . $post_data['api'] . '_api_token_created'));
} else {
$this->flash->addMessage('error', __('admin_message_'.$post_data['api'].'_api_token_was_not_created'));
$this->flash->addMessage('error', __('admin_message_' . $post_data['api'] . '_api_token_was_not_created'));
}
} else {
$this->flash->addMessage('error', __('admin_message_'.$post_data['api'].'_api_token_was_not_created'));
$this->flash->addMessage('error', __('admin_message_' . $post_data['api'] . '_api_token_was_not_created'));
}
return $response->withRedirect($this->router->pathFor('admin.api_tokens.index') . '?api=' . $post_data['api']);
@@ -183,8 +178,6 @@ class ApiController extends Controller
*
* @param Request $request PSR7 request
* @param Response $response PSR7 response
*
* @return Response
*/
public function edit(Request $request, Response $response) : Response
{
@@ -208,14 +201,14 @@ class ApiController extends Controller
],
'api_tokens' => [
'link' => $this->router->pathFor('admin.api_tokens.index') . '?api=' . $api,
'title' => __('admin_'.$api),
'title' => __('admin_' . $api),
'attributes' => ['class' => 'navbar-item'],
],
'api_tokens_add' => [
'link' => $this->router->pathFor('admin.api_tokens.add') . '?api=' . $api,
'title' => __('admin_create_new_'.$api.'_token'),
'title' => __('admin_create_new_' . $api . '_token'),
'attributes' => ['class' => 'navbar-item active'],
]
],
],
]
);
@@ -226,8 +219,6 @@ class ApiController extends Controller
*
* @param Request $request PSR7 request
* @param Response $response PSR7 response
*
* @return Response
*/
public function editProcess(Request $request, Response $response) : Response
{
@@ -240,24 +231,24 @@ class ApiController extends Controller
// Update API Token File
if (Filesystem::has($api_token_file_path)) {
if (Filesystem::write(
$api_token_file_path,
$this->parser->encode([
'title' => $post_data['title'],
'icon' => $post_data['icon'],
'limit_calls' => (int)$post_data['limit_calls'],
'limit_rate' => $post_data['limit_rate'],
'state' => $post_data['state'],
'uuid' => $post_data['uuid'],
'created_by' => $post_data['created_by'],
'created_at' => $post_data['created_at'],
'updated_by' => Session::get('uuid'),
'updated_at' => date($this->registry->get('settings.date_format'), time())
], 'yaml')
)) {
$this->flash->addMessage('success', __('admin_message_'.$post_data['api'].'_api_token_updated'));
$api_token_file_path,
$this->parser->encode([
'title' => $post_data['title'],
'icon' => $post_data['icon'],
'limit_calls' => (int) $post_data['limit_calls'],
'limit_rate' => $post_data['limit_rate'],
'state' => $post_data['state'],
'uuid' => $post_data['uuid'],
'created_by' => $post_data['created_by'],
'created_at' => $post_data['created_at'],
'updated_by' => Session::get('uuid'),
'updated_at' => date($this->registry->get('settings.date_format'), time()),
], 'yaml')
)) {
$this->flash->addMessage('success', __('admin_message_' . $post_data['api'] . '_api_token_updated'));
}
} else {
$this->flash->addMessage('error', __('admin_message_'.$post_data['api'].'_api_token_was_not_updated'));
$this->flash->addMessage('error', __('admin_message_' . $post_data['api'] . '_api_token_was_not_updated'));
}
return $response->withRedirect($this->router->pathFor('admin.api_tokens.index') . '?api=' . $data['api']);
@@ -268,20 +259,18 @@ class ApiController extends Controller
*
* @param Request $request PSR7 request
* @param Response $response PSR7 response
*
* @return Response
*/
public function deleteProcess(Request $request, Response $response) : Response
{
// Get POST data
$post_data = $request->getParsedBody();
$api_token_dir_path = PATH['tokens'] . '/' . $post_data['api'] . '/' . $post_data['api_token'];
$api_token_dir_path = PATH['tokens'] . '/' . $post_data['api'] . '/' . $post_data['api_token'];
if (Filesystem::deleteDir($api_token_dir_path)) {
$this->flash->addMessage('success', __('admin_message_'.$post_data['api'].'_api_token_deleted'));
$this->flash->addMessage('success', __('admin_message_' . $post_data['api'] . '_api_token_deleted'));
} else {
$this->flash->addMessage('error', __('admin_message_'.$post_data['api'].'_api_token_was_not_deleted'));
$this->flash->addMessage('error', __('admin_message_' . $post_data['api'] . '_api_token_was_not_deleted'));
}
return $response->withRedirect($this->router->pathFor('admin.api_tokens.index') . '?api=' . $post_data['api']);