Total Complexity | 42 |
Total Lines | 386 |
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) |
||
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 | return $documents; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Get all documents and folders in a certain path |
||
116 | * |
||
117 | * @param Repository $repository |
||
118 | * @param $folderPath |
||
119 | * @param string $state |
||
120 | * @return array |
||
121 | * @throws \Exception |
||
122 | */ |
||
123 | public function getDocumentsByPath(Repository $repository, $folderPath, $state = 'published') |
||
124 | { |
||
125 | if (!in_array($state, Document::$DOCUMENT_STATES, true)) { |
||
126 | throw new \RuntimeException('Unsupported document state: ' . $state); |
||
127 | } |
||
128 | $db = $this->getContentDbHandle(); |
||
129 | $folderPathWithWildcard = $folderPath . '%'; |
||
130 | |||
131 | $sql = 'SELECT * |
||
132 | FROM documents_' . $state . ' |
||
133 | WHERE `path` LIKE ' . $db->quote($folderPathWithWildcard) . ' |
||
134 | AND substr(`path`, ' . (strlen($folderPath) + 1) . ') NOT LIKE "%/%" |
||
135 | AND path != ' . $db->quote($folderPath) . ' |
||
136 | ORDER BY `type` DESC, `path` ASC'; |
||
137 | $stmt = $this->getDbStatement($sql); |
||
138 | |||
139 | $documents = $stmt->fetchAll(\PDO::FETCH_CLASS, Document::class); |
||
140 | foreach ($documents as $key => $document) { |
||
141 | $documents = $this->setAssetsToDocumentFolders($repository, $document, $db, $documents, $key); |
||
142 | } |
||
143 | return $documents; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param \CloudControl\Cms\storage\Repository $repository |
||
148 | * @param $path |
||
149 | * @param string $state |
||
150 | * @return bool|Document |
||
151 | * @throws \Exception |
||
152 | */ |
||
153 | public function getDocumentByPath(Repository $repository, $path, $state = 'published') |
||
154 | { |
||
155 | if (!in_array($state, Document::$DOCUMENT_STATES, true)) { |
||
156 | throw new \RuntimeException('Unsupported document state: ' . $state); |
||
157 | } |
||
158 | if ($path === '/') { |
||
159 | return $this->getRootFolder($repository); |
||
160 | } |
||
161 | $db = $this->getContentDbHandle(); |
||
162 | $document = $this->fetchDocument(' |
||
163 | SELECT * |
||
164 | FROM documents_' . $state . ' |
||
165 | WHERE path = ' . $db->quote($path) . ' |
||
166 | '); |
||
167 | if ($document instanceof Document && $document->type === 'folder') { |
||
168 | $document->dbHandle = $db; |
||
169 | $document->documentStorage = new DocumentStorage($repository); |
||
170 | } |
||
171 | return $document; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Return the result of the query as Document |
||
176 | * @param $sql |
||
177 | * @return mixed |
||
178 | * @throws \Exception |
||
179 | */ |
||
180 | protected function fetchDocument($sql) |
||
181 | { |
||
182 | $stmt = $this->getDbStatement($sql); |
||
183 | return $stmt->fetchObject(Document::class); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param Repository $repository |
||
188 | * @param $path |
||
189 | * @return bool|Document |
||
190 | * @throws \Exception |
||
191 | */ |
||
192 | public function getDocumentContainerByPath(Repository $repository, $path) |
||
193 | { |
||
194 | $document = $this->getDocumentByPath($repository, $path, 'unpublished'); |
||
195 | if ($document === false) { |
||
196 | return false; |
||
197 | } |
||
198 | $slugLength = strlen($document->slug); |
||
199 | $containerPath = substr($path, 0, -$slugLength); |
||
200 | if ($containerPath === '/') { |
||
201 | return $this->getRootFolder($repository); |
||
202 | } |
||
203 | if (substr($containerPath, -1) === '/') { |
||
204 | $containerPath = substr($containerPath, 0, -1); |
||
205 | } |
||
206 | $containerFolder = $this->getDocumentByPath($repository, $containerPath, 'unpublished'); |
||
207 | return $containerFolder; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Create a (non-existent) root folder Document and return it |
||
212 | * @param Repository $repository |
||
213 | * @return Document |
||
214 | */ |
||
215 | protected function getRootFolder(Repository $repository) |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Returns the count of all documents stored in the db |
||
227 | * |
||
228 | * @param string $state |
||
229 | * |
||
230 | * @return int |
||
231 | * @throws \Exception |
||
232 | */ |
||
233 | public function getTotalDocumentCount($state = 'published') |
||
234 | { |
||
235 | if (!in_array($state, Document::$DOCUMENT_STATES, true)) { |
||
236 | throw new \RuntimeException('Unsupported document state: ' . $state); |
||
237 | } |
||
238 | $db = $this->getContentDbHandle(); |
||
239 | $stmt = $db->query(' |
||
240 | SELECT count(*) |
||
241 | FROM documents_' . $state . ' |
||
242 | WHERE `type` != "folder" |
||
243 | '); |
||
244 | $result = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
245 | if (!is_array($result)) { |
||
246 | return 0; |
||
247 | } |
||
248 | return (int)current($result); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @return array |
||
253 | * @throws \Exception |
||
254 | */ |
||
255 | public function getPublishedDocumentsNoFolders() |
||
256 | { |
||
257 | $db = $this->getContentDbHandle(); |
||
258 | $sql = ' |
||
259 | SELECT * |
||
260 | FROM documents_published |
||
261 | WHERE `type` != "folder" |
||
262 | '; |
||
263 | $stmt = $db->query($sql); |
||
264 | $result = $stmt->fetchAll(\PDO::FETCH_CLASS, Document::class); |
||
265 | if ($stmt === false || !$stmt->execute()) { |
||
266 | $errorInfo = $db->errorInfo(); |
||
267 | $errorMsg = $errorInfo[2]; |
||
268 | throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
||
269 | } |
||
270 | return $result; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param $path |
||
275 | * @throws \Exception |
||
276 | */ |
||
277 | public function publishDocumentByPath($path) |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @param $path |
||
284 | * @throws \Exception |
||
285 | */ |
||
286 | public function unpublishDocumentByPath($path) |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param $path |
||
293 | * @param bool $publish |
||
294 | * @throws \Exception |
||
295 | */ |
||
296 | public function publishOrUnpublishDocumentByPath($path, $publish = true) |
||
318 | } |
||
319 | |||
320 | public function cleanPublishedDeletedDocuments() |
||
321 | { |
||
322 | $sql = ' DELETE FROM documents_published |
||
323 | WHERE documents_published.path IN ( |
||
324 | SELECT documents_published.path |
||
325 | FROM documents_published |
||
326 | LEFT JOIN documents_unpublished |
||
327 | ON documents_unpublished.path = documents_published.path |
||
328 | WHERE documents_unpublished.path IS NULL |
||
329 | )'; |
||
330 | $stmt = $this->getDbStatement($sql); |
||
331 | $stmt->execute(); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Save the document to the database |
||
336 | * |
||
337 | * @param Document $documentObject |
||
338 | * @param string $state |
||
339 | * |
||
340 | * @return bool |
||
341 | * @throws \Exception |
||
342 | */ |
||
343 | public function saveDocument($documentObject, $state = 'published') |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Delete the document from the database |
||
372 | * If it's a folder, also delete it's contents |
||
373 | * |
||
374 | * @param Repository $repository |
||
375 | * @param $path |
||
376 | * @throws \Exception |
||
377 | */ |
||
378 | public function deleteDocumentByPath(Repository $repository, $path) |
||
398 | } |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
404 | } |
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.