HummingBirdAnimeClient/src/API/Kitsu/Auth.php

128 lines
2.5 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
* Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package AnimeListClient
* @author Timothy J. Warren <tim@timshomepage.net>
2017-01-11 10:30:53 -05:00
* @copyright 2015 - 2017 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://github.com/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu;
use Aviat\AnimeClient\AnimeClient;
2017-01-27 12:35:28 -05:00
use Aviat\AnimeClient\API\{
CacheTrait,
Kitsu as K
};
use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
2017-01-12 15:41:20 -05:00
use Exception;
/**
* Kitsu API Authentication
*/
class Auth {
2017-01-27 12:35:28 -05:00
use CacheTrait;
use ContainerAware;
/**
* Anime API Model
*
* @var \Aviat\AnimeClient\API\Kitsu\Model
*/
protected $model;
/**
* Session object
*
* @var Aura\Session\Segment
*/
protected $segment;
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
2017-01-27 12:35:28 -05:00
$this->setCache($container->get('cache'));
$this->segment = $container->get('session')
->getSegment(AnimeClient::SESSION_SEGMENT);
$this->model = $container->get('kitsu-model');
}
/**
* Make the appropriate authentication call,
* and save the resulting auth token if successful
*
* @param string $password
* @return boolean
*/
public function authenticate($password)
{
$config = $this->container->get('config');
$username = $config->get(['kitsu_username']);
2017-01-12 15:41:20 -05:00
try
{
2017-01-27 12:35:28 -05:00
$auth = $this->model->authenticate($username, $password);
2017-01-12 15:41:20 -05:00
}
catch (Exception $e)
{
return FALSE;
}
2017-01-27 12:35:28 -05:00
if (FALSE !== $auth)
{
2017-01-27 12:35:28 -05:00
// Set the token in the cache for command line operations
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_CACHE_KEY);
$cacheItem->set($auth['access_token']);
$cacheItem->save();
$this->segment->set('auth_token', $auth['access_token']);
return TRUE;
}
return FALSE;
}
/**
* Check whether the current user is authenticated
*
* @return boolean
*/
public function is_authenticated()
{
return ($this->get_auth_token() !== FALSE);
}
/**
* Clear authentication values
*
* @return void
*/
public function logout()
{
$this->segment->clear();
}
/**
* Retrieve the authentication token from the session
*
* @return string|false
*/
public function get_auth_token()
{
return $this->segment->get('auth_token', FALSE);
}
}
2016-12-20 12:55:43 -05:00
// End of KitsuAuth.php