HummingBirdAnimeClient/src/Controller.php

152 lines
3.7 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
2015-11-16 11:40:01 -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
*
2015-11-16 11:40:01 -05:00
* @package HummingbirdAnimeClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2017 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 4.0
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
2016-10-20 22:09:36 -04:00
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient;
use const Aviat\AnimeClient\SESSION_SEGMENT;
2017-02-22 14:46:35 -05:00
use function Aviat\AnimeClient\_dir;
2017-03-08 13:46:50 -05:00
use Aviat\AnimeClient\API\JsonAPI;
2016-12-20 12:58:37 -05:00
use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
use Aviat\Ion\View\{HtmlView, HttpView, JsonView};
2016-10-20 22:09:36 -04:00
use InvalidArgumentException;
2015-09-17 23:11:18 -04:00
/**
2015-09-17 23:11:18 -04:00
* Controller base, defines output methods
2015-10-06 10:44:33 -04:00
*
* @property Response object $response
*/
class Controller {
2017-03-08 16:21:01 -05:00
use ControllerTrait;
2015-06-26 16:39:10 -04:00
/**
* Constructor
*
* @param ContainerInterface $container
*/
2015-09-17 23:11:18 -04:00
public function __construct(ContainerInterface $container)
2015-05-22 12:36:26 -04:00
{
2015-09-17 23:11:18 -04:00
$this->setContainer($container);
2016-03-03 16:53:17 -05:00
$auraUrlGenerator = $container->get('aura-router')->getGenerator();
2015-09-17 23:11:18 -04:00
$urlGenerator = $container->get('url-generator');
2016-04-21 11:14:21 -04:00
$this->cache = $container->get('cache');
$this->config = $container->get('config');
$this->request = $container->get('request');
$this->response = $container->get('response');
2017-02-17 08:25:19 -05:00
$this->baseData = array_merge((array)$this->baseData, [
'url' => $auraUrlGenerator,
'urlGenerator' => $urlGenerator,
'auth' => $container->get('auth'),
'config' => $this->config
]);
2015-09-17 23:11:18 -04:00
$this->urlGenerator = $urlGenerator;
2016-01-04 10:53:03 -05:00
$session = $container->get('session');
$this->session = $session->getSegment(SESSION_SEGMENT);
2016-01-04 10:53:03 -05:00
// Set a 'previous' flash value for better redirects
2017-02-16 14:30:06 -05:00
$serverParams = $this->request->getServerParams();
if (array_key_exists('HTTP_REFERER', $serverParams))
{
2017-02-16 14:30:06 -05:00
$this->session->setFlash('previous', $serverParams['HTTP_REFERER']);
}
2016-01-04 10:53:03 -05:00
// Set a message box if available
2017-02-15 11:30:16 -05:00
$this->baseData['message'] = $this->session->getFlash('message');
2015-05-22 12:36:26 -04:00
}
2017-03-10 12:50:48 -05:00
/**
* Show the user profile page
*
* @return void
*/
2017-03-08 12:55:49 -05:00
public function me()
{
$username = $this->config->get(['kitsu_username']);
$model = $this->container->get('kitsu-model');
2017-03-08 13:46:50 -05:00
$data = $model->getUserData($username);
$included = JsonAPI::lightlyOrganizeIncludes($data['included']);
2017-03-08 16:21:01 -05:00
$relationships = JsonAPI::fillRelationshipsFromIncludes($data['data']['relationships'], $included);
2017-03-08 13:46:50 -05:00
$this->outputHTML('me', [
'title' => 'About' . $this->config->get('whose_list'),
'attributes' => $data['data']['attributes'],
2017-03-08 16:21:01 -05:00
'relationships' => $relationships,
2017-03-08 13:46:50 -05:00
'included' => $included
]);
2017-03-08 12:55:49 -05:00
}
2015-11-04 16:36:54 -05:00
/**
* Show the login form
*
* @param string $status
* @return void
*/
2016-10-20 22:09:36 -04:00
public function login(string $status = '')
2015-11-04 16:36:54 -05:00
{
2016-10-20 22:09:36 -04:00
$message = '';
2015-11-04 16:36:54 -05:00
$view = new HtmlView($this->container);
2016-10-20 22:09:36 -04:00
if ($status !== '')
2015-11-04 16:36:54 -05:00
{
2017-02-16 14:30:06 -05:00
$message = $this->showMessage($view, 'error', $status);
2015-11-04 16:36:54 -05:00
}
2016-01-04 10:53:03 -05:00
// Set the redirect url
2017-02-16 14:30:06 -05:00
$this->setSessionRedirect();
2016-01-04 10:53:03 -05:00
2015-11-04 16:36:54 -05:00
$this->outputHTML('login', [
'title' => 'Api login',
'message' => $message
], $view);
}
2015-11-13 11:33:27 -05:00
/**
* Attempt login authentication
*
2015-11-18 10:48:05 -05:00
* @return void
2015-11-13 11:33:27 -05:00
*/
2016-12-20 12:58:37 -05:00
public function loginAction()
2015-11-13 11:33:27 -05:00
{
$auth = $this->container->get('auth');
$post = $this->request->getParsedBody();
if ($auth->authenticate($post['password']))
2015-11-13 11:33:27 -05:00
{
2017-02-22 14:46:35 -05:00
$this->sessionRedirect();
return;
2015-11-13 11:33:27 -05:00
}
2017-02-16 14:30:06 -05:00
$this->setFlashMessage('Invalid username or password.');
2017-01-12 15:41:20 -05:00
$this->redirect($this->urlGenerator->url('login'), 303);
2015-11-13 11:33:27 -05:00
}
/**
* Deauthorize the current user
*
* @return void
*/
public function logout()
{
$auth = $this->container->get('auth');
$auth->logout();
$this->redirectToDefaultRoute();
2015-11-13 11:33:27 -05:00
}
}
2015-09-15 13:19:29 -04:00
// End of BaseController.php