Passed
Push — main ( 700cae...bce8dd )
by Sílvio
01:08
created

CacheConfig   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 19
c 4
b 0
f 0
dl 0
loc 102
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setDriver() 0 3 1
A setDatabaseConnection() 0 3 1
A setTimeZone() 0 12 2
A getOptions() 0 3 1
A setUp() 0 3 1
A setLoggerPath() 0 13 1
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Helpers;
4
5
use Silviooosilva\CacheerPhp\Cacheer;
6
use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore;
7
use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore;
8
use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore;
9
use Silviooosilva\CacheerPhp\Core\Connect;
10
use Silviooosilva\CacheerPhp\Exceptions\ConnectionException;
11
use Silviooosilva\CacheerPhp\Utils\CacheDriver;
12
13
/**
14
 * Class CacheConfig
15
 * @author Sílvio Silva <https://github.com/silviooosilva>
16
 * @package Silviooosilva\CacheerPhp
17
 */
18
class CacheConfig
19
{
20
21
    /**
22
     * @var Cacheer
23
     */
24
    protected Cacheer $cacheer;
25
26
    /**
27
     * CacheConfig constructor.
28
     *
29
     * @param Cacheer $cacheer
30
     */
31
    public function __construct(Cacheer $cacheer)
32
    {
33
        $this->cacheer = $cacheer;
34
        $this->setTimeZone(date_default_timezone_get());
35
    }
36
37
    /**
38
     * Sets the default timezone for the application.
39
     * 
40
     * @param string $timezone
41
     * @return $this
42
     */
43
    public function setTimeZone(string $timezone): CacheConfig
44
    {
45
        /**
46
         * Make sure the provided timezone is valid
47
         * 
48
         * https://www.php.net/manual/en/timezones.php 
49
         * */
50
51
        if (in_array($timezone, timezone_identifiers_list())) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of timezone_identifiers_list() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
timezone_identifiers_list() of type void is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        if (in_array($timezone, /** @scrutinizer ignore-type */ timezone_identifiers_list())) {
Loading history...
52
            date_default_timezone_set($timezone);
53
        }
54
        return $this;
55
    }
56
57
    /**
58
     * Sets the cache driver for the application.
59
     * 
60
     * @return CacheDriver
61
     */
62
    public function setDriver(): CacheDriver
63
    {
64
        return new CacheDriver($this->cacheer);
65
    }
66
67
    /**
68
     * Sets the logger path for the cache driver.
69
     * 
70
     * @param string $path
71
     * @return Cacheer
72
     */
73
    public function setLoggerPath(string $path): Cacheer
74
    {
75
        
76
        $cacheDriver = $this->setDriver();
77
        $cacheDriver->logPath = $path;
78
79
        $cacheDriverInstance = $this->cacheer->cacheStore;
80
81
        return match (get_class($cacheDriverInstance)) {
82
            FileCacheStore::class => $cacheDriver->useFileDriver(),
83
            RedisCacheStore::class => $cacheDriver->useRedisDriver(),
84
            ArrayCacheStore::class => $cacheDriver->useArrayDriver(),
85
            default => $cacheDriver->useDatabaseDriver(),
86
        };
87
    }
88
89
    /**
90
     * Sets the database connection type for the application.
91
     *
92
     * @param string $driver
93
     * @return void
94
     * @throws ConnectionException
95
     */
96
    public function setDatabaseConnection(string $driver): void
97
    {
98
        Connect::setConnection($driver);
99
    }
100
101
    /**
102
     * Sets up the Cacheer instance with the provided options.
103
     *
104
     * @param array $options
105
     * @return void
106
     */
107
    public function setUp(array $options): void
108
    {
109
        $this->cacheer->options = $options;
110
    }
111
112
    /**
113
     * Gets the options for the Cacheer instance.
114
     *
115
     * @return array
116
     */
117
    public function getOptions(): array
118
    {
119
        return $this->cacheer->options;
120
    }
121
}
122