HummingBirdAnimeClient/src/AnimeClient/Model/Collection.php

86 lines
1.8 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2020-12-10 17:06:50 -05:00
* PHP version 7.4+
2016-08-30 10:01:18 -04:00
*
2017-02-15 16:13:32 -05:00
* @package HummingbirdAnimeClient
2016-08-30 10:01:18 -04:00
* @author Timothy J. Warren <tim@timshomepage.net>
2020-01-08 15:39:49 -05:00
* @copyright 2015 - 2020 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Model;
use Aviat\Ion\Di\ContainerInterface;
use PDOException;
2020-04-21 19:22:56 -04:00
use Query\QueryBuilderInterface;
use function Query;
/**
* Base model for anime and manga collections
*/
class Collection extends DB {
2019-01-07 14:29:15 -05:00
/**
* The query builder object
2020-04-21 19:22:56 -04:00
* @var QueryBuilderInterface
2019-01-07 14:29:15 -05:00
*/
2020-04-21 19:22:56 -04:00
protected QueryBuilderInterface $db;
2019-01-07 14:29:15 -05:00
/**
* Whether the database is valid for querying
2016-08-30 10:57:41 -04:00
* @var boolean
*/
2020-04-10 20:01:46 -04:00
protected bool $validDatabase = FALSE;
/**
* Create a new collection object
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
2017-02-15 15:35:41 -05:00
parent::__construct($container);
try
{
$this->db = Query($this->dbConfig);
2019-01-07 14:29:15 -05:00
$this->validDatabase = TRUE;
}
2019-12-09 16:17:25 -05:00
catch (PDOException $e)
{
$this->db = Query([
'type' => 'sqlite',
'file' => ':memory:',
]);
}
// Is database valid? If not, set a flag so the
// app can be run without a valid database
2018-10-05 14:32:05 -04:00
if ($this->dbConfig['type'] === 'sqlite')
{
2018-10-05 14:32:05 -04:00
$dbFileName = $this->dbConfig['file'];
2017-02-15 15:35:41 -05:00
if ($dbFileName !== ':memory:' && file_exists($dbFileName))
{
2017-02-15 15:35:41 -05:00
$dbFile = file_get_contents($dbFileName);
$this->validDatabase = (strpos($dbFile, 'SQLite format 3') === 0);
}
else
{
2017-02-15 15:35:41 -05:00
$this->validDatabase = FALSE;
}
}
2019-01-07 14:29:15 -05:00
else if ($this->db === NULL)
{
2019-01-07 14:29:15 -05:00
$this->validDatabase = FALSE;
}
}
}
// End of Collection.php