1
|
|
|
<?php |
2
|
|
|
namespace library\storage { |
3
|
|
|
|
4
|
|
|
use library\storage\factories\ApplicationComponentFactory; |
5
|
|
|
use library\storage\factories\BrickFactory; |
6
|
|
|
use library\storage\factories\DocumentFolderFactory; |
7
|
|
|
use library\storage\factories\DocumentTypeFactory; |
8
|
|
|
use library\storage\factories\ImageSetFactory; |
9
|
|
|
use library\storage\factories\UserFactory; |
10
|
|
|
use library\storage\storage\DocumentTypesStorage; |
11
|
|
|
use library\storage\storage\FilesStorage; |
12
|
|
|
use library\storage\storage\ImageSetStorage; |
13
|
|
|
use library\storage\storage\ImagesStorage; |
14
|
|
|
use library\storage\storage\SitemapStorage; |
15
|
|
|
use library\storage\storage\UsersStorage; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class JsonStorage |
19
|
|
|
* @package library\storage |
20
|
|
|
*/ |
21
|
|
|
class Storage |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var SitemapStorage |
25
|
|
|
*/ |
26
|
|
|
protected $sitemap; |
27
|
|
|
/** |
28
|
|
|
* @var ImagesStorage |
29
|
|
|
*/ |
30
|
|
|
protected $images; |
31
|
|
|
/** |
32
|
|
|
* @var ImageSetStorage |
33
|
|
|
*/ |
34
|
|
|
protected $imageSet; |
35
|
|
|
/** |
36
|
|
|
* @var FilesStorage |
37
|
|
|
*/ |
38
|
|
|
protected $files; |
39
|
|
|
/** |
40
|
|
|
* @var UsersStorage |
41
|
|
|
*/ |
42
|
|
|
protected $users; |
43
|
|
|
/** |
44
|
|
|
* @var DocumentTypesStorage |
45
|
|
|
*/ |
46
|
|
|
protected $documentTypes; |
47
|
|
|
/** |
48
|
|
|
* @var String |
49
|
|
|
*/ |
50
|
|
|
private $storageDir; |
51
|
|
|
/** |
52
|
|
|
* @var Repository |
53
|
|
|
*/ |
54
|
|
|
private $repository; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* JsonStorage constructor. |
58
|
|
|
* |
59
|
|
|
* @param string $storageDir |
60
|
|
|
*/ |
61
|
|
|
public function __construct($storageDir) |
62
|
|
|
{ |
63
|
|
|
$this->storageDir = $storageDir; |
64
|
|
|
$this->config(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Retrieve the data from the storagepath |
69
|
|
|
* so it can be interacted with |
70
|
|
|
* |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
|
|
private function config() |
74
|
|
|
{ |
75
|
|
|
$storagePath = __DIR__ . '/../../' . $this->storageDir; |
76
|
|
|
if (realpath($storagePath) === false) { |
77
|
|
|
initFramework(); |
78
|
|
|
if (Repository::create($storagePath)) { |
79
|
|
|
$repository = new Repository($storagePath); |
80
|
|
|
$repository->init(); |
81
|
|
|
$this->repository = $repository; |
82
|
|
|
} else { |
83
|
|
|
throw new \Exception('Could not create repository directory: ' . $storagePath); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
$this->repository = new Repository($storagePath); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return \library\storage\storage\UsersStorage |
93
|
|
|
*/ |
94
|
|
|
public function getUsers() |
95
|
|
|
{ |
96
|
|
|
if (!$this->users instanceof UsersStorage) { |
97
|
|
|
$this->users = new UsersStorage($this->repository); |
98
|
|
|
} |
99
|
|
|
return $this->users; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/* |
103
|
|
|
* |
104
|
|
|
* Documents |
105
|
|
|
* |
106
|
|
|
*/ |
107
|
|
|
/** |
108
|
|
|
* Get documents |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getDocuments() |
113
|
|
|
{ |
114
|
|
|
return $this->repository->getDocuments(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getTotalDocumentCount() |
118
|
|
|
{ |
119
|
|
|
return $this->repository->getTotalDocumentCount(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $slug |
124
|
|
|
* |
125
|
|
|
* @return mixed |
126
|
|
|
* @throws \Exception |
127
|
|
|
*/ |
128
|
|
|
public function getDocumentBySlug($slug) |
129
|
|
|
{ |
130
|
|
|
$path = '/' . $slug; |
131
|
|
|
|
132
|
|
|
return $this->repository->getDocumentByPath($path); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $postValues |
137
|
|
|
*/ |
138
|
|
|
public function saveDocument($postValues) |
139
|
|
|
{ |
140
|
|
|
$oldPath = '/' . $postValues['path']; |
141
|
|
|
|
142
|
|
|
$container = $this->getDocumentContainerByPath($oldPath); |
143
|
|
|
$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this); |
144
|
|
|
if ($container->path === '/') { |
145
|
|
|
$newPath = $container->path . $documentObject->slug; |
146
|
|
|
} else { |
147
|
|
|
$newPath = $container->path . '/' . $documentObject->slug; |
148
|
|
|
} |
149
|
|
|
$documentObject->path = $newPath; |
150
|
|
|
$this->repository->saveDocument($documentObject); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
View Code Duplication |
public function addDocument($postValues) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this); |
156
|
|
|
if ($postValues['path'] === '/') { |
157
|
|
|
$documentObject->path = $postValues['path'] . $documentObject->slug; |
158
|
|
|
} else { |
159
|
|
|
$documentObject->path = $postValues['path'] . '/' . $documentObject->slug; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->repository->saveDocument($documentObject); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function deleteDocumentBySlug($slug) |
166
|
|
|
{ |
167
|
|
|
$path = '/' . $slug; |
168
|
|
|
$this->repository->deleteDocumentByPath($path); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Add new document in given path |
173
|
|
|
* |
174
|
|
|
* @param array $postValues |
175
|
|
|
* |
176
|
|
|
* @throws \Exception |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
public function addDocumentFolder($postValues) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues); |
181
|
|
|
if ($postValues['path'] === '/') { |
182
|
|
|
$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug; |
183
|
|
|
} else { |
184
|
|
|
$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug; |
185
|
|
|
} |
186
|
|
|
$this->repository->saveDocument($documentFolderObject); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Delete a folder by its compound slug |
191
|
|
|
* |
192
|
|
|
* @param $slug |
193
|
|
|
* |
194
|
|
|
* @throws \Exception |
195
|
|
|
*/ |
196
|
|
|
public function deleteDocumentFolderBySlug($slug) |
197
|
|
|
{ |
198
|
|
|
$path = '/' . $slug; |
199
|
|
|
$this->repository->deleteDocumentByPath($path); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Retrieve a folder by its compound slug |
204
|
|
|
* |
205
|
|
|
* @param $slug |
206
|
|
|
* |
207
|
|
|
* @return mixed |
208
|
|
|
* @throws \Exception |
209
|
|
|
*/ |
210
|
|
|
public function getDocumentFolderBySlug($slug) |
211
|
|
|
{ |
212
|
|
|
$path = '/' . $slug; |
213
|
|
|
|
214
|
|
|
return $this->repository->getDocumentByPath($path); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Save changes to folder |
219
|
|
|
* |
220
|
|
|
* @param $postValues |
221
|
|
|
* |
222
|
|
|
* @throws \Exception |
223
|
|
|
*/ |
224
|
|
|
public function saveDocumentFolder($postValues) |
225
|
|
|
{ |
226
|
|
|
$this->addDocumentFolder($postValues); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Convert path to indeces |
231
|
|
|
* |
232
|
|
|
* @param $path |
233
|
|
|
* |
234
|
|
|
* @return array |
235
|
|
|
* @throws \Exception |
236
|
|
|
*/ |
237
|
|
|
private function getDocumentContainerByPath($path) |
238
|
|
|
{ |
239
|
|
|
return $this->repository->getDocumentContainerByPath($path); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return SitemapStorage |
244
|
|
|
*/ |
245
|
|
|
public function getSitemap() |
246
|
|
|
{ |
247
|
|
|
if (!$this->sitemap instanceof SitemapStorage) { |
248
|
|
|
$this->sitemap = new SitemapStorage($this->repository); |
249
|
|
|
} |
250
|
|
|
return $this->sitemap; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Get all images |
255
|
|
|
* |
256
|
|
|
* @return ImagesStorage |
257
|
|
|
*/ |
258
|
|
|
public function getImages() |
259
|
|
|
{ |
260
|
|
|
if (!$this->images instanceof ImagesStorage) { |
261
|
|
|
$this->images = new ImagesStorage($this->repository); |
262
|
|
|
} |
263
|
|
|
return $this->images; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get all files |
268
|
|
|
* |
269
|
|
|
* @return FilesStorage |
270
|
|
|
*/ |
271
|
|
|
public function getFiles() |
272
|
|
|
{ |
273
|
|
|
if (!$this->files instanceof FilesStorage) { |
274
|
|
|
$this->files = new FilesStorage($this->repository); |
275
|
|
|
} |
276
|
|
|
return $this->files; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return string |
281
|
|
|
*/ |
282
|
|
|
public function getStorageDir() |
283
|
|
|
{ |
284
|
|
|
return $this->storageDir; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function getContentDbHandle() |
288
|
|
|
{ |
289
|
|
|
return $this->repository->getContentDbHandle(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return DocumentTypesStorage |
294
|
|
|
*/ |
295
|
|
|
public function getDocumentTypes() |
296
|
|
|
{ |
297
|
|
|
if (!$this->documentTypes instanceof DocumentTypesStorage) { |
298
|
|
|
$this->documentTypes = new DocumentTypesStorage($this->repository); |
299
|
|
|
} |
300
|
|
|
return $this->documentTypes; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/* |
304
|
|
|
* |
305
|
|
|
* Bricks |
306
|
|
|
* |
307
|
|
|
*/ |
308
|
|
|
/** |
309
|
|
|
* @return array |
310
|
|
|
*/ |
311
|
|
|
public function getBricks() |
312
|
|
|
{ |
313
|
|
|
return $this->repository->bricks; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Add a brick |
318
|
|
|
* |
319
|
|
|
* @param $postValues |
320
|
|
|
* |
321
|
|
|
* @throws \Exception |
322
|
|
|
*/ |
323
|
|
|
public function addBrick($postValues) |
324
|
|
|
{ |
325
|
|
|
$brickObject = BrickFactory::createBrickFromPostValues($postValues); |
326
|
|
|
|
327
|
|
|
$bricks = $this->repository->bricks; |
328
|
|
|
$bricks[] = $brickObject; |
329
|
|
|
$this->repository->bricks = $bricks; |
330
|
|
|
|
331
|
|
|
$this->save(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Get a brick by its slug |
336
|
|
|
* |
337
|
|
|
* @param $slug |
338
|
|
|
* |
339
|
|
|
* @return \stdClass |
340
|
|
|
*/ |
341
|
|
|
public function getBrickBySlug($slug) |
342
|
|
|
{ |
343
|
|
|
$bricks = $this->repository->bricks; |
344
|
|
|
foreach ($bricks as $brick) { |
345
|
|
|
if ($brick->slug == $slug) { |
346
|
|
|
return $brick; |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return null; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Save changes to a brick |
355
|
|
|
* |
356
|
|
|
* @param $slug |
357
|
|
|
* @param $postValues |
358
|
|
|
* |
359
|
|
|
* @throws \Exception |
360
|
|
|
*/ |
361
|
|
|
public function saveBrick($slug, $postValues) |
362
|
|
|
{ |
363
|
|
|
$brickObject = BrickFactory::createBrickFromPostValues($postValues); |
364
|
|
|
|
365
|
|
|
$bricks = $this->repository->bricks; |
366
|
|
|
foreach ($bricks as $key => $brick) { |
367
|
|
|
if ($brick->slug == $slug) { |
368
|
|
|
$bricks[$key] = $brickObject; |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
$this->repository->bricks = $bricks; |
372
|
|
|
$this->save(); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Delete a brick by its slug |
377
|
|
|
* |
378
|
|
|
* @param $slug |
379
|
|
|
* |
380
|
|
|
* @throws \Exception |
381
|
|
|
*/ |
382
|
|
|
public function deleteBrickBySlug($slug) |
383
|
|
|
{ |
384
|
|
|
$bricks = $this->repository->bricks; |
385
|
|
|
foreach ($bricks as $key => $brickObject) { |
386
|
|
|
if ($brickObject->slug == $slug) { |
387
|
|
|
unset($bricks[$key]); |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$bricks = array_values($bricks); |
392
|
|
|
$this->repository->bricks = $bricks; |
393
|
|
|
$this->save(); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/* |
397
|
|
|
* |
398
|
|
|
* Misc |
399
|
|
|
* |
400
|
|
|
*/ |
401
|
|
|
/** |
402
|
|
|
* Save changes made to the repository |
403
|
|
|
* |
404
|
|
|
* @throws \Exception |
405
|
|
|
*/ |
406
|
|
|
private function save() |
407
|
|
|
{ |
408
|
|
|
$this->repository->save(); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/* |
412
|
|
|
* |
413
|
|
|
* Image Set |
414
|
|
|
* |
415
|
|
|
*/ |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Get the image set |
419
|
|
|
* |
420
|
|
|
* @return ImageSetStorage |
421
|
|
|
*/ |
422
|
|
View Code Duplication |
public function getImageSet() |
|
|
|
|
423
|
|
|
{ |
424
|
|
|
if (!$this->imageSet instanceof ImageSetStorage) { |
425
|
|
|
$this->imageSet = new ImageSetStorage($this->repository); |
426
|
|
|
} |
427
|
|
|
return $this->imageSet; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* @return array |
432
|
|
|
*/ |
433
|
|
|
public function getApplicationComponents() |
434
|
|
|
{ |
435
|
|
|
return $this->repository->applicationComponents; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
public function addApplicationComponent($postValues) |
439
|
|
|
{ |
440
|
|
|
$applicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues); |
441
|
|
|
$applicationComponents = $this->repository->applicationComponents; |
442
|
|
|
$applicationComponents[] = $applicationComponent; |
443
|
|
|
$this->repository->applicationComponents = $applicationComponents; |
444
|
|
|
|
445
|
|
|
$this->save(); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
public function getApplicationComponentBySlug($slug) |
449
|
|
|
{ |
450
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
451
|
|
|
foreach ($applicationComponents as $applicationComponent) { |
452
|
|
|
if ($applicationComponent->slug == $slug) { |
453
|
|
|
return $applicationComponent; |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
return null; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
public function saveApplicationComponent($slug, $postValues) |
461
|
|
|
{ |
462
|
|
|
$newApplicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues); |
463
|
|
|
|
464
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
465
|
|
|
foreach ($applicationComponents as $key => $applicationComponent) { |
466
|
|
|
if ($applicationComponent->slug == $slug) { |
467
|
|
|
$applicationComponents[$key] = $newApplicationComponent; |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
$this->repository->applicationComponents = $applicationComponents; |
471
|
|
|
$this->save(); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
public function deleteApplicationComponentBySlug($slug) |
475
|
|
|
{ |
476
|
|
|
$applicationComponents = $this->getApplicationComponents(); |
477
|
|
|
foreach ($applicationComponents as $key => $applicationComponent) { |
478
|
|
|
if ($applicationComponent->slug == $slug) { |
479
|
|
|
unset($applicationComponents[$key]); |
480
|
|
|
} |
481
|
|
|
} |
482
|
|
|
$applicationComponents = array_values($applicationComponents); |
483
|
|
|
$this->repository->applicationComponents = $applicationComponents; |
484
|
|
|
$this->save(); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
} |
488
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.