Passed
Push — v9 ( 345353...555fde )
by Georges
04:49
created

DriverBaseTrait   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 231
rs 10
c 0
b 0
f 0
wmc 29

14 Methods

Rating   Name   Duplication   Size   Complexity  
A driverUnwrapEdate() 0 7 2
A getInstanceId() 0 3 1
A getDefaultConfig() 0 5 1
A driverPreWrap() 0 31 5
A driverUnwrapData() 0 3 1
A setConfig() 0 5 1
A driverUnwrapCdate() 0 7 2
A getConfigClass() 0 7 3
A decode() 0 3 1
A getDriverName() 0 6 2
A __construct() 0 25 5
A driverUnwrapMdate() 0 7 2
A getItemClass() 0 7 2
A encode() 0 3 1
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Core\Pool;
17
18
use DateTime;
19
use DateTimeInterface;
20
use Phpfastcache\Config\ConfigurationOptionInterface;
21
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
22
use Phpfastcache\Util\ClassNamespaceResolverTrait;
23
use Throwable;
24
use Phpfastcache\Config\ConfigurationOption;
25
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
26
use Phpfastcache\Entities\DriverIO;
27
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
28
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
29
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
30
use Phpfastcache\Exceptions\PhpfastcacheIOException;
31
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
32
use ReflectionObject;
33
34
trait DriverBaseTrait
35
{
36
    use DriverPoolAbstractTrait;
37
    use ClassNamespaceResolverTrait;
38
39
    protected static array $cacheItemClasses = [];
40
41
    protected ConfigurationOptionInterface $config;
42
43
    protected object|array|null $instance;
0 ignored issues
show
Bug introduced by
The type Phpfastcache\Core\Pool\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
45
    protected string $driverName;
46
47
    protected string $instanceId;
48
49
    /**
50
     * Driver constructor.
51
     * @param ConfigurationOptionInterface $config
52
     * @param string $instanceId
53
     * @throws PhpfastcacheCoreException
54
     * @throws PhpfastcacheDriverCheckException
55
     * @throws PhpfastcacheDriverConnectException
56
     * @throws PhpfastcacheIOException
57
     * @throws PhpfastcacheInvalidArgumentException
58
     */
59
    public function __construct(ConfigurationOptionInterface $config, string $instanceId)
60
    {
61
        $this->setConfig($config);
62
        $this->instanceId = $instanceId;
63
        $this->IO = new DriverIO();
0 ignored issues
show
Bug Best Practice introduced by
The property IO does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
64
65
        if (!$this->driverCheck()) {
66
            throw new PhpfastcacheDriverCheckException(\sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...t::DRIVER_CHECK_FAILURE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
        }
68
69
        try {
70
            $this->driverConnect();
71
            $config->lock($this);
0 ignored issues
show
Bug introduced by
$this of type Phpfastcache\Core\Pool\DriverBaseTrait is incompatible with the type Phpfastcache\Core\Pool\E...dCacheItemPoolInterface expected by parameter $poolInstance of Phpfastcache\Config\Lock...rationInterface::lock(). ( Ignorable by Annotation )

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

71
            $config->lock(/** @scrutinizer ignore-type */ $this);
Loading history...
72
        } catch (Throwable $e) {
73
            throw new PhpfastcacheDriverConnectException(
74
                sprintf(
75
                    self::DRIVER_CONNECT_FAILURE,
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...:DRIVER_CONNECT_FAILURE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
                    $e::class,
77
                    $this->getDriverName(),
78
                    $e->getMessage(),
79
                    $e->getLine() ?: 'unknown line',
80
                    $e->getFile() ?: 'unknown file'
81
                ),
82
                0,
83
                $e
84
            );
85
        }
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getDriverName(): string
92
    {
93
        if (!isset($this->driverName)) {
94
            $this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1));
95
        }
96
        return $this->driverName;
97
    }
98
99
    /**
100
     * @return ConfigurationOption
101
     */
102
    public function getDefaultConfig(): ConfigurationOption
103
    {
104
        $className = $this::getConfigClass();
105
106
        return new $className();
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public static function getConfigClass(): string
113
    {
114
        $localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config';
115
        if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) {
116
            return $localConfigClass;
117
        }
118
        return ConfigurationOption::class;
119
    }
120
121
    public static function getItemClass(): string
122
    {
123
        if (!isset(self::$cacheItemClasses[static::class])) {
124
            self::$cacheItemClasses[static::class] = self::getClassNamespace() . '\\' . 'Item';
125
        }
126
127
        return self::$cacheItemClasses[static::class];
128
    }
129
130
131
    /**
132
     * @param ExtendedCacheItemInterface $item
133
     * @param bool $stringifyDate
134
     * @return array
135
     * @throws PhpfastcacheLogicException
136
     */
137
    public function driverPreWrap(ExtendedCacheItemInterface $item, bool $stringifyDate = false): array
138
    {
139
        $wrap = [
140
            self::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...RIVER_KEY_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
141
            self::DRIVER_DATA_WRAPPER_INDEX => $item->get(),
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...IVER_DATA_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
142
            self::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(),
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...IVER_TAGS_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
143
            self::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(),
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...VER_EDATE_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
144
        ];
145
146
        if ($this->getConfig()->isItemDetailedDate()) {
147
            $wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime();
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...VER_MDATE_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
148
            /**
149
             * If the creation date exists
150
             * reuse it else set a new Date
151
             */
152
            $wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate() ?: new DateTime();
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...VER_CDATE_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
153
        } else {
154
            $wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = null;
155
            $wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = null;
156
        }
157
158
        if ($stringifyDate) {
159
            $wrap = \array_map(static function ($value) {
160
                if ($value instanceof DateTimeInterface) {
161
                    return $value->format(DateTimeInterface::W3C);
162
                }
163
                return $value;
164
            }, $wrap);
165
        }
166
167
        return $wrap;
168
    }
169
170
    /**
171
     * @return ConfigurationOption
172
     */
173
    abstract public function getConfig(): ConfigurationOption;
174
175
    /**
176
     * @param ConfigurationOptionInterface $config
177
     * @return static
178
     */
179
    public function setConfig(ConfigurationOptionInterface $config): static
180
    {
181
        $this->config = $config;
182
183
        return $this;
184
    }
185
186
    /**
187
     * @param array $wrapper
188
     * @return mixed
189
     * @throws \Exception
190
     */
191
    public function driverUnwrapData(array $wrapper): mixed
192
    {
193
        return $wrapper[self::DRIVER_DATA_WRAPPER_INDEX];
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...IVER_DATA_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
194
    }
195
196
    /**
197
     * @param array $wrapper
198
     * @return DateTime
199
     */
200
    public function driverUnwrapEdate(array $wrapper): \DateTime
201
    {
202
        if ($wrapper[self::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTime) {
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...VER_EDATE_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
203
            return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX];
204
        }
205
206
        return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX]);
207
    }
208
209
    /**
210
     * @param array $wrapper
211
     * @return DateTime|null
212
     */
213
    public function driverUnwrapCdate(array $wrapper): ?\DateTime
214
    {
215
        if ($wrapper[self::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTime) {
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...VER_CDATE_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
216
            return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX];
217
        }
218
219
        return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX]);
220
    }
221
222
    /**
223
     * @param array $wrapper
224
     * @return DateTime|null
225
     */
226
    public function driverUnwrapMdate(array $wrapper): ?\DateTime
227
    {
228
        if ($wrapper[self::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTime) {
0 ignored issues
show
Bug introduced by
The constant Phpfastcache\Core\Pool\D...VER_MDATE_WRAPPER_INDEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
229
            return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX];
230
        }
231
232
        return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX]);
233
    }
234
235
    /**
236
     * @return string
237
     */
238
    public function getInstanceId(): string
239
    {
240
        return $this->instanceId;
241
    }
242
243
    /**
244
     * Encode data types such as object/array
245
     * for driver that does not support
246
     * non-scalar value
247
     * @param $data
248
     * @return string
249
     */
250
    protected function encode($data): string
251
    {
252
        return \serialize($data);
253
    }
254
255
    /**
256
     * Decode data types such as object/array
257
     * for driver that does not support
258
     * non-scalar value
259
     * @param string|null $value
260
     * @return mixed
261
     */
262
    protected function decode(?string $value): mixed
263
    {
264
        return \unserialize((string) $value, ['allowed_classes' => true]);
265
    }
266
}
267