Update manga model to cache the one api response.

This commit is contained in:
Timothy Warren 2016-04-12 13:41:03 -04:00
parent a7063e9a49
commit 29041a4667
5 changed files with 16 additions and 1021 deletions

View File

@ -40,6 +40,9 @@ return function(array $config_array = []) {
$config = new Config($config_array); $config = new Config($config_array);
$container->set('config', $config); $container->set('config', $config);
// Create Cache Object
$container->set('cache', new CacheManager($container));
// Create Aura Router Object // Create Aura Router Object
$container->set('aura-router', new RouterContainer); $container->set('aura-router', new RouterContainer);
@ -80,7 +83,6 @@ return function(array $config_array = []) {
// Miscellaneous Classes // Miscellaneous Classes
$container->set('auth', new HummingbirdAuth($container)); $container->set('auth', new HummingbirdAuth($container));
$container->set('cache', new CacheManager($container));
$container->set('url-generator', new UrlGenerator($container)); $container->set('url-generator', new UrlGenerator($container));
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------

View File

@ -26,12 +26,6 @@ class Manga extends Controller {
use \Aviat\Ion\StringWrapper; use \Aviat\Ion\StringWrapper;
/**
* The cache manager
* @var \Aviat\Ion\Cache\CacheInterface
*/
protected $cache;
/** /**
* The manga model * The manga model
* @var object $model * @var object $model
@ -53,7 +47,6 @@ class Manga extends Controller {
{ {
parent::__construct($container); parent::__construct($container);
$this->cache = $container->get('cache');
$this->model = $container->get('manga-model'); $this->model = $container->get('manga-model');
$this->base_data = array_merge($this->base_data, [ $this->base_data = array_merge($this->base_data, [
'menu_name' => 'manga_list', 'menu_name' => 'manga_list',
@ -89,8 +82,8 @@ class Manga extends Controller {
]; ];
$data = ($status !== 'all') $data = ($status !== 'all')
? [$map[$status] => $this->cache->get($this->model, 'get_list', ['status' => $map[$status]]) ] ? [$map[$status] => $this->model->get_list($map[$status]) ]
: $this->cache->get($this->model, 'get_all_lists'); : $this->model->get_list('All');
$this->outputHTML('manga/' . $view_map[$view], [ $this->outputHTML('manga/' . $view_map[$view], [
'title' => $title, 'title' => $title,

View File

@ -168,23 +168,6 @@ class Manga extends API {
return Json::decode($response->getBody(), TRUE); return Json::decode($response->getBody(), TRUE);
} }
/**
* Get the full set of anime lists
*
* @return array
*/
public function get_all_lists()
{
$data = $this->_get_list_from_api();
foreach ($data as &$val)
{
$this->sort_by_name($val, 'manga');
}
return $data;
}
/** /**
* Get a category out of the full list * Get a category out of the full list
* *
@ -193,10 +176,8 @@ class Manga extends API {
*/ */
public function get_list($status) public function get_list($status)
{ {
$data = $this->_get_list_from_api($status); $data = $this->cache->get($this, '_get_list_from_api');
$this->sort_by_name($data, 'manga'); return ($status !== 'All') ? $data[$status] : $data;
return $data;
} }
/** /**
@ -205,7 +186,7 @@ class Manga extends API {
* @param string $status * @param string $status
* @return array * @return array
*/ */
protected function _get_list_from_api($status = "All") public function _get_list_from_api($status = "All")
{ {
$config = [ $config = [
'query' => [ 'query' => [
@ -216,11 +197,7 @@ class Manga extends API {
$response = $this->get('manga_library_entries', $config); $response = $this->get('manga_library_entries', $config);
$data = $this->transform($response); $data = $this->transform($response);
$output = $this->map_by_status($data); return $this->map_by_status($data);
return (array_key_exists($status, $output))
? $output[$status]
: $output;
} }
/** /**
@ -242,6 +219,7 @@ class Manga extends API {
$zippered_data = $this->zipper_lists($api_data); $zippered_data = $this->zipper_lists($api_data);
$transformer = new Transformer\MangaListTransformer(); $transformer = new Transformer\MangaListTransformer();
$transformed_data = $transformer->transform_collection($zippered_data); $transformed_data = $transformer->transform_collection($zippered_data);
return $transformed_data; return $transformed_data;
} }
@ -272,6 +250,11 @@ class Manga extends API {
$output[$key][] = $entry; $output[$key][] = $entry;
} }
foreach($output as &$val)
{
$this->sort_by_name($val, 'manga');
}
return $output; return $output;
} }

View File

@ -71,21 +71,4 @@ class MangaModelTest extends AnimeClient_TestCase {
$expected = Json::decodeFile($this->mockDir . '/get-all-lists.json'); $expected = Json::decodeFile($this->mockDir . '/get-all-lists.json');
$this->assertEquals($expected['Reading'], $this->model->get_list('Reading')); $this->assertEquals($expected['Reading'], $this->model->get_list('Reading'));
} }
public function testGetAllLists()
{
if (($var = getenv('CI')))
{
$this->markTestSkipped();
}
$data = file_get_contents($this->mockDir . '/manga.json');
$client = $this->getMockClient(200, [
'Content-type' => 'application/json'
], $data);
$this->model->__set('client', $client);
$expected = Json::decodeFile($this->mockDir . '/get-all-lists.json');
$this->assertEquals($expected, $this->model->get_all_lists());
}
} }

File diff suppressed because one or more lines are too long