HummingBirdAnimeClient/src/Model/API.php

202 lines
4.2 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
/**
2016-12-20 12:58:37 -05:00
* Anime List Client
2015-11-16 11:40:01 -05:00
*
2016-12-20 12:58:37 -05:00
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
2015-11-16 11:40:01 -05:00
*
2016-10-20 22:09:36 -04:00
* PHP version 7
2016-08-30 10:01:18 -04:00
*
2016-12-20 12:58:37 -05:00
* @package AnimeListClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2016-12-20 12:58:37 -05:00
* @version 4.0
2015-11-16 11:40:01 -05:00
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
2016-10-20 22:09:36 -04:00
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient\Model;
2016-10-20 22:32:17 -04:00
use Aviat\AnimeClient\AnimeClient;
2016-12-20 12:58:37 -05:00
use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
2016-10-20 22:32:17 -04:00
use Aviat\Ion\Model;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
2016-08-30 10:57:41 -04:00
use Psr\Http\Message\ResponseInterface;
/**
* Base model for api interaction
2015-10-15 09:25:30 -04:00
*
* @method ResponseInterface get(string $uri, array $options);
* @method ResponseInterface delete(string $uri, array $options);
* @method ResponseInterface head(string $uri, array $options);
* @method ResponseInterface options(string $uri, array $options);
* @method ResponseInterface patch(string $uri, array $options);
* @method ResponseInterface post(string $uri, array $options);
* @method ResponseInterface put(string $uri, array $options);
*/
class API extends Model {
2016-12-20 12:58:37 -05:00
use ContainerAware;
/**
* Config manager
* @var ConfigInterface
*/
protected $config;
/**
* Base url for making api requests
* @var string
*/
protected $base_url = '';
/**
* The Guzzle http client object
* @var object
*/
protected $client;
/**
* Cookie jar object for api requests
* @var object
*/
protected $cookieJar;
2016-04-12 13:41:50 -04:00
/**
* Cache manager
* @var \Aviat\Ion\Cache\CacheInterface
*/
protected $cache;
2016-12-20 12:58:37 -05:00
/**
* Default settings for Guzzle
* @var array
*/
protected $connectionDefaults = [];
/**
* Constructor
*
* @param ContainerInterface $container
*/
2015-09-17 23:11:18 -04:00
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->config = $container->get('config');
2016-04-12 13:41:50 -04:00
$this->cache = $container->get('cache');
2015-12-08 16:39:49 -05:00
$this->init();
}
/**
* Set up the class properties
*
* @return void
*/
protected function init()
{
$this->cookieJar = new CookieJar();
$this->client = new Client([
'base_uri' => $this->base_url,
'cookies' => TRUE,
2015-10-19 12:50:46 -04:00
'http_errors' => FALSE,
2016-12-20 12:58:37 -05:00
'defaults' => array_merge([
'cookies' => $this->cookieJar,
'headers' => [
2016-12-20 12:58:37 -05:00
'User-Agent' => "Tim's Anime Client/4.0",
'Accept-Encoding' => 'application/vnd.api+json',
'Content-Type' => 'application/vnd.api+json'
],
2015-11-11 14:53:09 -05:00
'timeout' => 25,
'connect_timeout' => 25
2016-12-20 12:58:37 -05:00
], $this->connectionDefaults)
]);
}
2015-10-15 09:25:30 -04:00
/**
* Magic methods to call guzzle api client
*
* @param string $method
* @param array $args
* @return ResponseInterface|null
*/
public function __call($method, $args)
{
$valid_methods = [
'get',
2016-12-20 12:58:37 -05:00
'getAsync',
2015-10-15 09:25:30 -04:00
'delete',
2016-12-20 12:58:37 -05:00
'deleteAsync',
2015-10-15 09:25:30 -04:00
'head',
2016-12-20 12:58:37 -05:00
'headAsync',
2015-10-15 09:25:30 -04:00
'options',
2016-12-20 12:58:37 -05:00
'optionsAsync',
2015-10-15 09:25:30 -04:00
'patch',
2016-12-20 12:58:37 -05:00
'patchAsync',
2015-10-15 09:25:30 -04:00
'post',
2016-12-20 12:58:37 -05:00
'postAsync',
'put',
'putAsync'
2015-10-15 09:25:30 -04:00
];
if ( ! in_array($method, $valid_methods))
{
return NULL;
}
array_unshift($args, strtoupper($method));
2015-11-05 11:26:03 -05:00
return call_user_func_array([$this->client, 'request'], $args);
2015-10-15 09:25:30 -04:00
}
/**
* Get the data for the specified library entry
*
* @param string $id
* @param string $status
* @return array
*/
public function get_library_item($id, $status)
{
$data = $this->_get_list_from_api($status);
$index_array = array_column($data, 'id');
$key = array_search($id, $index_array);
return $key !== FALSE
? $data[$key]
: [];
}
/**
* Sort the manga entries by their title
*
* @codeCoverageIgnore
* @param array $array
* @param string $sort_key
* @return void
*/
2016-10-20 22:09:36 -04:00
protected function sort_by_name(array &$array, string $sort_key)
{
2016-08-30 10:57:41 -04:00
$sort = [];
foreach ($array as $key => $item)
{
$sort[$key] = $item[$sort_key]['title'];
}
array_multisort($sort, SORT_ASC, $array);
}
/**
* 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
*/
2016-10-20 22:09:36 -04:00
protected function _get_list_from_api(string $status): array
{
return [];
}
}
// End of BaseApiModel.php