Completed
Push — master ( be9660...707803 )
by Georges
16s queued 13s
created

DriverBaseTrait::driverWriteTags()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 74
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 25
nc 11
nop 1
dl 0
loc 74
rs 8.4444
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 file.
10
 *
11
 * @author Khoa Bui (khoaofgod)  <[email protected]> https://www.phpfastcache.com
12
 * @author Georges.L (Geolim4)  <[email protected]>
13
 *
14
 */
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Core\Pool;
18
19
use DateTime;
20
use Exception;
21
use Phpfastcache\Config\ConfigurationOption;
22
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
23
use Phpfastcache\Entities\DriverIO;
24
use Phpfastcache\Exceptions\{PhpfastcacheDriverCheckException, PhpfastcacheDriverConnectException};
25
use ReflectionObject;
26
27
28
/**
29
 * Class DriverBaseTrait
30
 * @package phpFastCache\Cache
31
 */
32
trait DriverBaseTrait
33
{
34
    use ExtendedCacheItemPoolTrait;
35
36
    /**
37
     * @var ConfigurationOption the options
38
     */
39
    protected $config;
40
41
    /**
42
     * @var bool
43
     */
44
    protected $fallback = false;
45
46
    /**
47
     * @var mixed Instance of driver service
48
     */
49
    protected $instance;
50
51
    /**
52
     * @var string
53
     */
54
    protected $driverName;
55
56
    /**
57
     * @internal This variable is read-access only
58
     * @var string
59
     */
60
    protected $instanceId;
61
62
    /**
63
     * Driver constructor.
64
     * @param ConfigurationOption $config
65
     * @param string $instanceId
66
     * @throws PhpfastcacheDriverCheckException
67
     * @throws PhpfastcacheDriverConnectException
68
     */
69
    public function __construct(ConfigurationOption $config, $instanceId)
70
    {
71
        $this->setConfig($config);
72
        $this->instanceId = $instanceId;
73
        $this->IO = new DriverIO();
74
75
        if (!$this->driverCheck()) {
76
            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...
77
        }
78
79
        try {
80
            $this->driverConnect();
81
        } catch (Exception $e) {
82
            throw new PhpfastcacheDriverConnectException(
83
                sprintf(
84
                    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...
85
                    $this->getDriverName(),
86
                    $e->getMessage(),
87
                    $e->getLine() ?: 'unknown line',
88
                    $e->getFile() ?: 'unknown file'
89
                )
90
            );
91
        }
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getDriverName(): string
98
    {
99
        if (!$this->driverName) {
100
            $this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1));
101
        }
102
        return $this->driverName;
103
    }
104
105
    /**
106
     * @return ConfigurationOption
107
     */
108
    public function getDefaultConfig(): ConfigurationOption
109
    {
110
        $className = self::getConfigClass();
111
        return new $className;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public static function getConfigClass(): string
118
    {
119
        $localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config';
120
        if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) {
121
            return $localConfigClass;
122
        }
123
        return ConfigurationOption::class;
124
    }
125
126
    /**
127
     * @param ExtendedCacheItemInterface $item
128
     * @return array
129
     */
130
    public function driverPreWrap(ExtendedCacheItemInterface $item): array
131
    {
132
        $wrap = [
133
            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...
134
            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...
135
            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...
136
        ];
137
138
        if ($this->getConfig()->isItemDetailedDate()) {
139
            $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...
140
            /**
141
             * If the creation date exists
142
             * reuse it else set a new Date
143
             */
144
            $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...
145
        } else {
146
            $wrap[self::DRIVER_MDATE_WRAPPER_INDEX] = null;
147
            $wrap[self::DRIVER_CDATE_WRAPPER_INDEX] = null;
148
        }
149
150
        return $wrap;
151
    }
152
153
    /**
154
     * @return ConfigurationOption
155
     */
156
    public function getConfig(): ConfigurationOption
157
    {
158
        return $this->config;
159
    }
160
161
    /**
162
     * @param ConfigurationOption $config
163
     */
164
    public function setConfig(ConfigurationOption $config)
165
    {
166
        $this->config = $config;
167
    }
168
169
    /**
170
     * @param array $wrapper
171
     * @return mixed
172
     */
173
    public function driverUnwrapData(array $wrapper)
174
    {
175
        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...
176
    }
177
178
179
    /**
180
     * @param array $wrapper
181
     * @return DateTime
182
     */
183
    public function driverUnwrapEdate(array $wrapper)
184
    {
185
        return $wrapper[self::DRIVER_EDATE_WRAPPER_INDEX];
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...
186
    }
187
188
    /**
189
     * @param array $wrapper
190
     * @return DateTime
191
     */
192
    public function driverUnwrapCdate(array $wrapper)
193
    {
194
        return $wrapper[self::DRIVER_CDATE_WRAPPER_INDEX];
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...
195
    }
196
197
    /**
198
     * @param array $wrapper
199
     * @return DateTime
200
     */
201
    public function driverUnwrapMdate(array $wrapper)
202
    {
203
        return $wrapper[self::DRIVER_MDATE_WRAPPER_INDEX];
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...
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function getInstanceId(): string
210
    {
211
        return $this->instanceId;
212
    }
213
214
215
    /**
216
     * Encode data types such as object/array
217
     * for driver that does not support
218
     * non-scalar value
219
     * @param $data
220
     * @return string
221
     */
222
    protected function encode($data): string
223
    {
224
        return \serialize($data);
225
    }
226
227
    /**
228
     * Decode data types such as object/array
229
     * for driver that does not support
230
     * non-scalar value
231
     * @param string|null $value
232
     * @return mixed
233
     */
234
    protected function decode($value)
235
    {
236
        return \unserialize((string)$value, ['allowed_classes' => true]);
237
    }
238
239
    /**
240
     * Check if phpModule or CGI
241
     * @return bool
242
     */
243
    protected function isPHPModule(): bool
244
    {
245
        return (\PHP_SAPI === 'apache2handler' || \strpos(\PHP_SAPI, 'handler') !== false);
246
    }
247
}
248