Passed
Pull Request — main (#2)
by Sílvio
03:03
created

CacheDriver::useArrayDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Utils;
4
5
use Exception;
6
use Silviooosilva\CacheerPhp\Cacheer;
7
use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore;
8
use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore;
9
use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore;
10
use Silviooosilva\CacheerPhp\CacheStore\DatabaseCacheStore;
11
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException;
12
use Silviooosilva\CacheerPhp\Helpers\EnvHelper;
13
14
/**
15
 * Class CacheDriver
16
 * @author Sílvio Silva <https://github.com/silviooosilva>
17
 * @package Silviooosilva\CacheerPhp
18
 */
19
class CacheDriver
20
{
21
22
    /**
23
     * @var Cacheer
24
     */
25
    protected $cacheer;
26
27
    /** @param string $logPath */
28
    public string $logPath = 'cacheer.log';
29
30
    public function __construct(Cacheer $cacheer)
31
    {
32
        $this->cacheer = $cacheer;
33
    }
34
35
    /**
36
     * @return Cacheer
37
     */
38
    public function useDatabaseDriver()
39
    {
40
        $this->cacheer->cacheStore = new DatabaseCacheStore($this->logPath);
41
        return $this->cacheer;
42
    }
43
44
    /**
45
     * @throws \Exception
46
     * @return Cacheer
47
     */
48
    public function useFileDriver()
49
    {
50
        if (!isset($this->cacheer->options['cacheDir'])) {
51
            throw CacheFileException::create("The 'cacheDir' option is required for the file driver.");
52
        }
53
        $this->cacheer->options['loggerPath'] = $this->logPath;
54
        $this->cacheer->cacheStore = new FileCacheStore($this->cacheer->options);
55
        return $this->cacheer;
56
    }
57
58
    /**
59
     * @return Cacheer
60
     */
61
    public function useRedisDriver()
62
    {
63
        $this->cacheer->cacheStore = new RedisCacheStore($this->logPath);
64
        return $this->cacheer;
65
    }
66
67
    /**
68
    * @return Cacheer
69
    */
70
    public function useArrayDriver()
71
    {
72
        $this->cacheer->cacheStore = new ArrayCacheStore($this->logPath);
73
        return $this->cacheer;
74
    }
75
76
    /**
77
     * @return Cacheer
78
     */
79
    public function useDefaultDriver()
80
    {
81
        if (!isset($this->cacheer->options['cacheDir'])) {
82
            $cacheDir = EnvHelper::getRootPath() . "CacheerPHP/Cache";
83
            if ($this->isDir($cacheDir)) {
84
                $this->cacheer->options['cacheDir'] = $cacheDir;
85
86
            } else {
87
                throw CacheFileException::create("Failed to create cache directory: " . $cacheDir);
88
            }
89
        }
90
        $this->useFileDriver();
91
        return $this->cacheer;
92
    }
93
94
    /**
95
    * @param mixed $dirName
96
    * @return bool
97
    */
98
    private function isDir(mixed $dirName)
99
    {
100
      if (is_dir($dirName)) {
101
          return true;
102
      }
103
      return mkdir($dirName, 0755, true);
104
    }
105
}
106