Code style fixes to satisfy phpcs

This commit is contained in:
Timothy Warren 2016-08-30 10:57:41 -04:00
parent 230c80459e
commit dd2d25d54c
21 changed files with 288 additions and 247 deletions

View File

@ -1,7 +1,7 @@
language: php language: php
install: install:
- composer install --no-dev - composer install
php: php:
- 5.5 - 5.5
@ -17,7 +17,7 @@ before_script:
script: script:
- mkdir -p build/logs - mkdir -p build/logs
- phpunit -c build - phpunit -c build
services: services:
- redis - redis

View File

@ -67,11 +67,11 @@ class LogicalOperatorAndSniff implements Sniff
$error_message = 'Logical operator should be in upper case;' $error_message = 'Logical operator should be in upper case;'
. ' use "' . strtoupper($operator_string) . ' use "' . strtoupper($operator_string)
. '" instead of "' . $operator_string . '"'; . '" instead of "' . $operator_string . '"';
$phpcsFile->addError($error_message, $stackPtr); $phpcsFile->addError($error_message, $stackPtr, 'LowercaseLogicalOperator');
} }
$warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"'; $warning_message = 'The symbolic form "&&" is preferred over the literal form "AND"';
$phpcsFile->addWarning($warning_message, $stackPtr); $phpcsFile->addWarning($warning_message, $stackPtr, 'UseOfLiteralAndOperator');
}//end process() }//end process()

View File

@ -72,7 +72,7 @@ class StrictComparisonOperatorSniff implements Sniff
$error_message = '"==" and "!=" are prohibited; use "' $error_message = '"==" and "!=" are prohibited; use "'
. self::$_replacements[$operator_code] . '" instead of "' . self::$_replacements[$operator_code] . '" instead of "'
. $operator_string . '".'; . $operator_string . '".';
$phpcsFile->addError($error_message, $stackPtr); $phpcsFile->addError($error_message, $stackPtr, 'NonStrictComparisonUsed');
}//end process() }//end process()

View File

@ -77,7 +77,7 @@ class DisallowSpaceIndentSniff implements Sniff
if (strpos($tokens[$stackPtr]['content'], " ") !== false) { if (strpos($tokens[$stackPtr]['content'], " ") !== false) {
$error = 'Tabs must be used to indent lines; spaces are not allowed for code indentation'; $error = 'Tabs must be used to indent lines; spaces are not allowed for code indentation';
$phpcsFile->addError($error, $stackPtr); $phpcsFile->addError($error, $stackPtr, 'SpacesUsedForIndentation');
} }
}//end process() }//end process()

View File

@ -8,7 +8,7 @@
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net> * @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2016 Timothy J. Warren * @copyright 2015 - 2016 Timothy J. Warren
* @link https://github.com/timw4mail/HummingBirdAnimeClient * @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 3.1 * @version 3.1
* @license MIT * @link https://github.com/timw4mail/HummingBirdAnimeClient
*/ */

View File

@ -25,6 +25,8 @@
<!-- One statement per line --> <!-- One statement per line -->
<rule ref="Generic.Formatting.DisallowMultipleStatements"/> <rule ref="Generic.Formatting.DisallowMultipleStatements"/>
<!-- Classes and functions should be commented --> <!-- Classes and functions should be commented -->
<rule ref="PEAR.Commenting.ClassComment"> <rule ref="PEAR.Commenting.ClassComment">
<exclude name="PEAR.Commenting.ClassComment.MissingCategoryTag" /> <exclude name="PEAR.Commenting.ClassComment.MissingCategoryTag" />
@ -37,9 +39,10 @@
<!-- Exclude this sniff because it doesn't understand multiple types --> <!-- Exclude this sniff because it doesn't understand multiple types -->
<exclude name="PEAR.Commenting.FunctionComment.MissingParamComment" /> <exclude name="PEAR.Commenting.FunctionComment.MissingParamComment" />
<exclude name="PEAR.Commenting.FunctionComment.SpacingAfterParamType" /> <exclude name="PEAR.Commenting.FunctionComment.SpacingAfterParamType" />
<exclude name="PEAR.Commenting.FunctionComment.SpacingAfterParamName" />
</rule> </rule>
<!-- Use warnings for docblock comments for files and variables, since nothing is cleary explained --> <!-- Use warnings for docblock comments for files and variables, since nothing is clearly explained -->
<rule ref="PEAR.Commenting.FileComment"> <rule ref="PEAR.Commenting.FileComment">
<exclude name="PEAR.Commenting.FileComment.InvalidVersion" /> <exclude name="PEAR.Commenting.FileComment.InvalidVersion" />
<exclude name="PEAR.Commenting.FileComment.MissingCategoryTag" /> <exclude name="PEAR.Commenting.FileComment.MissingCategoryTag" />
@ -58,7 +61,9 @@
<!-- Use Allman style indenting. With the exception of Class declarations, <!-- Use Allman style indenting. With the exception of Class declarations,
braces are always placed on a line by themselves, and indented at the same level as the control statement that "owns" them. --> braces are always placed on a line by themselves, and indented at the same level as the control statement that "owns" them. -->
<rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman"/> <rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman"/>
<rule ref="PEAR.WhiteSpace.ScopeClosingBrace"/> <rule ref="PEAR.WhiteSpace.ScopeClosingBrace">
<exclude name="PEAR.WhiteSpace.ScopeClosingBrace.BreakIndent" />
</rule>
<rule ref="Generic.Functions.FunctionCallArgumentSpacing"/> <rule ref="Generic.Functions.FunctionCallArgumentSpacing"/>
<!-- Use only short array syntax --> <!-- Use only short array syntax -->

View File

@ -2,9 +2,9 @@
$animeclient_file_patterns = [ $animeclient_file_patterns = [
'app/config/*.php', 'app/config/*.php',
'app/booststrap.php', 'app/bootstrap.php',
'src/functions.php', 'src/functions.php',
'src/Aviat/AnimeClient/*.php' 'src/*.php'
]; ];
if ( ! function_exists('glob_recursive')) if ( ! function_exists('glob_recursive'))

View File

@ -4,15 +4,17 @@
* *
* An API client for Hummingbird to manage anime and manga watch lists * An API client for Hummingbird to manage anime and manga watch lists
* *
* PHP version 5.6
*
* @package HummingbirdAnimeClient * @package HummingbirdAnimeClient
* @author Timothy J. Warren * @author Timothy J. Warren <tim@timshomepage.net>
* @copyright Copyright (c) 2015 - 2016 * @copyright 2015 - 2016 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 3.1
* @link https://github.com/timw4mail/HummingBirdAnimeClient * @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/ */
use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\AnimeClient;
use Whoops\Handler\PrettyPageHandler; use Whoops\Handler\PrettyPageHandler;
use Whoops\Handler\JsonResponseHandler;
// Work around the silly timezone error // Work around the silly timezone error
$timezone = ini_get('date.timezone'); $timezone = ini_get('date.timezone');

View File

@ -8,10 +8,11 @@
<target>phpdoc</target> <target>phpdoc</target>
</transformer> </transformer>
<transformations> <transformations>
<template name="clean" /> <template name="zend" />
</transformations> </transformations>
<files> <files>
<directory>src</directory> <directory>src</directory>
<directory>vendor/aviat/ion</directory> <directory>vendor/aviat/ion/src</directory>
<directory>vendor/container-interop/container-interop/src</directory>
</files> </files>
</phpdoc> </phpdoc>

View File

@ -22,14 +22,32 @@ use Aviat\AnimeClient\Util;
*/ */
class CacheImages extends BaseCommand { class CacheImages extends BaseCommand {
/**
* Manga Model
*
* @var Aviat\AnimeClient\Model\Manga
*/
protected $mangaModel; protected $mangaModel;
/**
* Anime Model
*
* @var Aviat\AnimeClient\Model\Anime
*/
protected $animeModel; protected $animeModel;
/**
* Miscellaneous helper methods
*
* @var Aviat\AnimeClient\Util
*/
protected $util; protected $util;
/* /**
* Convert manga images * Convert manga images
* *
* @throws \ConsoleKit\ConsoleException * @throws \ConsoleKit\ConsoleException
* @return void
*/ */
protected function getMangaImages() protected function getMangaImages()
{ {
@ -51,6 +69,7 @@ class CacheImages extends BaseCommand {
* Convert anime images * Convert anime images
* *
* @throws \ConsoleKit\ConsoleException * @throws \ConsoleKit\ConsoleException
* @return void
*/ */
protected function getAnimeImages() protected function getAnimeImages()
{ {

View File

@ -19,6 +19,7 @@ use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\View\HttpView; use Aviat\Ion\View\HttpView;
use Aviat\Ion\View\HtmlView; use Aviat\Ion\View\HtmlView;
use Aviat\Ion\View\JsonView; use Aviat\Ion\View\JsonView;
use Doctrine\Instantiator\Exception\InvalidArgumentException;
/** /**
* Controller base, defines output methods * Controller base, defines output methods
@ -60,7 +61,7 @@ class Controller {
protected $model; protected $model;
/** /**
* Url generatation class * Url generation class
* @var UrlGenerator * @var UrlGenerator
*/ */
protected $urlGenerator; protected $urlGenerator;
@ -117,6 +118,8 @@ class Controller {
/** /**
* Redirect to the default controller/url from an empty path * Redirect to the default controller/url from an empty path
*
* @return void
*/ */
public function redirect_to_default() public function redirect_to_default()
{ {
@ -151,7 +154,7 @@ class Controller {
} }
$util = $this->container->get('util'); $util = $this->container->get('util');
$double_form_page = $server_params['HTTP_REFERER'] == $this->request->getUri(); $double_form_page = $server_params['HTTP_REFERER'] === $this->request->getUri();
// Don't attempt to set the redirect url if // Don't attempt to set the redirect url if
// the page is one of the form type pages, // the page is one of the form type pages,
@ -214,6 +217,7 @@ class Controller {
* @param HtmlView $view * @param HtmlView $view
* @param string $template * @param string $template
* @param array $data * @param array $data
* @throws \InvalidArgumentException
* @return string * @return string
*/ */
protected function load_partial($view, $template, array $data = []) protected function load_partial($view, $template, array $data = [])
@ -273,7 +277,7 @@ class Controller {
$view = new HtmlView($this->container); $view = new HtmlView($this->container);
if ($status != "") if ($status !== "")
{ {
$message = $this->show_message($view, 'error', $status); $message = $this->show_message($view, 'error', $status);
} }

View File

@ -42,6 +42,7 @@ class Anime extends BaseController {
/** /**
* Data cache * Data cache
* @var Aviat\Ion\Cache\CacheInterface
*/ */
protected $cache; protected $cache;
@ -103,7 +104,7 @@ class Anime extends BaseController {
'list' => 'list' 'list' => 'list'
]; ];
$data = ($type != 'all') $data = ($type !== 'all')
? $this->cache->get($this->model, 'get_list', ['status' => $model_map[$type]]) ? $this->cache->get($this->model, 'get_list', ['status' => $model_map[$type]])
: $this->cache->get($this->model, 'get_all_lists', []); : $this->cache->get($this->model, 'get_all_lists', []);
@ -251,6 +252,8 @@ class Anime extends BaseController {
/** /**
* Update an anime item * Update an anime item
*
* @return void
*/ */
public function update() public function update()
{ {
@ -261,12 +264,14 @@ class Anime extends BaseController {
/** /**
* Remove an anime from the list * Remove an anime from the list
*
* @return void
*/ */
public function delete() public function delete()
{ {
$response = $this->model->delete($this->request->getParsedBody()); $response = $this->model->delete($this->request->getParsedBody());
if ($response['body'] == TRUE) if ((bool)$response['body'] === TRUE)
{ {
$this->set_flash_message("Successfully deleted anime.", 'success'); $this->set_flash_message("Successfully deleted anime.", 'success');
$this->cache->purge(); $this->cache->purge();

View File

@ -177,11 +177,11 @@ class Manga extends Controller {
* *
* @return void * @return void
*/ */
public function search() public function search()
{ {
$query_data = $this->request->getQueryParams(); $query_data = $this->request->getQueryParams();
$this->outputJSON($this->model->search($query_data['query'])); $this->outputJSON($this->model->search($query_data['query']));
} }
/** /**
* Update an anime item via a form submission * Update an anime item via a form submission
@ -200,7 +200,7 @@ class Manga extends Controller {
$result = Json::decode((string)$full_result['body']); $result = Json::decode((string)$full_result['body']);
if ($full_result['statusCode'] == 200) if ((int)$full_result['statusCode'] === 200)
{ {
$m =& $result['manga'][0]; $m =& $result['manga'][0];
$title = ( ! empty($m['english_title'])) $title = ( ! empty($m['english_title']))
@ -232,12 +232,14 @@ class Manga extends Controller {
/** /**
* Remove an manga from the list * Remove an manga from the list
*
* @return void
*/ */
public function delete() public function delete()
{ {
$response = $this->model->delete($this->request->getParsedBody()); $response = $this->model->delete($this->request->getParsedBody());
if ($response['body'] == TRUE) if ((bool)$response['body'] === TRUE)
{ {
$this->set_flash_message("Successfully deleted manga.", 'success'); $this->set_flash_message("Successfully deleted manga.", 'success');
$this->cache->purge(); $this->cache->purge();

View File

@ -138,6 +138,7 @@ class Dispatcher extends RoutingBase {
* the current route * the current route
* *
* @param \Aura\Router\Route $route * @param \Aura\Router\Route $route
* @throws \LogicException
* @return array * @return array
*/ */
protected function process_route($route) protected function process_route($route)

View File

@ -17,13 +17,9 @@ namespace Aviat\AnimeClient\Model;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Psr7\Request; use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Psr7\ResponseInterface;
use GuzzleHttp\Exception\ClientException;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Cache\CacheInterface;
use Aviat\Ion\Model; use Aviat\Ion\Model;
use Aviat\AnimeClient\AnimeClient; use Aviat\AnimeClient\AnimeClient;
@ -166,7 +162,7 @@ class API extends Model {
*/ */
protected function sort_by_name(&$array, $sort_key) protected function sort_by_name(&$array, $sort_key)
{ {
$sort = array(); $sort = [];
foreach ($array as $key => $item) foreach ($array as $key => $item)
{ {

View File

@ -59,7 +59,7 @@ class Anime extends API {
public function update($data) public function update($data)
{ {
$auth = $this->container->get('auth'); $auth = $this->container->get('auth');
if ( ! $auth->is_authenticated() || ! array_key_exists('id', $data)) if ( ! $auth->is_authenticated() OR ! array_key_exists('id', $data))
{ {
return FALSE; return FALSE;
} }
@ -86,7 +86,7 @@ class Anime extends API {
public function delete($data) public function delete($data)
{ {
$auth = $this->container->get('auth'); $auth = $this->container->get('auth');
if ( ! $auth->is_authenticated() || ! array_key_exists('id', $data)) if ( ! $auth->is_authenticated() OR ! array_key_exists('id', $data))
{ {
return FALSE; return FALSE;
} }
@ -176,7 +176,7 @@ class Anime extends API {
* *
* @param string $name * @param string $name
* @return array * @return array
* @throws RuntimeException * @throws \RuntimeException
*/ */
public function search($name) public function search($name)
{ {
@ -190,12 +190,12 @@ class Anime extends API {
$response = $this->get('search/anime', $config); $response = $this->get('search/anime', $config);
if ($response->getStatusCode() != 200) if ((int) $response->getStatusCode() !== 200)
{ {
$logger->warning("Non 200 response for search api call"); $logger->warning("Non 200 response for search api call");
$logger->warning($response->getBody()); $logger->warning($response->getBody());
throw new RuntimeException($response->getEffectiveUrl()); throw new \RuntimeException($response->getEffectiveUrl());
} }
return Json::decode($response->getBody(), TRUE); return Json::decode($response->getBody(), TRUE);
@ -214,7 +214,7 @@ class Anime extends API {
'allow_redirects' => FALSE 'allow_redirects' => FALSE
]; ];
if ($status != "all") if ($status !== "all")
{ {
$config['query']['status'] = $status; $config['query']['status'] = $status;
} }
@ -250,11 +250,6 @@ class Anime extends API {
]; ];
$username = $this->config->get('hummingbird_username'); $username = $this->config->get('hummingbird_username');
/*$auth = $this->container->get('auth');
if ($auth->is_authenticated())
{
$config['query']['auth_token'] = $auth->get_auth_token();
}*/
$response = $this->get("users/{$username}/library", $config); $response = $this->get("users/{$username}/library", $config);
return Json::decode($response->getBody(), TRUE); return Json::decode($response->getBody(), TRUE);
@ -264,7 +259,7 @@ class Anime extends API {
* Handle transforming of api data * Handle transforming of api data
* *
* @param string $status * @param string $status
* @param \GuzzleHttp\Message\Response * @param \GuzzleHttp\Message\Response $response
* @return array * @return array
*/ */
protected function transform($status, $response) protected function transform($status, $response)

View File

@ -218,7 +218,7 @@ class AnimeCollection extends Collection {
*/ */
private function json_import() private function json_import()
{ {
if ( ! file_exists('import.json') || ! $this->valid_database) if ( ! file_exists('import.json') OR ! $this->valid_database)
{ {
return; return;
} }

View File

@ -32,7 +32,7 @@ class Collection extends DB {
/** /**
* Whether the database is valid for querying * Whether the database is valid for querying
* @var bool * @var boolean
*/ */
protected $valid_database = FALSE; protected $valid_database = FALSE;

View File

@ -144,7 +144,7 @@ class Manga extends API {
* *
* @param string $name * @param string $name
* @return array * @return array
* @throws RuntimeException * @throws \RuntimeException
*/ */
public function search($name) public function search($name)
{ {
@ -160,7 +160,7 @@ class Manga extends API {
$response = $this->get('search.json', $config); $response = $this->get('search.json', $config);
if ($response->getStatusCode() != 200) if ((int) $response->getStatusCode() !== 200)
{ {
$logger->warning("Non 200 response for search api call"); $logger->warning("Non 200 response for search api call");
$logger->warning($response->getBody()); $logger->warning($response->getBody());

View File

@ -24,6 +24,7 @@ class UrlGenerator extends RoutingBase {
/** /**
* The current HTTP host * The current HTTP host
* @var string
*/ */
protected $host; protected $host;
@ -43,7 +44,7 @@ class UrlGenerator extends RoutingBase {
* *
* @return string * @return string
*/ */
public function asset_url(/*...*/) public function asset_url()
{ {
$args = func_get_args(); $args = func_get_args();
$base_url = rtrim($this->url(""), '/'); $base_url = rtrim($this->url(""), '/');
@ -106,6 +107,7 @@ class UrlGenerator extends RoutingBase {
* Full default path for the list pages * Full default path for the list pages
* *
* @param string $type * @param string $type
* @throws \InvalidArgumentException
* @return string * @return string
*/ */
public function default_url($type) public function default_url($type)

View File

@ -1,195 +1,204 @@
<?php <?php
/** /**
* Hummingbird Anime Client * Hummingbird Anime Client
* *
* An API client for Hummingbird to manage anime and manga watch lists * An API client for Hummingbird to manage anime and manga watch lists
* *
* @package HummingbirdAnimeClient * PHP version 5.6
* @author Timothy J. Warren *
* @copyright Copyright (c) 2015 - 2016 * @package HummingbirdAnimeClient
* @link https://github.com/timw4mail/HummingBirdAnimeClient * @author Timothy J. Warren <tim@timshomepage.net>
* @license MIT * @copyright 2015 - 2016 Timothy J. Warren
*/ * @license http://www.opensource.org/licenses/mit-license.html MIT License
namespace Aviat\AnimeClient; * @version 3.1
* @link https://github.com/timw4mail/HummingBirdAnimeClient
use abeautifulsite\SimpleImage; */
use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Di\ContainerInterface; namespace Aviat\AnimeClient;
/** use abeautifulsite\SimpleImage;
* Utility method class use Aviat\Ion\ConfigInterface;
*/ use Aviat\Ion\Di\ContainerInterface;
class Util {
/**
use \Aviat\Ion\Di\ContainerAware; * Utility method class
*/
private static $form_pages = [ class Util {
'edit',
'add', use \Aviat\Ion\Di\ContainerAware;
'update',
'update_form', /**
'login', * Routes that don't require a second navigation level
'logout', * @var array
'details' */
]; private static $form_pages = [
'edit',
/** 'add',
* The config manager 'update',
* @var ConfigInterface 'update_form',
*/ 'login',
private $config; 'logout',
'details'
/** ];
* Set up the Util class
* /**
* @param ContainerInterface $container * The config manager
*/ * @var ConfigInterface
public function __construct(ContainerInterface $container) */
{ private $config;
$this->setContainer($container);
$this->config = $container->get('config'); /**
} * Set up the Util class
*
/** * @param ContainerInterface $container
* HTML selection helper function */
* public function __construct(ContainerInterface $container)
* @param string $a - First item to compare {
* @param string $b - Second item to compare $this->setContainer($container);
* @return string $this->config = $container->get('config');
*/ }
public static function is_selected($a, $b)
{ /**
return ($a === $b) ? 'selected' : ''; * HTML selection helper function
} *
* @param string $a - First item to compare
/** * @param string $b - Second item to compare
* Inverse of selected helper function * @return string
* */
* @param string $a - First item to compare public static function is_selected($a, $b)
* @param string $b - Second item to compare {
* @return string return ($a === $b) ? 'selected' : '';
*/ }
public static function is_not_selected($a, $b)
{ /**
return ($a !== $b) ? 'selected' : ''; * Inverse of selected helper function
} *
* @param string $a - First item to compare
/** * @param string $b - Second item to compare
* Determine whether to show the sub-menu * @return string
* */
* @return bool public static function is_not_selected($a, $b)
*/ {
public function is_view_page() return ($a !== $b) ? 'selected' : '';
{ }
$url = $this->container->get('request')
->getUri(); /**
$page_segments = explode("/", $url); * Determine whether to show the sub-menu
*
$intersect = array_intersect($page_segments, self::$form_pages); * @return bool
*/
return empty($intersect); public function is_view_page()
} {
$url = $this->container->get('request')
/** ->getUri();
* Determine whether the page is a page with a form, and $page_segments = explode("/", $url);
* not suitable for redirection
* $intersect = array_intersect($page_segments, self::$form_pages);
* @return boolean
*/ return empty($intersect);
public function is_form_page() }
{
return ! $this->is_view_page(); /**
} * Determine whether the page is a page with a form, and
* not suitable for redirection
/** *
* Get the path of the cached version of the image. Create the cached image * @return boolean
* if the file does not already exist */
* public function is_form_page()
* @codeCoverageIgnore {
* @param string $api_path - The original image url return ! $this->is_view_page();
* @param string $series_slug - The part of the url with the series name, becomes the image name }
* @param string $type - Anime or Manga, controls cache path
* @return string - the frontend path for the cached image /**
* @throws DomainException * Get the path of the cached version of the image. Create the cached image
*/ * if the file does not already exist
public function get_cached_image($api_path, $series_slug, $type = "anime") *
{ * @codeCoverageIgnore
$path_parts = explode('?', basename($api_path)); * @param string $api_path - The original image url
$path = current($path_parts); * @param string $series_slug - The part of the url with the series name, becomes the image name
$ext_parts = explode('.', $path); * @param string $type - Anime or Manga, controls cache path
$ext = end($ext_parts); * @return string - the frontend path for the cached image
* @throws \DomainException
// Workaround for some broken file extensions */
if ($ext == "jjpg") public function get_cached_image($api_path, $series_slug, $type = "anime")
{ {
$ext = "jpg"; $path_parts = explode('?', basename($api_path));
} $path = current($path_parts);
$ext_parts = explode('.', $path);
// Failsafe for weird urls $ext = end($ext_parts);
if (strlen($ext) > 3)
{ // Workaround for some broken file extensions
return $api_path; if ($ext === "jjpg")
} {
$ext = "jpg";
$img_cache_path = $this->config->get('img_cache_path'); }
$cached_image = "{$series_slug}.{$ext}";
$cached_path = "{$img_cache_path}/{$type}/{$cached_image}"; // Failsafe for weird urls
if (strlen($ext) > 3)
// Cache the file if it doesn't already exist {
if ( ! file_exists($cached_path)) return $api_path;
{ }
if (function_exists('curl_init'))
{ $img_cache_path = $this->config->get('img_cache_path');
$ch = curl_init($api_path); $cached_image = "{$series_slug}.{$ext}";
$fp = fopen($cached_path, 'wb'); $cached_path = "{$img_cache_path}/{$type}/{$cached_image}";
curl_setopt_array($ch, [
CURLOPT_FILE => $fp, // Cache the file if it doesn't already exist
CURLOPT_HEADER => 0 if ( ! file_exists($cached_path))
]); {
curl_exec($ch); if (function_exists('curl_init'))
curl_close($ch); {
fclose($fp); $ch = curl_init($api_path);
} $fp = fopen($cached_path, 'wb');
else if (ini_get('allow_url_fopen')) curl_setopt_array($ch, [
{ CURLOPT_FILE => $fp,
copy($api_path, $cached_path); CURLOPT_HEADER => 0
} ]);
else curl_exec($ch);
{ curl_close($ch);
throw new \DomainException("Couldn't cache images because they couldn't be downloaded."); fclose($fp);
} }
else if (ini_get('allow_url_fopen'))
// Resize the image {
if ($type == 'anime') copy($api_path, $cached_path);
{ }
$resize_width = 220; else
$resize_height = 319; {
$this->_resize($cached_path, $resize_width, $resize_height); throw new \DomainException("Couldn't cache images because they couldn't be downloaded.");
} }
}
// Resize the image
return "/public/images/{$type}/{$cached_image}"; if ($type === 'anime')
} {
$resize_width = 220;
/** $resize_height = 319;
* Resize an image $this->_resize($cached_path, $resize_width, $resize_height);
* }
* @codeCoverageIgnore }
* @param string $path
* @param string $width return "/public/images/{$type}/{$cached_image}";
* @param string $height }
*/
private function _resize($path, $width, $height) /**
{ * Resize an image
try *
{ * @codeCoverageIgnore
$img = new SimpleImage($path); * @param string $path
$img->resize($width, $height)->save(); * @param string $width
} * @param string $height
catch (Exception $e) * @return void
{ */
// Catch image errors, since they don't otherwise affect private function _resize($path, $width, $height)
// functionality {
} try
} {
$img = new SimpleImage($path);
$img->resize($width, $height)->save();
}
catch (Exception $e)
{
// Catch image errors, since they don't otherwise affect
// functionality
}
}
} }