Total Complexity | 41 |
Total Lines | 381 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like ContentRepository 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 ContentRepository, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class ContentRepository |
||
13 | { |
||
14 | protected $storagePath; |
||
15 | protected $contentDbHandle; |
||
16 | |||
17 | /** |
||
18 | * ContentRepository constructor. |
||
19 | * @param $storagePath |
||
20 | */ |
||
21 | public function __construct($storagePath) |
||
22 | { |
||
23 | $this->storagePath = $storagePath; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @return \PDO |
||
28 | */ |
||
29 | public function getContentDbHandle() |
||
30 | { |
||
31 | if ($this->contentDbHandle === null) { |
||
32 | $this->contentDbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'content.db'); |
||
|
|||
33 | } |
||
34 | return $this->contentDbHandle; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Prepare the sql statement |
||
39 | * @param $sql |
||
40 | * @return \PDOStatement |
||
41 | * @throws \Exception |
||
42 | */ |
||
43 | protected function getDbStatement($sql) |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param Repository $repository |
||
57 | * @param $document |
||
58 | * @param $db |
||
59 | * @param $documents |
||
60 | * @param $key |
||
61 | * @return mixed |
||
62 | */ |
||
63 | private function setAssetsToDocumentFolders(Repository $repository, $document, $db, $documents, $key) |
||
64 | { |
||
65 | if ($document->type === 'folder') { |
||
66 | $document->dbHandle = $db; |
||
67 | $document->documentStorage = new DocumentStorage($repository); |
||
68 | $documents[$key] = $document; |
||
69 | } |
||
70 | |||
71 | return $documents; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param Repository $repository |
||
76 | * @param string $folderPath |
||
77 | * @return array |
||
78 | * @throws \Exception |
||
79 | */ |
||
80 | public function getDocumentsWithState(Repository $repository, $folderPath = '/') |
||
81 | { |
||
82 | $db = $this->getContentDbHandle(); |
||
83 | $folderPathWithWildcard = $folderPath . '%'; |
||
84 | |||
85 | $ifRootIndex = 1; |
||
86 | if ($folderPath === '/') { |
||
87 | $ifRootIndex = 0; |
||
88 | } |
||
89 | |||
90 | $sql = ' |
||
91 | SELECT documents_unpublished.*, |
||
92 | IFNULL(documents_published.state,"unpublished") as state, |
||
93 | IFNULL(documents_published.publicationDate,NULL) as publicationDate, |
||
94 | (documents_published.lastModificationDate != documents_unpublished.lastModificationDate) as unpublishedChanges |
||
95 | FROM documents_unpublished |
||
96 | LEFT JOIN documents_published |
||
97 | ON documents_published.path = documents_unpublished.path |
||
98 | WHERE documents_unpublished.`path` LIKE ' . $db->quote($folderPathWithWildcard) . ' |
||
99 | AND substr(documents_unpublished.`path`, ' . (strlen($folderPath) + $ifRootIndex + 1) . ') NOT LIKE "%/%" |
||
100 | AND length(documents_unpublished.`path`) > ' . (strlen($folderPath) + $ifRootIndex) . ' |
||
101 | AND documents_unpublished.path != ' . $db->quote($folderPath) . ' |
||
102 | ORDER BY documents_unpublished.`type` DESC, documents_unpublished.`path` ASC |
||
103 | '; |
||
104 | $stmt = $this->getDbStatement($sql); |
||
105 | |||
106 | |||
107 | $documents = $stmt->fetchAll(\PDO::FETCH_CLASS, Document::class); |
||
108 | foreach ($documents as $key => $document) { |
||
109 | $documents = $this->setAssetsToDocumentFolders($repository, $document, $db, $documents, $key); |
||
110 | } |
||
111 | //dump($documents); |
||
112 | return $documents; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get all documents and folders in a certain path |
||
117 | * |
||
118 | * @param Repository $repository |
||
119 | * @param $folderPath |
||
120 | * @param string $state |
||
121 | * @return array |
||
122 | * @throws \Exception |
||
123 | */ |
||
124 | public function getDocumentsByPath(Repository $repository, $folderPath, $state = 'published') |
||
125 | { |
||
126 | if (!in_array($state, Document::$DOCUMENT_STATES, true)) { |
||
127 | throw new \RuntimeException('Unsupported document state: ' . $state); |
||
128 | } |
||
129 | $db = $this->getContentDbHandle(); |
||
130 | $folderPathWithWildcard = $folderPath . '%'; |
||
131 | |||
132 | $sql = 'SELECT * |
||
133 | FROM documents_' . $state . ' |
||
134 | WHERE `path` LIKE ' . $db->quote($folderPathWithWildcard) . ' |
||
135 | AND substr(`path`, ' . (strlen($folderPath) + 1) . ') NOT LIKE "%/%" |
||
136 | AND path != ' . $db->quote($folderPath) . ' |
||
137 | ORDER BY `type` DESC, `path` ASC'; |
||
138 | $stmt = $this->getDbStatement($sql); |
||
139 | |||
140 | $documents = $stmt->fetchAll(\PDO::FETCH_CLASS, Document::class); |
||
141 | foreach ($documents as $key => $document) { |
||
142 | $documents = $this->setAssetsToDocumentFolders($repository, $document, $db, $documents, $key); |
||
143 | } |
||
144 | return $documents; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param \CloudControl\Cms\storage\Repository $repository |
||
149 | * @param $path |
||
150 | * @param string $state |
||
151 | * @return bool|Document |
||
152 | * @throws \Exception |
||
153 | */ |
||
154 | public function getDocumentByPath(Repository $repository, $path, $state = 'published') |
||
155 | { |
||
156 | if (!in_array($state, Document::$DOCUMENT_STATES, true)) { |
||
157 | throw new \RuntimeException('Unsupported document state: ' . $state); |
||
158 | } |
||
159 | $db = $this->getContentDbHandle(); |
||
160 | $document = $this->fetchDocument(' |
||
161 | SELECT * |
||
162 | FROM documents_' . $state . ' |
||
163 | WHERE path = ' . $db->quote($path) . ' |
||
164 | '); |
||
165 | if ($document instanceof Document && $document->type === 'folder') { |
||
166 | $document->dbHandle = $db; |
||
167 | $document->documentStorage = new DocumentStorage($repository); |
||
168 | } |
||
169 | return $document; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return the result of the query as Document |
||
174 | * @param $sql |
||
175 | * @return mixed |
||
176 | * @throws \Exception |
||
177 | */ |
||
178 | protected function fetchDocument($sql) |
||
179 | { |
||
180 | $stmt = $this->getDbStatement($sql); |
||
181 | return $stmt->fetchObject(Document::class); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param Repository $repository |
||
186 | * @param $path |
||
187 | * @return bool|Document |
||
188 | * @throws \Exception |
||
189 | */ |
||
190 | public function getDocumentContainerByPath(Repository $repository, $path) |
||
191 | { |
||
192 | $document = $this->getDocumentByPath($repository, $path, 'unpublished'); |
||
193 | if ($document === false) { |
||
194 | return false; |
||
195 | } |
||
196 | $slugLength = strlen($document->slug); |
||
197 | $containerPath = substr($path, 0, -$slugLength); |
||
198 | if ($containerPath === '/') { |
||
199 | return $this->getRootFolder(); |
||
200 | } |
||
201 | if (substr($containerPath, -1) === '/') { |
||
202 | $containerPath = substr($containerPath, 0, -1); |
||
203 | } |
||
204 | $containerFolder = $this->getDocumentByPath($repository, $containerPath, 'unpublished'); |
||
205 | return $containerFolder; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Create a (non-existent) root folder Document and return it |
||
210 | * @return Document |
||
211 | */ |
||
212 | protected function getRootFolder() |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Returns the count of all documents stored in the db |
||
222 | * |
||
223 | * @param string $state |
||
224 | * |
||
225 | * @return int |
||
226 | * @throws \Exception |
||
227 | */ |
||
228 | public function getTotalDocumentCount($state = 'published') |
||
229 | { |
||
230 | if (!in_array($state, Document::$DOCUMENT_STATES, true)) { |
||
231 | throw new \RuntimeException('Unsupported document state: ' . $state); |
||
232 | } |
||
233 | $db = $this->getContentDbHandle(); |
||
234 | $stmt = $db->query(' |
||
235 | SELECT count(*) |
||
236 | FROM documents_' . $state . ' |
||
237 | WHERE `type` != "folder" |
||
238 | '); |
||
239 | $result = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
240 | if (!is_array($result)) { |
||
241 | return 0; |
||
242 | } |
||
243 | return (int)current($result); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return array |
||
248 | * @throws \Exception |
||
249 | */ |
||
250 | public function getPublishedDocumentsNoFolders() |
||
251 | { |
||
252 | $db = $this->getContentDbHandle(); |
||
253 | $sql = ' |
||
254 | SELECT * |
||
255 | FROM documents_published |
||
256 | WHERE `type` != "folder" |
||
257 | '; |
||
258 | $stmt = $db->query($sql); |
||
259 | $result = $stmt->fetchAll(\PDO::FETCH_CLASS, Document::class); |
||
260 | if ($stmt === false || !$stmt->execute()) { |
||
261 | $errorInfo = $db->errorInfo(); |
||
262 | $errorMsg = $errorInfo[2]; |
||
263 | throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
||
264 | } |
||
265 | return $result; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @param $path |
||
270 | * @throws \Exception |
||
271 | */ |
||
272 | public function publishDocumentByPath($path) |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @param $path |
||
279 | * @throws \Exception |
||
280 | */ |
||
281 | public function unpublishDocumentByPath($path) |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param $path |
||
288 | * @param bool $publish |
||
289 | * @throws \Exception |
||
290 | */ |
||
291 | public function publishOrUnpublishDocumentByPath($path, $publish = true) |
||
313 | } |
||
314 | |||
315 | public function cleanPublishedDeletedDocuments() |
||
316 | { |
||
317 | $sql = ' DELETE FROM documents_published |
||
318 | WHERE documents_published.path IN ( |
||
319 | SELECT documents_published.path |
||
320 | FROM documents_published |
||
321 | LEFT JOIN documents_unpublished |
||
322 | ON documents_unpublished.path = documents_published.path |
||
323 | WHERE documents_unpublished.path IS NULL |
||
324 | )'; |
||
325 | $stmt = $this->getDbStatement($sql); |
||
326 | $stmt->execute(); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Save the document to the database |
||
331 | * |
||
332 | * @param Document $documentObject |
||
333 | * @param string $state |
||
334 | * |
||
335 | * @return bool |
||
336 | * @throws \Exception |
||
337 | */ |
||
338 | public function saveDocument($documentObject, $state = 'published') |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Delete the document from the database |
||
367 | * If it's a folder, also delete it's contents |
||
368 | * |
||
369 | * @param Repository $repository |
||
370 | * @param $path |
||
371 | * @throws \Exception |
||
372 | */ |
||
373 | public function deleteDocumentByPath(Repository $repository, $path) |
||
393 | } |
||
394 | } |
||
395 | } |
||
396 | } |
||
397 | |||
398 | |||
399 | } |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.