CacheConfig   A
last analyzed

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 setLoggerPath() 0 13 1
A getOptions() 0 3 1
A setUp() 0 3 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\Enums\DatabaseDriver;
11
use Silviooosilva\CacheerPhp\Exceptions\ConnectionException;
12
use Silviooosilva\CacheerPhp\Utils\CacheDriver;
13
14
/**
15
 * Class CacheConfig
16
 * @author Sílvio Silva <https://github.com/silviooosilva>
17
 * @package Silviooosilva\CacheerPhp
18
 */
19
class CacheConfig
20
{
21
22
    /**
23
     * @var Cacheer
24
     */
25
    protected Cacheer $cacheer;
26
27
    /**
28
     * CacheConfig constructor.
29
     *
30
     * @param Cacheer $cacheer
31
     */
32
    public function __construct(Cacheer $cacheer)
33
    {
34
        $this->cacheer = $cacheer;
35
        $this->setTimeZone(date_default_timezone_get());
36
    }
37
38
    /**
39
     * Sets the default timezone for the application.
40
     * 
41
     * @param string $timezone
42
     * @return $this
43
     */
44
    public function setTimeZone(string $timezone): CacheConfig
45
    {
46
        /**
47
         * Make sure the provided timezone is valid
48
         * 
49
         * https://www.php.net/manual/en/timezones.php 
50
         * */
51
52
        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

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