Total Complexity | 47 |
Total Lines | 328 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Driver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Driver implements ExtendedCacheItemPoolInterface |
||
35 | { |
||
36 | use DriverBaseTrait, IOHelperTrait; |
||
37 | |||
38 | /** |
||
39 | * |
||
40 | */ |
||
41 | const FILE_DIR = 'sqlite'; |
||
42 | /** |
||
43 | * |
||
44 | */ |
||
45 | const INDEXING_FILE = 'indexing'; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $maxSize = 10; // 10 mb |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $currentDB = 1; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $SqliteDir = ''; |
||
61 | |||
62 | /** |
||
63 | * @var \PDO |
||
64 | */ |
||
65 | protected $indexing; |
||
66 | |||
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | * @throws \Phpfastcache\Exceptions\PhpfastcacheCoreException |
||
71 | */ |
||
72 | public function getSqliteDir(): string |
||
73 | { |
||
74 | return $this->SqliteDir ?: $this->getPath() . \DIRECTORY_SEPARATOR . self::FILE_DIR; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function driverCheck(): bool |
||
81 | { |
||
82 | return \extension_loaded('pdo_sqlite') && (\is_writable($this->getSqliteDir()) || @mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return bool |
||
87 | * @throws PhpfastcacheIOException |
||
88 | */ |
||
89 | protected function driverConnect(): bool |
||
90 | { |
||
91 | if (!\file_exists($this->getSqliteDir()) && !@mkdir($this->getSqliteDir(), $this->getDefaultChmod(), true)) { |
||
92 | throw new PhpfastcacheIOException(\sprintf('Sqlite cannot write in "%s", aborting...', $this->getPath())); |
||
93 | } |
||
94 | if (!\file_exists($this->getPath() . '/' . self::FILE_DIR)) { |
||
95 | if (!mkdir($this->getPath() . '/' . self::FILE_DIR, $this->getDefaultChmod(), true) |
||
96 | ) { |
||
97 | $this->fallback = true; |
||
98 | } |
||
99 | } |
||
100 | $this->SqliteDir = $this->getPath() . '/' . self::FILE_DIR; |
||
101 | |||
102 | return true; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param \Psr\Cache\CacheItemInterface $item |
||
107 | * @return null|array |
||
108 | */ |
||
109 | protected function driverRead(CacheItemInterface $item) |
||
110 | { |
||
111 | try { |
||
112 | $stm = $this->getDb($item->getKey()) |
||
113 | ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
||
114 | $stm->execute([ |
||
115 | ':keyword' => $item->getKey(), |
||
116 | ]); |
||
117 | $row = $stm->fetch(PDO::FETCH_ASSOC); |
||
118 | |||
119 | } catch (PDOException $e) { |
||
120 | try { |
||
121 | $stm = $this->getDb($item->getKey(), true) |
||
122 | ->prepare("SELECT * FROM `caching` WHERE `keyword`=:keyword LIMIT 1"); |
||
123 | $stm->execute([ |
||
124 | ':keyword' => $item->getKey(), |
||
125 | ]); |
||
126 | $row = $stm->fetch(PDO::FETCH_ASSOC); |
||
127 | } catch (PDOException $e) { |
||
128 | return null; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | if (isset($row['object'])) { |
||
133 | return $this->decode($row['object']); |
||
134 | } |
||
135 | |||
136 | return null; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param \Psr\Cache\CacheItemInterface $item |
||
141 | * @return mixed |
||
142 | * @throws PhpfastcacheInvalidArgumentException |
||
143 | */ |
||
144 | protected function driverWrite(CacheItemInterface $item): bool |
||
145 | { |
||
146 | /** |
||
147 | * Check for Cross-Driver type confusion |
||
148 | */ |
||
149 | if ($item instanceof Item) { |
||
150 | try { |
||
151 | $stm = $this->getDb($item->getKey()) |
||
152 | ->prepare("INSERT OR REPLACE INTO `caching` (`keyword`,`object`,`exp`) values(:keyword,:object,:exp)"); |
||
153 | $stm->execute([ |
||
154 | ':keyword' => $item->getKey(), |
||
155 | ':object' => $this->encode($this->driverPreWrap($item)), |
||
156 | ':exp' => $item->getExpirationDate()->getTimestamp(), |
||
157 | ]); |
||
158 | |||
159 | return true; |
||
160 | } catch (\PDOException $e) { |
||
161 | return false; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param \Psr\Cache\CacheItemInterface $item |
||
170 | * @return bool |
||
171 | * @throws PhpfastcacheInvalidArgumentException |
||
172 | */ |
||
173 | protected function driverDelete(CacheItemInterface $item): bool |
||
174 | { |
||
175 | /** |
||
176 | * Check for Cross-Driver type confusion |
||
177 | */ |
||
178 | if ($item instanceof Item) { |
||
179 | try { |
||
180 | $stm = $this->getDb($item->getKey()) |
||
181 | ->prepare("DELETE FROM `caching` WHERE (`exp` <= :U) OR (`keyword`=:keyword) "); |
||
182 | |||
183 | return $stm->execute([ |
||
184 | ':keyword' => $item->getKey(), |
||
185 | ':U' => \time(), |
||
186 | ]); |
||
187 | } catch (PDOException $e) { |
||
188 | return false; |
||
189 | } |
||
190 | } else { |
||
191 | throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected'); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return bool |
||
197 | */ |
||
198 | protected function driverClear(): bool |
||
199 | { |
||
200 | $this->instance = []; |
||
201 | $this->indexing = null; |
||
202 | |||
203 | // delete everything before reset indexing |
||
204 | $dir = opendir($this->getSqliteDir()); |
||
205 | while ($file = readdir($dir)) { |
||
|
|||
206 | if ($file != '.' && $file != '..') { |
||
207 | unlink($this->getSqliteDir() . '/' . $file); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | return true; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * INIT NEW DB |
||
216 | * @param \PDO $db |
||
217 | */ |
||
218 | public function initDB(\PDO $db) |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * INIT Indexing DB |
||
229 | * @param \PDO $db |
||
230 | */ |
||
231 | public function initIndexing(\PDO $db) |
||
232 | { |
||
233 | |||
234 | // delete everything before reset indexing |
||
235 | $dir = opendir($this->SqliteDir); |
||
236 | while ($file = readdir($dir)) { |
||
237 | if ($file != '.' && $file != '..' && $file != 'indexing' && $file != 'dbfastcache') { |
||
238 | unlink($this->SqliteDir . '/' . $file); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | $db->exec('DROP TABLE if exists "balancing"'); |
||
243 | $db->exec('CREATE TABLE "balancing" ("keyword" VARCHAR PRIMARY KEY NOT NULL UNIQUE, "db" INTEGER)'); |
||
244 | $db->exec('CREATE INDEX "db" ON "balancing" ("db")'); |
||
245 | $db->exec('CREATE UNIQUE INDEX "lookup" ON "balancing" ("keyword")'); |
||
246 | |||
247 | } |
||
248 | |||
249 | /** |
||
250 | * INIT Instant DB |
||
251 | * Return Database of Keyword |
||
252 | * @param $keyword |
||
253 | * @return int |
||
254 | */ |
||
255 | public function indexing($keyword) |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @param string $keyword |
||
322 | * @param bool $reset |
||
323 | * @return PDO |
||
324 | */ |
||
325 | public function getDb(string $keyword, bool $reset = false): PDO |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @return array |
||
358 | */ |
||
359 | public function __sleep(): array |
||
362 | } |
||
363 | } |