Refactor KitsuModel, add more docblocks

This commit is contained in:
Timothy Warren 2017-01-26 13:03:38 -05:00
parent ece4f343e2
commit 0ae52a13f3
3 changed files with 74 additions and 17 deletions

View File

@ -22,7 +22,7 @@ use Aura\Session\SessionFactory;
use Aviat\AnimeClient\API\Kitsu\{ use Aviat\AnimeClient\API\Kitsu\{
Auth as KitsuAuth, Auth as KitsuAuth,
ListItem as KitsuListItem, ListItem as KitsuListItem,
KitsuModel Model as KitsuModel
}; };
use Aviat\AnimeClient\API\MAL\{ use Aviat\AnimeClient\API\MAL\{
ListItem as MALListItem, ListItem as MALListItem,

View File

@ -29,7 +29,7 @@ use GuzzleHttp\Exception\ClientException;
/** /**
* Kitsu API Model * Kitsu API Model
*/ */
class KitsuModel { class Model {
use CacheTrait; use CacheTrait;
use ContainerAware; use ContainerAware;
use KitsuTrait; use KitsuTrait;
@ -65,7 +65,7 @@ class KitsuModel {
/** /**
* KitsuModel constructor. * Constructor.
*/ */
public function __construct(ListItem $listItem) public function __construct(ListItem $listItem)
{ {
@ -87,15 +87,23 @@ class KitsuModel {
*/ */
public function getUserIdByUsername(string $username) public function getUserIdByUsername(string $username)
{ {
$data = $this->getRequest('users', [ $cacheItem = $this->cache->getItem($this->getHashForMethodCall($this, __METHOD__, $username));
'query' => [
'filter' => [ if ( ! $cacheItem->isHit())
'name' => $username {
$data = $this->getRequest('users', [
'query' => [
'filter' => [
'name' => $username
]
] ]
] ]);
]);
return $data['data'][0]['id']; $cacheItem->set($data['data'][0]['id']);
$cacheItem->save();
}
return $cacheItem->get();
} }
/** /**
@ -139,6 +147,12 @@ class KitsuModel {
return $this->animeTransformer->transform($baseData); return $this->animeTransformer->transform($baseData);
} }
/**
* Get information about a particular anime
*
* @param string $animeId
* @return array
*/
public function getAnimeById(string $animeId): array public function getAnimeById(string $animeId): array
{ {
$baseData = $this->getRawMediaDataById('anime', $animeId); $baseData = $this->getRawMediaDataById('anime', $animeId);
@ -161,9 +175,11 @@ class KitsuModel {
* Get the anime list for the configured user * Get the anime list for the configured user
* *
* @param string $status - The watching status to filter the list with * @param string $status - The watching status to filter the list with
* @param int $limit - The number of list entries to fetch for a page
* @param int $offset - The page offset
* @return array * @return array
*/ */
public function getAnimeList(string $status): array public function getAnimeList(string $status, int $limit = 600, int $offset = 0): array
{ {
$options = [ $options = [
'query' => [ 'query' => [
@ -174,8 +190,8 @@ class KitsuModel {
], ],
'include' => 'media,media.genres,media.mappings,anime.streamingLinks', 'include' => 'media,media.genres,media.mappings,anime.streamingLinks',
'page' => [ 'page' => [
'offset' => 0, 'offset' => $offset,
'limit' => 600 'limit' => $limit
] ]
] ]
]; ];
@ -201,7 +217,15 @@ class KitsuModel {
return $cacheItem->get(); return $cacheItem->get();
} }
public function getMangaList($status): array /**
* Get the manga list for the configured user
*
* @param string $status - The reading status by which to filter the list
* @param int $limit - The number of list items to fetch per page
* @param int $offset - The page offset
* @return array
*/
public function getMangaList(string $status, int $limit = 200, int $offset = 0): array
{ {
$options = [ $options = [
'query' => [ 'query' => [
@ -212,8 +236,8 @@ class KitsuModel {
], ],
'include' => 'media', 'include' => 'media',
'page' => [ 'page' => [
'offset' => 0, 'offset' => $offset,
'limit' => 200 'limit' => $limit
], ],
'sort' => '-updated_at' 'sort' => '-updated_at'
] ]
@ -239,6 +263,13 @@ class KitsuModel {
return $cacheItem->get(); return $cacheItem->get();
} }
/**
* Search for an anime or manga
*
* @param string $type - 'anime' or 'manga'
* @param string $query - name of the item to search for
* @return array
*/
public function search(string $type, string $query): array public function search(string $type, string $query): array
{ {
$options = [ $options = [
@ -264,12 +295,24 @@ class KitsuModel {
return $raw; return $raw;
} }
/**
* Create a list item
*
* @param array $data
* @return bool
*/
public function createListItem(array $data): bool public function createListItem(array $data): bool
{ {
$data['user_id'] = $this->getUserIdByUsername($this->getUsername()); $data['user_id'] = $this->getUserIdByUsername($this->getUsername());
return $this->listItem->create($data); return $this->listItem->create($data);
} }
/**
* Get the data for a specific list item, generally for editing
*
* @param string $listId - The unique identifier of that list item
* @return array
*/
public function getListItem(string $listId): array public function getListItem(string $listId): array
{ {
$baseData = $this->listItem->get($listId); $baseData = $this->listItem->get($listId);
@ -294,6 +337,12 @@ class KitsuModel {
} }
} }
/**
* Modify a list item
*
* @param array $data
* @return array
*/
public function updateListItem(array $data) public function updateListItem(array $data)
{ {
try try
@ -313,6 +362,12 @@ class KitsuModel {
} }
} }
/**
* Remove a list item
*
* @param string $id - The id of the list item to remove
* @return bool
*/
public function deleteListItem(string $id): bool public function deleteListItem(string $id): bool
{ {
return $this->listItem->delete($id); return $this->listItem->delete($id);

View File

@ -61,7 +61,9 @@ class AnimeListTransformer extends AbstractTransformer {
} }
} }
$streamingLinks = Kitsu::parseListItemStreamingLinks($included, $animeId); $streamingLinks = (array_key_exists('streamingLinks', $anime['relationships']))
? Kitsu::parseListItemStreamingLinks($included, $animeId)
: [];
return [ return [
'id' => $item['id'], 'id' => $item['id'],