Skip to content

Commit

Permalink
Use non-static methods in Config class (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
VerifiedJoseph authored Oct 17, 2023
1 parent 63678c9 commit d56f26f
Show file tree
Hide file tree
Showing 22 changed files with 395 additions and 188 deletions.
9 changes: 5 additions & 4 deletions cache-viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
require 'vendor/autoload.php';
require 'include/version.php';

use App\Configuration as Config;
use App\Config;
use App\Helper\Output;
use App\CacheViewer;

try {
Config::checkInstall();
Config::checkConfig();
$config = new Config();
$config->checkInstall();
$config->checkConfig();

$viewer = new CacheViewer();
$viewer = new CacheViewer($config);

} catch (Exception $e) {
Output::Error($e->getMessage());
Expand Down
9 changes: 5 additions & 4 deletions feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
require 'vendor/autoload.php';
require 'include/version.php';

use App\Configuration as Config;
use App\Config;
use App\Helper\Output;
use App\Feed;

try {
Config::checkInstall();
Config::checkConfig();
$config = new Config();
$config->checkInstall();
$config->checkConfig();

$feed = new Feed();
$feed = new Feed($config);
$feed->generate();
$feed->output();
} catch (Exception $e) {
Expand Down
25 changes: 18 additions & 7 deletions include/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@
namespace App;

use Curl\Curl;
use App\Configuration as Config;
use App\Config;
use App\Helper\Url;
use stdClass;
use Exception;

class Api
{
/** @var Config Config class instance */
private Config $config;

/** @var array<int, int> $expectedStatusCodes Non-error HTTP status codes returned by the API */
private array $expectedStatusCodes = [200, 304];

/**
* @param Config $config Config class instance
*/
public function __construct(Config $config)
{
$this->config = $config;
}

/**
* Get channel or playlist details
*
Expand All @@ -25,7 +36,7 @@ class Api
*/
public function getDetails(string $type, string $parameter, string $eTag)
{
$url = Url::getApi($type, $parameter, (string) Config::get('YOUTUBE_API_KEY'));
$url = Url::getApi($type, $parameter, $this->config->getApiKey());
$response = $this->fetch($url, $eTag);

if ($response['statusCode'] === 200 && empty($response['data']->items)) {
Expand All @@ -43,7 +54,7 @@ public function getDetails(string $type, string $parameter, string $eTag)
*/
public function getVideos(string $parameter)
{
$url = Url::getApi('videos', $parameter, (string) Config::get('YOUTUBE_API_KEY'));
$url = Url::getApi('videos', $parameter, $this->config->getApiKey());
$response = $this->fetch($url);

return $response['data'];
Expand All @@ -57,7 +68,7 @@ public function getVideos(string $parameter)
*/
public function searchChannels(string $parameter): object
{
$url = Url::getApi('searchChannels', $parameter, (string) Config::get('YOUTUBE_API_KEY'));
$url = Url::getApi('searchChannels', $parameter, $this->config->getApiKey());
$response = $this->fetch($url);

return $response['data'];
Expand All @@ -71,7 +82,7 @@ public function searchChannels(string $parameter): object
*/
public function searchPlaylists(string $parameter): object
{
$url = Url::getApi('searchPlaylists', $parameter, (string) Config::get('YOUTUBE_API_KEY'));
$url = Url::getApi('searchPlaylists', $parameter, $this->config->getApiKey());
$response = $this->fetch($url);

return $response['data'];
Expand All @@ -95,7 +106,7 @@ private function fetch(string $url, string $eTag = ''): array
$curl->setHeader('If-None-Match', $eTag);
}

$curl->setUserAgent(Config::getUserAgent());
$curl->setUserAgent($this->config->getUserAgent());
$curl->get($url);

if ($curl->getCurlErrorCode() !== 0) {
Expand Down Expand Up @@ -123,7 +134,7 @@ private function handleError(stdClass $response): void
{
$error = $response->error->errors[0];

if (Config::get('RAW_API_ERRORS') === true) {
if ($this->config->get('RAW_API_ERRORS') === true) {
$raw = json_encode($response->error, JSON_PRETTY_PRINT);

throw new Exception(
Expand Down
19 changes: 11 additions & 8 deletions include/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace App;

use App\Configuration as Config;
use App\Config;
use App\Helper\File;

class Cache
{
/** @var Config Config class instance */
private Config $config;

/** @var string $name Cache filename */
private string $name = '';

Expand All @@ -17,12 +20,12 @@ class Cache
private string $path = '';

/**
* Constructor
*
* @param string $feedId Feed ID
* @param Config $config Config class instance
*/
public function __construct(string $feedId)
public function __construct(string $feedId, Config $config)
{
$this->config = $config;
$this->setName($feedId);
$this->setPath();
}
Expand All @@ -42,7 +45,7 @@ public function getData(): array
*/
public function load(): void
{
if (Config::get('DISABLE_CACHE') === false) {
if ($this->config->getCacheDisableStatus() === false) {
$contents = File::read($this->path);
$decoded = json_decode($contents, true);

Expand All @@ -59,7 +62,7 @@ public function load(): void
*/
public function save(array $data = []): void
{
if (Config::get('DISABLE_CACHE') === false) {
if ($this->config->getCacheDisableStatus() === false) {
$data = (string) json_encode($data);
File::write($this->path, $data);
}
Expand All @@ -80,7 +83,7 @@ private function setName(string $feedId): void
*/
private function setPath(): void
{
$this->path = Config::getCacheDirPath() . DIRECTORY_SEPARATOR .
$this->name . '.' . Config::getCacheFileExtension();
$this->path = $this->config->getCacheDirPath() . DIRECTORY_SEPARATOR .
$this->name . '.' . $this->config->getCacheFileExtension();
}
}
87 changes: 69 additions & 18 deletions include/CacheViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App;

use App\Configuration as Config;
use App\Config;
use App\Template;
use App\Helper\File;
use App\Helper\Convert;
Expand All @@ -11,6 +11,9 @@

class CacheViewer
{
/** @var Config Config class instance */
private Config $config;

/** @var string $cacheId Current cache file ID */
private string $cacheId = '';

Expand All @@ -24,18 +27,20 @@ class CacheViewer
private int $cacheSize = 0;

/**
* Constructor
* @param Config $config Config class instance
*
* @throws Exception if ENABLE_CACHE_VIEWER is false
* @throws Exception if DISABLE_CACHE is true
*/
public function __construct()
public function __construct(Config $config)
{
if (Config::get('ENABLE_CACHE_VIEWER') === false) {
$this->config = $config;

if ($this->config->get('ENABLE_CACHE_VIEWER') === false) {
throw new Exception('Cache viewer is disabled.');
}

if (Config::get('DISABLE_CACHE') === true) {
if ($this->config->getCacheDisableStatus() === true) {
throw new Exception('Cache viewer not available. Cache is disabled.');
}

Expand Down Expand Up @@ -73,9 +78,9 @@ private function checkInputs(): void
*/
private function loadFiles(): void
{
$regex = '/.' . preg_quote(Config::getCacheFileExtension()) . '$/';
$regex = '/.' . preg_quote($this->config->getCacheFileExtension()) . '$/';

$cacheDirectory = new \RecursiveDirectoryIterator(Config::getCacheDirPath());
$cacheDirectory = new \RecursiveDirectoryIterator($this->config->getCacheDirPath());
$cacheFiles = new \RegexIterator($cacheDirectory, $regex);

foreach ($cacheFiles as $file) {
Expand All @@ -87,7 +92,7 @@ private function loadFiles(): void
}

$this->data[] = array(
'id' => $file->getBasename('.' . Config::getCacheFileExtension()),
'id' => $file->getBasename('.' . $this->config->getCacheFileExtension()),
'modified' => $file->getMTime(),
'size' => $file->getSize(),
'contents' => $data
Expand Down Expand Up @@ -119,12 +124,26 @@ private function display(): void
foreach ($this->data as $index => $data) {
$number = $index + 1;

$modified = Convert::unixTime($data['modified']);
$modified = Convert::unixTime(
$data['modified'],
'Y-m-d H:i:s',
$this->config->getTimezone()
);
$size = Convert::fileSize($data['size']);

$xmlUrl = Url::getFeed($data['contents']['details']['type'], $data['contents']['details']['id'], 'rss');
$htmlUrl = Url::getFeed($data['contents']['details']['type'], $data['contents']['details']['id'], 'html');
$xmlUrl = Url::getFeed(
$this->config->getSelfUrl(),
$data['contents']['details']['type'],
$data['contents']['details']['id'],
'rss'
);

$htmlUrl = Url::getFeed(
$this->config->getSelfUrl(),
$data['contents']['details']['type'],
$data['contents']['details']['id'],
'html'
);
$tbody .= <<<HTML
<tr class="center">
<td id="{$data['id']}">$number</td>
Expand Down Expand Up @@ -223,8 +242,17 @@ private function displayFileDetails(array $data): string
*/
private function displayChannel(array $channel): string
{
$fetched = Convert::unixTime($channel['fetched']);
$expires = Convert::unixTime($channel['expires']);
$fetched = Convert::unixTime(
$channel['fetched'],
'Y-m-d H:i:s',
$this->config->getTimezone()
);

$expires = Convert::unixTime(
$channel['expires'],
'Y-m-d H:i:s',
$this->config->getTimezone()
);

$html = <<<HTML
<strong>Details:</strong>
Expand Down Expand Up @@ -262,8 +290,17 @@ private function displayFeed(array $feed): string
{
$videoIDs = implode(' ', $feed['videos']);

$fetched = Convert::unixTime($feed['fetched']);
$expires = Convert::unixTime($feed['expires']);
$fetched = Convert::unixTime(
$feed['fetched'],
'Y-m-d H:i:s',
$this->config->getTimezone()
);

$expires = Convert::unixTime(
$feed['expires'],
'Y-m-d H:i:s',
$this->config->getTimezone()
);

$html = <<<HTML
<strong>Feed:</strong>
Expand Down Expand Up @@ -297,9 +334,23 @@ private function displayVideos(array $videos): string
$tags = implode(', ', $video['tags']);
$tagCount = count($video['tags']);

$fetched = Convert::unixTime($video['fetched']);
$expires = Convert::unixTime($video['expires']);
$published = Convert::unixTime($video['published']);
$fetched = Convert::unixTime(
$video['fetched'],
'Y-m-d H:i:s',
$this->config->getTimezone()
);

$expires = Convert::unixTime(
$video['expires'],
'Y-m-d H:i:s',
$this->config->getTimezone()
);

$published = Convert::unixTime(
$video['published'],
'Y-m-d H:i:s',
$this->config->getTimezone()
);

$videoHtml .= <<<HTML
<tr>
Expand Down
Loading

0 comments on commit d56f26f

Please sign in to comment.