Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
2 / 6
CRAP
16.67% covered (danger)
16.67%
4 / 24
FileCache
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
2 / 6
67.87
16.67% covered (danger)
16.67%
4 / 24
 get
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 set
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 destroy
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 has
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
3 / 3
 ttl
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 clear
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 14
<?php
declare(strict_types=1);
namespace Eywa\Cache {
    use Eywa\File\File;
    class FileCache implements CacheInterface
    {
        /**
         * @inheritDoc
         */
        public function get(string $key)
        {
            if ($this->has($key)) {
                return (new File($key))->read();
            }
            return false;
        }
        /**
         * @inheritDoc
         */
        public function set(string $key, $value): CacheInterface
        {
            (new File($key, EMPTY_AND_WRITE_FILE_MODE))->write($value)->flush();
            return $this;
        }
        /**
         * @inheritDoc
         */
        public function destroy(string $key): bool
        {
            return  (new File($key))->remove();
        }
        /**
         * @inheritDoc
         */
        public function has(string $key): bool
        {
            $x = env('CACHE_TIME_DIVISER', 60);
            $x = $x <= 0 ? 1 : $x;
            return file_exists($key) &&   (time() - filemtime($key)) / $x  < $this->ttl();
        }
        /**
         * @inheritDoc
         */
        public function ttl(): int
        {
            return  intval(env('CACHE_TTL', CACHE_DEFAULT_TTL));
        }
        /**
         * @inheritDoc
         */
        public function clear(): bool
        {
            $x = function () {
                return array_merge(
                    files(
                        base('cache', '*.html')
                    ),
                    files(
                        base('cache', '*', '*.html')
                    ),
                    array_merge(
                        files(
                            base('cache', '*.php')
                        ),
                        files(
                            base('cache', '*', '*.php')
                        )
                    )
                );
            };
            foreach ($x() as $file) {
                unlink($file);
            }
            return  not_def($x());
        }
    }
}