Passed
Push — develop ( 0194c7...d1d076 )
by Jens
03:10
created

Storage::getImages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
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\FilesStorage;
11
	use library\storage\storage\ImageSetStorage;
12
	use library\storage\storage\ImagesStorage;
13
	use library\storage\storage\SitemapStorage;
14
	use library\storage\storage\UsersStorage;
15
16
	/**
17
	 * Class JsonStorage
18
	 * @package library\storage
19
	 */
20
	class Storage
21
	{
22
		/**
23
		 * @var SitemapStorage
24
		 */
25
		protected $sitemap;
26
		/**
27
		 * @var ImagesStorage
28
		 */
29
		protected $images;
30
		/**
31
		 * @var ImageSetStorage
32
		 */
33
		protected $imageSet;
34
		/**
35
		 * @var FilesStorage
36
		 */
37
		protected $files;
38
		/**
39
		 * @var UsersStorage
40
		 */
41
		protected $users;
42
		/**
43
		 * @var String
44
		 */
45
		private $storageDir;
46
		/**
47
		 * @var Repository
48
		 */
49
		private $repository;
50
51
		/**
52
		 * JsonStorage constructor.
53
		 *
54
		 * @param string $storageDir
55
		 */
56
		public function __construct($storageDir)
57
		{
58
			$this->storageDir = $storageDir;
59
			$this->config();
60
		}
61
62
		/**
63
		 * Retrieve the data from the storagepath
64
		 * so it can be interacted with
65
		 *
66
		 * @throws \Exception
67
		 */
68
		private function config()
69
		{
70
			$storagePath = __DIR__ . '/../../' . $this->storageDir;
71
			if (realpath($storagePath) === false) {
72
				initFramework();
73
				if (Repository::create($storagePath)) {
74
					$repository = new Repository($storagePath);
75
					$repository->init();
76
					$this->repository = $repository;
77
				} else {
78
					throw new \Exception('Could not create repository directory: ' . $storagePath);
79
				}
80
			} else {
81
				$this->repository = new Repository($storagePath);
82
			}
83
84
		}
85
86
87
		/**
88
		 * Get user by username
89
		 *
90
		 * @param $username
91
		 *
92
		 * @return array
93
		 */
94 View Code Duplication
		public function getUserByUsername($username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
95
		{
96
			$return = array();
97
98
			$users = $this->repository->users;
99
			foreach ($users as $user) {
100
				if ($user->username == $username) {
101
					$return = $user;
102
					break;
103
				}
104
			}
105
106
			return $return;
107
		}
108
109
		/**
110
		 * @return \library\storage\storage\UsersStorage
111
		 */
112
		public function getUsers()
113
		{
114
			if (!$this->users instanceof UsersStorage) {
115
				$this->users = new UsersStorage($this->repository);
116
			}
117
			return $this->users;
118
		}
119
120
		/*
121
		 *
122
		 * Documents
123
		 *
124
		 */
125
		/**
126
		 * Get documents
127
		 *
128
		 * @return array
129
		 */
130
		public function getDocuments()
131
		{
132
			return $this->repository->getDocuments();
133
		}
134
135
		public function getTotalDocumentCount()
136
		{
137
			return $this->repository->getTotalDocumentCount();
138
		}
139
140
		/**
141
		 * @param string $slug
142
		 *
143
		 * @return mixed
144
		 * @throws \Exception
145
		 */
146
		public function getDocumentBySlug($slug)
147
		{
148
			$path = '/' . $slug;
149
150
			return $this->repository->getDocumentByPath($path);
151
		}
152
153
		/**
154
		 * @param $postValues
155
		 */
156
		public function saveDocument($postValues)
157
		{
158
			$oldPath = '/' . $postValues['path'];
159
160
			$container = $this->getDocumentContainerByPath($oldPath);
161
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
162
			if ($container->path === '/') {
163
				$newPath = $container->path . $documentObject->slug;
164
			} else {
165
				$newPath = $container->path . '/' . $documentObject->slug;
166
			}
167
			$documentObject->path = $newPath;
168
			$this->repository->saveDocument($documentObject);
169
		}
170
171 View Code Duplication
		public function addDocument($postValues)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
172
		{
173
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
174
			if ($postValues['path'] === '/') {
175
				$documentObject->path = $postValues['path'] . $documentObject->slug;
176
			} else {
177
				$documentObject->path = $postValues['path'] . '/' . $documentObject->slug;
178
			}
179
180
			$this->repository->saveDocument($documentObject);
181
		}
182
183
		public function deleteDocumentBySlug($slug)
184
		{
185
			$path = '/' . $slug;
186
			$this->repository->deleteDocumentByPath($path);
187
		}
188
189
		/**
190
		 * Add new document in given path
191
		 *
192
		 * @param array $postValues
193
		 *
194
		 * @throws \Exception
195
		 */
196 View Code Duplication
		public function addDocumentFolder($postValues)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
197
		{
198
			$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
199
			if ($postValues['path'] === '/') {
200
				$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
201
			} else {
202
				$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
203
			}
204
			$this->repository->saveDocument($documentFolderObject);
205
		}
206
207
		/**
208
		 * Delete a folder by its compound slug
209
		 *
210
		 * @param $slug
211
		 *
212
		 * @throws \Exception
213
		 */
214
		public function deleteDocumentFolderBySlug($slug)
215
		{
216
			$path = '/' . $slug;
217
			$this->repository->deleteDocumentByPath($path);
218
		}
219
220
		/**
221
		 * Retrieve a folder by its compound slug
222
		 *
223
		 * @param $slug
224
		 *
225
		 * @return mixed
226
		 * @throws \Exception
227
		 */
228
		public function getDocumentFolderBySlug($slug)
229
		{
230
			$path = '/' . $slug;
231
232
			return $this->repository->getDocumentByPath($path);
233
		}
234
235
		/**
236
		 * Save changes to folder
237
		 *
238
		 * @param $postValues
239
		 *
240
		 * @throws \Exception
241
		 */
242
		public function saveDocumentFolder($postValues)
243
		{
244
			$this->addDocumentFolder($postValues);
245
		}
246
247
		/**
248
		 * Convert path to indeces
249
		 *
250
		 * @param $path
251
		 *
252
		 * @return array
253
		 * @throws \Exception
254
		 */
255
		private function getDocumentContainerByPath($path)
256
		{
257
			return $this->repository->getDocumentContainerByPath($path);
258
		}
259
260
		/**
261
		 * @return SitemapStorage
262
		 */
263
		public function getSitemap()
264
		{
265
			if (!$this->sitemap instanceof SitemapStorage) {
266
				$this->sitemap = new SitemapStorage($this->repository);
267
			}
268
			return $this->sitemap;
269
		}
270
271
		/*
272
		 *
273
		 * Images
274
		 *
275
		 */
276
		/**
277
		 * Get all images
278
		 *
279
		 * @return ImagesStorage
280
		 */
281
		public function getImages()
282
		{
283
			if (!$this->images instanceof ImagesStorage) {
284
				$this->images = new ImagesStorage($this->repository);
285
			}
286
			return $this->images;
287
		}
288
289
		/*
290
		 *
291
		 * Files
292
		 *
293
		 */
294
		/**
295
		 * Get all files
296
		 *
297
		 * @return FilesStorage
298
		 */
299
		public function getFiles()
300
		{
301
			if (!$this->files instanceof FilesStorage) {
302
				$this->files = new FilesStorage($this->repository);
303
			}
304
			return $this->files;
305
		}
306
307
		/**
308
		 * @return string
309
		 */
310
		public function getStorageDir()
311
		{
312
			return $this->storageDir;
313
		}
314
315
		public function getContentDbHandle()
316
		{
317
			return $this->repository->getContentDbHandle();
318
		}
319
320
		/*
321
		 * 
322
		 * Configuration
323
		 *
324
		 */
325
		/**
326
		 * @return array
327
		 */
328
		public function getDocumentTypes()
329
		{
330
			return $this->repository->documentTypes;
331
		}
332
333
		/**
334
		 * Add a document type from post values
335
		 *
336
		 * @param $postValues
337
		 *
338
		 * @throws \Exception
339
		 */
340
		public function addDocumentType($postValues)
341
		{
342
			$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues);
343
344
			$documentTypes = $this->repository->documentTypes;
345
			$documentTypes[] = $documentTypeObject;
346
			$this->repository->documentTypes = $documentTypes;
347
348
			$this->save();
349
		}
350
351
		/**
352
		 * Delete document type
353
		 *
354
		 * @param $slug
355
		 *
356
		 * @throws \Exception
357
		 */
358 View Code Duplication
		public function deleteDocumentTypeBySlug($slug)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
359
		{
360
			$documentTypes = $this->repository->documentTypes;
361
			foreach ($documentTypes as $key => $documentTypeObject) {
362
				if ($documentTypeObject->slug == $slug) {
363
					unset($documentTypes[$key]);
364
				}
365
			}
366
			$documentTypes = array_values($documentTypes);
367
			$this->repository->documentTypes = $documentTypes;
368
			$this->save();
369
		}
370
371
		/**
372
		 * Get document type by its slug
373
		 *
374
		 * @param      $slug
375
		 * @param bool $getBricks
376
		 *
377
		 * @return mixed
378
		 */
379
		public function getDocumentTypeBySlug($slug, $getBricks = false)
380
		{
381
			$documentTypes = $this->repository->documentTypes;
382
			foreach ($documentTypes as $documentType) {
383
				if ($documentType->slug == $slug) {
384
					if ($getBricks === true) {
385
						foreach ($documentType->bricks as $key => $brick) {
386
							$brickStructure = $this->getBrickBySlug($brick->brickSlug);
387
							$documentType->bricks[$key]->structure = $brickStructure;
388
						}
389
						foreach ($documentType->dynamicBricks as $key => $brickSlug) {
390
							$brickStructure = $this->getBrickBySlug($brickSlug);
391
							$documentType->dynamicBricks[$key] = $brickStructure;
392
						}
393
					}
394
395
					return $documentType;
396
				}
397
			}
398
399
			return null;
400
		}
401
402
		/**
403
		 * Save changes to a document type
404
		 *
405
		 * @param $slug
406
		 * @param $postValues
407
		 *
408
		 * @throws \Exception
409
		 */
410 View Code Duplication
		public function saveDocumentType($slug, $postValues)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
411
		{
412
			$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues);
413
414
			$documentTypes = $this->repository->documentTypes;
415
			foreach ($documentTypes as $key => $documentType) {
416
				if ($documentType->slug == $slug) {
417
					$documentTypes[$key] = $documentTypeObject;
418
				}
419
			}
420
			$this->repository->documentTypes = $documentTypes;
421
			$this->save();
422
		}
423
424
		/*
425
		 *
426
		 * Bricks
427
		 *
428
		 */
429
		/**
430
		 * @return array
431
		 */
432
		public function getBricks()
433
		{
434
			return $this->repository->bricks;
435
		}
436
437
		/**
438
		 * Add a brick
439
		 *
440
		 * @param $postValues
441
		 *
442
		 * @throws \Exception
443
		 */
444
		public function addBrick($postValues)
445
		{
446
			$brickObject = BrickFactory::createBrickFromPostValues($postValues);
447
448
			$bricks = $this->repository->bricks;
449
			$bricks[] = $brickObject;
450
			$this->repository->bricks = $bricks;
451
452
			$this->save();
453
		}
454
455
		/**
456
		 * Get a brick by its slug
457
		 *
458
		 * @param $slug
459
		 *
460
		 * @return \stdClass
461
		 */
462
		public function getBrickBySlug($slug)
463
		{
464
			$bricks = $this->repository->bricks;
465
			foreach ($bricks as $brick) {
466
				if ($brick->slug == $slug) {
467
					return $brick;
468
				}
469
			}
470
471
			return null;
472
		}
473
474
		/**
475
		 * Save changes to a brick
476
		 *
477
		 * @param $slug
478
		 * @param $postValues
479
		 *
480
		 * @throws \Exception
481
		 */
482
		public function saveBrick($slug, $postValues)
483
		{
484
			$brickObject = BrickFactory::createBrickFromPostValues($postValues);
485
486
			$bricks = $this->repository->bricks;
487
			foreach ($bricks as $key => $brick) {
488
				if ($brick->slug == $slug) {
489
					$bricks[$key] = $brickObject;
490
				}
491
			}
492
			$this->repository->bricks = $bricks;
493
			$this->save();
494
		}
495
496
		/**
497
		 * Delete a brick by its slug
498
		 *
499
		 * @param $slug
500
		 *
501
		 * @throws \Exception
502
		 */
503
		public function deleteBrickBySlug($slug)
504
		{
505
			$bricks = $this->repository->bricks;
506
			foreach ($bricks as $key => $brickObject) {
507
				if ($brickObject->slug == $slug) {
508
					unset($bricks[$key]);
509
				}
510
			}
511
512
			$bricks = array_values($bricks);
513
			$this->repository->bricks = $bricks;
514
			$this->save();
515
		}
516
517
		/*
518
		 * 
519
		 * Misc
520
		 *
521
		 */
522
		/**
523
		 * Save changes made to the repository
524
		 *
525
		 * @throws \Exception
526
		 */
527
		private function save()
528
		{
529
			$this->repository->save();
530
		}
531
532
		/*
533
		 *
534
		 * Image Set
535
		 *
536
		 */
537
538
		/**
539
		 * Get the image set
540
		 *
541
		 * @return ImageSetStorage
542
		 */
543 View Code Duplication
		public function getImageSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
544
		{
545
			if (!$this->imageSet instanceof ImageSetStorage) {
546
				$this->imageSet = new ImageSetStorage($this->repository);
547
			}
548
			return $this->imageSet;
549
		}
550
551
		/**
552
		 * @return array
553
		 */
554
		public function getApplicationComponents()
555
		{
556
			return $this->repository->applicationComponents;
557
		}
558
559
		public function addApplicationComponent($postValues)
560
		{
561
			$applicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
562
			$applicationComponents = $this->repository->applicationComponents;
563
			$applicationComponents[] = $applicationComponent;
564
			$this->repository->applicationComponents = $applicationComponents;
565
566
			$this->save();
567
		}
568
569
		public function getApplicationComponentBySlug($slug)
570
		{
571
			$applicationComponents = $this->getApplicationComponents();
572
			foreach ($applicationComponents as $applicationComponent) {
573
				if ($applicationComponent->slug == $slug) {
574
					return $applicationComponent;
575
				}
576
			}
577
578
			return null;
579
		}
580
581
		public function saveApplicationComponent($slug, $postValues)
582
		{
583
			$newApplicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
584
585
			$applicationComponents = $this->getApplicationComponents();
586
			foreach ($applicationComponents as $key => $applicationComponent) {
587
				if ($applicationComponent->slug == $slug) {
588
					$applicationComponents[$key] = $newApplicationComponent;
589
				}
590
			}
591
			$this->repository->applicationComponents = $applicationComponents;
592
			$this->save();
593
		}
594
595
		public function deleteApplicationComponentBySlug($slug)
596
		{
597
			$applicationComponents = $this->getApplicationComponents();
598
			foreach ($applicationComponents as $key => $applicationComponent) {
599
				if ($applicationComponent->slug == $slug) {
600
					unset($applicationComponents[$key]);
601
				}
602
			}
603
			$applicationComponents = array_values($applicationComponents);
604
			$this->repository->applicationComponents = $applicationComponents;
605
			$this->save();
606
		}
607
608
	}
609
}