Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
12 / 12
Sqlite
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
12 / 12
 __construct
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 doClear
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 rebuild
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
<?php
/*
 * This file is part of the Shieldon Simple Cache package.
 *
 * (c) Terry L. <contact@terryl.in>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
declare(strict_types=1);
namespace Shieldon\SimpleCache\Driver;
use Shieldon\SimpleCache\CacheProvider;
use PDO;
use function file_put_contents;
use function rtrim;
/**
 * A cache driver class provided by SQLite database.
 */
class Sqlite extends CacheProvider
{
    use SqlTrait;
    /**
     * The absolute path of the storage's directory.
     * It must be writable.
     *
     * @var string
     */
    protected $storage = '/tmp/simple-cache';
    /**
     * Constructor.
     *
     * @param array $setting The settings.
     * 
     * @throws CacheException
     */
    public function __construct(array $setting = [])
    {
        if (isset($setting['storage'])) {
            $this->storage = rtrim($setting['storage'], '/');
        }
        $this->assertDirectoryWritable($this->storage);
        $this->db = new PDO('sqlite:' . $this->storage . '/cache.sqlite3');
    }
    /**
     * Delete all caches by an extended Cache Driver.
     * 
     * @return bool
     */
    protected function doClear(): bool
    {
        $sql = 'DELETE FROM ' . $this->table;
        $query = $this->db->prepare($sql);
        $result = $query->execute();
        return $result;
    }
    /**
     * @inheritDoc
     */
    public function rebuild(): bool
    {
        try {
            $sql = "CREATE TABLE IF NOT EXISTS {$this->table} (
                cache_key VARCHAR(40) PRIMARY KEY,
                cache_value LONGTEXT
            );";
            $this->db->query($sql);
        // @codeCoverageIgnoreStart
        } catch (Exception $e) {
            file_put_contents('php://stderr', $e->getMessage());
            return false;
        }
        // @codeCoverageIgnoreEnd
        return true;
    }
}