From 540a82fe221974b3038dfee2dc41de05e820fd9e Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 2 Feb 2016 21:28:32 -0500 Subject: [PATCH] Minor code quality fixes for Scrutinizer --- public/js/anime_collection.js | 4 ++- src/Aviat/AnimeClient/Controller/Anime.php | 11 ++++-- src/Aviat/AnimeClient/Model/API.php | 13 +++++++ src/Aviat/AnimeClient/Model/Anime.php | 27 ++++++++++++++ src/Aviat/AnimeClient/Model/Manga.php | 42 ++++++++-------------- src/Aviat/AnimeClient/RoutingBase.php | 13 +++++-- src/Aviat/Ion/Enum/MessageType.php | 0 src/Aviat/Ion/Json.php | 4 +-- tests/mocks.php | 4 +++ 9 files changed, 82 insertions(+), 36 deletions(-) delete mode 100644 src/Aviat/Ion/Enum/MessageType.php diff --git a/public/js/anime_collection.js b/public/js/anime_collection.js index eecb382d..494c2e1c 100755 --- a/public/js/anime_collection.js +++ b/public/js/anime_collection.js @@ -20,7 +20,9 @@ $.get('/public/templates/anime-ajax-search-results.html', tempHtml => { $('#search').on('keypress', AnimeClient.throttle(250, function(e) { let query = encodeURIComponent($(this).val()); - console.log($(this).val()); + if (query === '') { + return; + } search(tempHtml, query); })); diff --git a/src/Aviat/AnimeClient/Controller/Anime.php b/src/Aviat/AnimeClient/Controller/Anime.php index c1084185..e1654982 100644 --- a/src/Aviat/AnimeClient/Controller/Anime.php +++ b/src/Aviat/AnimeClient/Controller/Anime.php @@ -244,8 +244,6 @@ class Anime extends BaseController { /** * Update an anime item - * - * @return boolean|null */ public function update() { @@ -253,6 +251,15 @@ class Anime extends BaseController { $this->outputJSON($response['body'], $response['statusCode']); } + /** + * Remove an anime from the list + */ + public function delete() + { + $response = $this->model->update($this->request->post->get()); + $this->outputJSON($response['body'], $response['statusCode']); + } + /** * View details of an anime * diff --git a/src/Aviat/AnimeClient/Model/API.php b/src/Aviat/AnimeClient/Model/API.php index a3c03cfc..4e8dc13d 100644 --- a/src/Aviat/AnimeClient/Model/API.php +++ b/src/Aviat/AnimeClient/Model/API.php @@ -178,5 +178,18 @@ class API extends BaseModel { return FALSE; } + + /** + * Dummy function that should be abstract. Is not abstract because + * this class is used concretely for authorizing API calls + * + * @TODO Refactor, and make this abstract + * @param string $status + * @return array + */ + protected function _get_list_from_api($status) + { + return []; + } } // End of BaseApiModel.php \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Model/Anime.php b/src/Aviat/AnimeClient/Model/Anime.php index cfead157..cd1cfb6c 100644 --- a/src/Aviat/AnimeClient/Model/Anime.php +++ b/src/Aviat/AnimeClient/Model/Anime.php @@ -74,6 +74,33 @@ class Anime extends API { ]; } + /** + * Remove an anime from a list + * + * @param array $data + * @return array + */ + public function delete($data) + { + $auth = $this->container->get('auth'); + if ( ! $auth->is_authenticated() || ! array_key_exists('id', $data)) + { + return FALSE; + } + + $id = $data['id']; + $data['auth_token'] = $auth->get_auth_token(); + + $response = $this->client->post("libraries/{$id}/remove", [ + 'form_params' => $data + ]); + + return [ + 'statusCode' => $response->getStatusCode(), + 'body' => Json::decode($response->getBody(), TRUE) + ]; + } + /** * Get the full set of anime lists * diff --git a/src/Aviat/AnimeClient/Model/Manga.php b/src/Aviat/AnimeClient/Model/Manga.php index 7f6f44fe..8705323e 100644 --- a/src/Aviat/AnimeClient/Model/Manga.php +++ b/src/Aviat/AnimeClient/Model/Manga.php @@ -50,7 +50,7 @@ class Manga extends API { */ protected $base_url = "https://hummingbird.me/"; - protected function _auth_json_call($type, $url, $json) + protected function _manga_api_call($type, $url, $json=null) { $token = $this->container->get('auth') ->get_auth_token(); @@ -65,10 +65,16 @@ class Manga extends API { ]); $cookieJar->setCookie($cookie_data); - $result = $this->client->request(strtoupper($type), $url, [ - 'cookies' => $cookieJar, - 'json' => $json - ]); + $config = [ + 'cookies' => $cookieJar + ]; + + if ( ! is_null($json)) + { + $config['json'] = $json; + } + + $result = $this->client->request(strtoupper($type), $url, $config); return [ 'statusCode' => $result->getStatusCode(), @@ -90,7 +96,7 @@ class Manga extends API { ] ]; - return $this->_auth_json_call('post', 'manga_library_entries', $object); + return $this->_manga_api_call('post', 'manga_library_entries', $object); } /** @@ -103,7 +109,7 @@ class Manga extends API { { $id = $data['id']; - return $this->_auth_json_call( + return $this->_manga_api_call( 'put', "manga_library_entries/{$id}", ['manga_library_entry' => $data] @@ -120,27 +126,7 @@ class Manga extends API { { $id = $data['id']; - $token = $this->container->get('auth') - ->get_auth_token(); - - // Set the token cookie, with the authentication token - // from the auth class. - $cookieJar = $this->cookieJar; - $cookie_data = new SetCookie([ - 'Name' => 'token', - 'Value' => $token, - 'Domain' => 'hummingbird.me' - ]); - $cookieJar->setCookie($cookie_data); - - $result = $this->delete("manga_library_entries/{$id}", [ - 'cookies' => $cookieJar, - ]); - - return [ - 'statusCode' => $result->getStatusCode(), - 'body' => $result->getBody() - ]; + return $this->_manga_api_call('delete', "manga_library_entries/{$id}"); } /** diff --git a/src/Aviat/AnimeClient/RoutingBase.php b/src/Aviat/AnimeClient/RoutingBase.php index b52ec8c0..e79c4e71 100644 --- a/src/Aviat/AnimeClient/RoutingBase.php +++ b/src/Aviat/AnimeClient/RoutingBase.php @@ -39,6 +39,12 @@ class RoutingBase { */ protected $routes; + /** + * Route configuration options + * @var array + */ + protected $route_config; + /** * Constructor * @@ -48,8 +54,9 @@ class RoutingBase { { $this->container = $container; $this->config = $container->get('config'); - $this->base_routes = $this->config->get('routes'); - $this->routes = $this->base_routes['routes']; + $base_routes = $this->config->get('routes'); + $this->routes = $base_routes['routes']; + $this->route_config = $base_routes['route_config']; } /** @@ -60,7 +67,7 @@ class RoutingBase { */ public function __get($key) { - $routing_config = $this->base_routes['route_config']; + $routing_config =& $this->route_config; if (array_key_exists($key, $routing_config)) { diff --git a/src/Aviat/Ion/Enum/MessageType.php b/src/Aviat/Ion/Enum/MessageType.php deleted file mode 100644 index e69de29b..00000000 diff --git a/src/Aviat/Ion/Json.php b/src/Aviat/Ion/Json.php index a74d9552..42370b19 100644 --- a/src/Aviat/Ion/Json.php +++ b/src/Aviat/Ion/Json.php @@ -23,8 +23,8 @@ class Json { * Encode data in json format * * @param mixed $data - * @param int $options=0 - * @param int $depth=512 + * @param int $options + * @param int $depth * @return string */ public static function encode($data, $options = 0, $depth = 512) diff --git a/tests/mocks.php b/tests/mocks.php index bc2510ba..b8649aab 100644 --- a/tests/mocks.php +++ b/tests/mocks.php @@ -144,6 +144,10 @@ class MockBaseApiModel extends BaseApiModel { use MockInjectionTrait; protected $base_url = 'https://httpbin.org/'; + protected function _get_list_from_api($status) + { + return []; + } } class TestAnimeModel extends AnimeModel {