Passed
Push — develop ( eeeda6...0194c7 )
by Jens
03:21
created

Storage::getDocumentBySlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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
15
	/**
16
	 * Class JsonStorage
17
	 * @package library\storage
18
	 */
19
	class Storage
20
	{
21
		/**
22
		 * @var SitemapStorage
23
		 */
24
		protected $sitemap;
25
		/**
26
		 * @var ImagesStorage
27
		 */
28
		protected $images;
29
		/**
30
		 * @var ImageSetStorage
31
		 */
32
		protected $imageSet;
33
		/**
34
		 * @var FilesStorage
35
		 */
36
		protected $files;
37
		/**
38
		 * @var String
39
		 */
40
		private $storageDir;
41
		/**
42
		 * @var Repository
43
		 */
44
		private $repository;
45
46
		/**
47
		 * JsonStorage constructor.
48
		 *
49
		 * @param string $storageDir
50
		 */
51
		public function __construct($storageDir)
52
		{
53
			$this->storageDir = $storageDir;
54
			$this->config();
55
		}
56
57
		/**
58
		 * Retrieve the data from the storagepath
59
		 * so it can be interacted with
60
		 *
61
		 * @throws \Exception
62
		 */
63
		private function config()
64
		{
65
			$storagePath = __DIR__ . '/../../' . $this->storageDir;
66
			if (realpath($storagePath) === false) {
67
				initFramework();
68
				if (Repository::create($storagePath)) {
69
					$repository = new Repository($storagePath);
70
					$repository->init();
71
					$this->repository = $repository;
72
				} else {
73
					throw new \Exception('Could not create repository directory: ' . $storagePath);
74
				}
75
			} else {
76
				$this->repository = new Repository($storagePath);
77
			}
78
79
		}
80
81
82
		/**
83
		 * Get user by username
84
		 *
85
		 * @param $username
86
		 *
87
		 * @return array
88
		 */
89 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...
90
		{
91
			$return = array();
92
93
			$users = $this->repository->users;
94
			foreach ($users as $user) {
95
				if ($user->username == $username) {
96
					$return = $user;
97
					break;
98
				}
99
			}
100
101
			return $return;
102
		}
103
104
		/**
105
		 * Get user by slug
106
		 *
107
		 * @param $slug
108
		 *
109
		 * @return array
110
		 */
111 View Code Duplication
		public function getUserBySlug($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...
112
		{
113
			$return = array();
114
115
			$users = $this->repository->users;
116
			foreach ($users as $user) {
117
				if ($user->slug == $slug) {
118
					$return = $user;
119
					break;
120
				}
121
			}
122
123
			return $return;
124
		}
125
126
		/**
127
		 * Get all users
128
		 *
129
		 * @return mixed
130
		 */
131
		public function getUsers()
132
		{
133
			return $this->repository->users;
134
		}
135
136
		/**
137
		 * Save user
138
		 *
139
		 * @param $slug
140
		 * @param $postValues
141
		 *
142
		 * @throws \Exception
143
		 */
144
		public function saveUser($slug, $postValues)
145
		{
146
			$userObj = UserFactory::createUserFromPostValues($postValues);
147
			if ($userObj->slug != $slug) {
148
				// If the username changed, check for duplicates
149
				$doesItExist = $this->getUserBySlug($userObj->slug);
150
				if (!empty($doesItExist)) {
151
					throw new \Exception('Trying to rename user to existing username');
152
				}
153
			}
154
			$users = $this->getUsers();
155
			foreach ($users as $key => $user) {
156
				if ($user->slug == $slug) {
157
					$users[$key] = $userObj;
158
				}
159
			}
160
			$this->repository->users = $users;
161
			$this->save();
162
		}
163
164
		/**
165
		 * Add user
166
		 *
167
		 * @param $postValues
168
		 *
169
		 * @throws \Exception
170
		 */
171
		public function addUser($postValues)
172
		{
173
			$userObj = UserFactory::createUserFromPostValues($postValues);
174
175
			$doesItExist = $this->getUserBySlug($userObj->slug);
176
			if (!empty($doesItExist)) {
177
				throw new \Exception('Trying to add username that already exists.');
178
			}
179
			$users = $this->repository->users;
180
			$users[] = $userObj;
181
			$this->repository->users = $users;
182
			$this->save();
183
		}
184
185
		/**
186
		 * Delete user by slug
187
		 *
188
		 * @param $slug
189
		 *
190
		 * @throws \Exception
191
		 */
192
		public function deleteUserBySlug($slug)
193
		{
194
			$userToDelete = $this->getUserBySlug($slug);
195
			if (empty($userToDelete)) {
196
				throw new \Exception('Trying to delete a user that doesn\'t exist.');
197
			}
198
			$users = $this->getUsers();
199
			foreach ($users as $key => $user) {
200
				if ($user->slug == $userToDelete->slug) {
201
					unset($users[$key]);
202
					$this->repository->users = array_values($users);
203
				}
204
			}
205
			$this->save();
206
		}
207
208
		/*
209
		 *
210
		 * Documents
211
		 *
212
		 */
213
		/**
214
		 * Get documents
215
		 *
216
		 * @return array
217
		 */
218
		public function getDocuments()
219
		{
220
			return $this->repository->getDocuments();
221
		}
222
223
		public function getTotalDocumentCount()
224
		{
225
			return $this->repository->getTotalDocumentCount();
226
		}
227
228
		/**
229
		 * @param string $slug
230
		 *
231
		 * @return mixed
232
		 * @throws \Exception
233
		 */
234
		public function getDocumentBySlug($slug)
235
		{
236
			$path = '/' . $slug;
237
238
			return $this->repository->getDocumentByPath($path);
239
		}
240
241
		/**
242
		 * @param $postValues
243
		 */
244
		public function saveDocument($postValues)
245
		{
246
			$oldPath = '/' . $postValues['path'];
247
248
			$container = $this->getDocumentContainerByPath($oldPath);
249
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
250
			if ($container->path === '/') {
251
				$newPath = $container->path . $documentObject->slug;
252
			} else {
253
				$newPath = $container->path . '/' . $documentObject->slug;
254
			}
255
			$documentObject->path = $newPath;
256
			$this->repository->saveDocument($documentObject);
257
		}
258
259 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...
260
		{
261
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
262
			if ($postValues['path'] === '/') {
263
				$documentObject->path = $postValues['path'] . $documentObject->slug;
264
			} else {
265
				$documentObject->path = $postValues['path'] . '/' . $documentObject->slug;
266
			}
267
268
			$this->repository->saveDocument($documentObject);
269
		}
270
271
		public function deleteDocumentBySlug($slug)
272
		{
273
			$path = '/' . $slug;
274
			$this->repository->deleteDocumentByPath($path);
275
		}
276
277
		/**
278
		 * Add new document in given path
279
		 *
280
		 * @param array $postValues
281
		 *
282
		 * @throws \Exception
283
		 */
284 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...
285
		{
286
			$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
287
			if ($postValues['path'] === '/') {
288
				$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
289
			} else {
290
				$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
291
			}
292
			$this->repository->saveDocument($documentFolderObject);
293
		}
294
295
		/**
296
		 * Delete a folder by its compound slug
297
		 *
298
		 * @param $slug
299
		 *
300
		 * @throws \Exception
301
		 */
302
		public function deleteDocumentFolderBySlug($slug)
303
		{
304
			$path = '/' . $slug;
305
			$this->repository->deleteDocumentByPath($path);
306
		}
307
308
		/**
309
		 * Retrieve a folder by its compound slug
310
		 *
311
		 * @param $slug
312
		 *
313
		 * @return mixed
314
		 * @throws \Exception
315
		 */
316
		public function getDocumentFolderBySlug($slug)
317
		{
318
			$path = '/' . $slug;
319
320
			return $this->repository->getDocumentByPath($path);
321
		}
322
323
		/**
324
		 * Save changes to folder
325
		 *
326
		 * @param $postValues
327
		 *
328
		 * @throws \Exception
329
		 */
330
		public function saveDocumentFolder($postValues)
331
		{
332
			$this->addDocumentFolder($postValues);
333
		}
334
335
		/**
336
		 * Convert path to indeces
337
		 *
338
		 * @param $path
339
		 *
340
		 * @return array
341
		 * @throws \Exception
342
		 */
343
		private function getDocumentContainerByPath($path)
344
		{
345
			return $this->repository->getDocumentContainerByPath($path);
346
		}
347
348
		/**
349
		 * @return SitemapStorage
350
		 */
351
		public function getSitemap()
352
		{
353
			if (!$this->sitemap instanceof SitemapStorage) {
354
				$this->sitemap = new SitemapStorage($this->repository);
355
			}
356
			return $this->sitemap;
357
		}
358
359
		/*
360
		 *
361
		 * Images
362
		 *
363
		 */
364
		/**
365
		 * Get all images
366
		 *
367
		 * @return ImagesStorage
368
		 */
369
		public function getImages()
370
		{
371
			if (!$this->images instanceof ImagesStorage) {
372
				$this->images = new ImagesStorage($this->repository);
373
			}
374
			return $this->images;
375
		}
376
377
		/*
378
		 *
379
		 * Files
380
		 *
381
		 */
382
		/**
383
		 * Get all files
384
		 *
385
		 * @return FilesStorage
386
		 */
387
		public function getFiles()
388
		{
389
			if (!$this->files instanceof FilesStorage) {
390
				$this->files = new FilesStorage($this->repository);
391
			}
392
			return $this->files;
393
		}
394
395
		/**
396
		 * @return string
397
		 */
398
		public function getStorageDir()
399
		{
400
			return $this->storageDir;
401
		}
402
403
		public function getContentDbHandle()
404
		{
405
			return $this->repository->getContentDbHandle();
406
		}
407
408
		/*
409
		 * 
410
		 * Configuration
411
		 *
412
		 */
413
		/**
414
		 * @return array
415
		 */
416
		public function getDocumentTypes()
417
		{
418
			return $this->repository->documentTypes;
419
		}
420
421
		/**
422
		 * Add a document type from post values
423
		 *
424
		 * @param $postValues
425
		 *
426
		 * @throws \Exception
427
		 */
428
		public function addDocumentType($postValues)
429
		{
430
			$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues);
431
432
			$documentTypes = $this->repository->documentTypes;
433
			$documentTypes[] = $documentTypeObject;
434
			$this->repository->documentTypes = $documentTypes;
435
436
			$this->save();
437
		}
438
439
		/**
440
		 * Delete document type
441
		 *
442
		 * @param $slug
443
		 *
444
		 * @throws \Exception
445
		 */
446 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...
447
		{
448
			$documentTypes = $this->repository->documentTypes;
449
			foreach ($documentTypes as $key => $documentTypeObject) {
450
				if ($documentTypeObject->slug == $slug) {
451
					unset($documentTypes[$key]);
452
				}
453
			}
454
			$documentTypes = array_values($documentTypes);
455
			$this->repository->documentTypes = $documentTypes;
456
			$this->save();
457
		}
458
459
		/**
460
		 * Get document type by its slug
461
		 *
462
		 * @param      $slug
463
		 * @param bool $getBricks
464
		 *
465
		 * @return mixed
466
		 */
467
		public function getDocumentTypeBySlug($slug, $getBricks = false)
468
		{
469
			$documentTypes = $this->repository->documentTypes;
470
			foreach ($documentTypes as $documentType) {
471
				if ($documentType->slug == $slug) {
472
					if ($getBricks === true) {
473
						foreach ($documentType->bricks as $key => $brick) {
474
							$brickStructure = $this->getBrickBySlug($brick->brickSlug);
475
							$documentType->bricks[$key]->structure = $brickStructure;
476
						}
477
						foreach ($documentType->dynamicBricks as $key => $brickSlug) {
478
							$brickStructure = $this->getBrickBySlug($brickSlug);
479
							$documentType->dynamicBricks[$key] = $brickStructure;
480
						}
481
					}
482
483
					return $documentType;
484
				}
485
			}
486
487
			return null;
488
		}
489
490
		/**
491
		 * Save changes to a document type
492
		 *
493
		 * @param $slug
494
		 * @param $postValues
495
		 *
496
		 * @throws \Exception
497
		 */
498 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...
499
		{
500
			$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues);
501
502
			$documentTypes = $this->repository->documentTypes;
503
			foreach ($documentTypes as $key => $documentType) {
504
				if ($documentType->slug == $slug) {
505
					$documentTypes[$key] = $documentTypeObject;
506
				}
507
			}
508
			$this->repository->documentTypes = $documentTypes;
509
			$this->save();
510
		}
511
512
		/*
513
		 *
514
		 * Bricks
515
		 *
516
		 */
517
		/**
518
		 * @return array
519
		 */
520
		public function getBricks()
521
		{
522
			return $this->repository->bricks;
523
		}
524
525
		/**
526
		 * Add a brick
527
		 *
528
		 * @param $postValues
529
		 *
530
		 * @throws \Exception
531
		 */
532
		public function addBrick($postValues)
533
		{
534
			$brickObject = BrickFactory::createBrickFromPostValues($postValues);
535
536
			$bricks = $this->repository->bricks;
537
			$bricks[] = $brickObject;
538
			$this->repository->bricks = $bricks;
539
540
			$this->save();
541
		}
542
543
		/**
544
		 * Get a brick by its slug
545
		 *
546
		 * @param $slug
547
		 *
548
		 * @return \stdClass
549
		 */
550
		public function getBrickBySlug($slug)
551
		{
552
			$bricks = $this->repository->bricks;
553
			foreach ($bricks as $brick) {
554
				if ($brick->slug == $slug) {
555
					return $brick;
556
				}
557
			}
558
559
			return null;
560
		}
561
562
		/**
563
		 * Save changes to a brick
564
		 *
565
		 * @param $slug
566
		 * @param $postValues
567
		 *
568
		 * @throws \Exception
569
		 */
570
		public function saveBrick($slug, $postValues)
571
		{
572
			$brickObject = BrickFactory::createBrickFromPostValues($postValues);
573
574
			$bricks = $this->repository->bricks;
575
			foreach ($bricks as $key => $brick) {
576
				if ($brick->slug == $slug) {
577
					$bricks[$key] = $brickObject;
578
				}
579
			}
580
			$this->repository->bricks = $bricks;
581
			$this->save();
582
		}
583
584
		/**
585
		 * Delete a brick by its slug
586
		 *
587
		 * @param $slug
588
		 *
589
		 * @throws \Exception
590
		 */
591
		public function deleteBrickBySlug($slug)
592
		{
593
			$bricks = $this->repository->bricks;
594
			foreach ($bricks as $key => $brickObject) {
595
				if ($brickObject->slug == $slug) {
596
					unset($bricks[$key]);
597
				}
598
			}
599
600
			$bricks = array_values($bricks);
601
			$this->repository->bricks = $bricks;
602
			$this->save();
603
		}
604
605
		/*
606
		 * 
607
		 * Misc
608
		 *
609
		 */
610
		/**
611
		 * Save changes made to the repository
612
		 *
613
		 * @throws \Exception
614
		 */
615
		private function save()
616
		{
617
			$this->repository->save();
618
		}
619
620
		/*
621
		 *
622
		 * Image Set
623
		 *
624
		 */
625
626
		/**
627
		 * Get the image set
628
		 *
629
		 * @return ImageSetStorage
630
		 */
631 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...
632
		{
633
			if (!$this->imageSet instanceof ImageSetStorage) {
634
				$this->imageSet = new ImageSetStorage($this->repository);
635
			}
636
			return $this->imageSet;
637
		}
638
639
		/**
640
		 * @return array
641
		 */
642
		public function getApplicationComponents()
643
		{
644
			return $this->repository->applicationComponents;
645
		}
646
647
		public function addApplicationComponent($postValues)
648
		{
649
			$applicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
650
			$applicationComponents = $this->repository->applicationComponents;
651
			$applicationComponents[] = $applicationComponent;
652
			$this->repository->applicationComponents = $applicationComponents;
653
654
			$this->save();
655
		}
656
657
		public function getApplicationComponentBySlug($slug)
658
		{
659
			$applicationComponents = $this->getApplicationComponents();
660
			foreach ($applicationComponents as $applicationComponent) {
661
				if ($applicationComponent->slug == $slug) {
662
					return $applicationComponent;
663
				}
664
			}
665
666
			return null;
667
		}
668
669
		public function saveApplicationComponent($slug, $postValues)
670
		{
671
			$newApplicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
672
673
			$applicationComponents = $this->getApplicationComponents();
674
			foreach ($applicationComponents as $key => $applicationComponent) {
675
				if ($applicationComponent->slug == $slug) {
676
					$applicationComponents[$key] = $newApplicationComponent;
677
				}
678
			}
679
			$this->repository->applicationComponents = $applicationComponents;
680
			$this->save();
681
		}
682
683
		public function deleteApplicationComponentBySlug($slug)
684
		{
685
			$applicationComponents = $this->getApplicationComponents();
686
			foreach ($applicationComponents as $key => $applicationComponent) {
687
				if ($applicationComponent->slug == $slug) {
688
					unset($applicationComponents[$key]);
689
				}
690
			}
691
			$applicationComponents = array_values($applicationComponents);
692
			$this->repository->applicationComponents = $applicationComponents;
693
			$this->save();
694
		}
695
696
	}
697
}