Passed
Push — develop ( 39fefa...eeeda6 )
by Jens
03:13
created

Storage::getFileByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
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\ImageSetStorage;
11
	use library\storage\storage\ImagesStorage;
12
	use library\storage\storage\SitemapStorage;
13
14
	/**
15
	 * Class JsonStorage
16
	 * @package library\storage
17
	 */
18
	class Storage
19
	{
20
		/**
21
		 * @var SitemapStorage
22
		 */
23
		protected $sitemap;
24
		/**
25
		 * @var ImagesStorage
26
		 */
27
		protected $images;
28
		/**
29
		 * @var ImageSetStorage
30
		 */
31
		protected $imageSet;
32
		/**
33
		 * @var String
34
		 */
35
		private $storageDir;
36
		/**
37
		 * @var Repository
38
		 */
39
		private $repository;
40
41
		/**
42
		 * JsonStorage constructor.
43
		 *
44
		 * @param string $storageDir
45
		 */
46
		public function __construct($storageDir)
47
		{
48
			$this->storageDir = $storageDir;
49
			$this->config();
50
		}
51
52
		/**
53
		 * Retrieve the data from the storagepath
54
		 * so it can be interacted with
55
		 *
56
		 * @throws \Exception
57
		 */
58
		private function config()
59
		{
60
			$storagePath = __DIR__ . '/../../' . $this->storageDir;
61
			if (realpath($storagePath) === false) {
62
				initFramework();
63
				if (Repository::create($storagePath)) {
64
					$repository = new Repository($storagePath);
65
					$repository->init();
66
					$this->repository = $repository;
67
				} else {
68
					throw new \Exception('Could not create repository directory: ' . $storagePath);
69
				}
70
			} else {
71
				$this->repository = new Repository($storagePath);
72
			}
73
74
		}
75
76
77
		/**
78
		 * Get user by username
79
		 *
80
		 * @param $username
81
		 *
82
		 * @return array
83
		 */
84 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...
85
		{
86
			$return = array();
87
88
			$users = $this->repository->users;
89
			foreach ($users as $user) {
90
				if ($user->username == $username) {
91
					$return = $user;
92
					break;
93
				}
94
			}
95
96
			return $return;
97
		}
98
99
		/**
100
		 * Get user by slug
101
		 *
102
		 * @param $slug
103
		 *
104
		 * @return array
105
		 */
106 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...
107
		{
108
			$return = array();
109
110
			$users = $this->repository->users;
111
			foreach ($users as $user) {
112
				if ($user->slug == $slug) {
113
					$return = $user;
114
					break;
115
				}
116
			}
117
118
			return $return;
119
		}
120
121
		/**
122
		 * Get all users
123
		 *
124
		 * @return mixed
125
		 */
126
		public function getUsers()
127
		{
128
			return $this->repository->users;
129
		}
130
131
		/**
132
		 * Save user
133
		 *
134
		 * @param $slug
135
		 * @param $postValues
136
		 *
137
		 * @throws \Exception
138
		 */
139
		public function saveUser($slug, $postValues)
140
		{
141
			$userObj = UserFactory::createUserFromPostValues($postValues);
142
			if ($userObj->slug != $slug) {
143
				// If the username changed, check for duplicates
144
				$doesItExist = $this->getUserBySlug($userObj->slug);
145
				if (!empty($doesItExist)) {
146
					throw new \Exception('Trying to rename user to existing username');
147
				}
148
			}
149
			$users = $this->getUsers();
150
			foreach ($users as $key => $user) {
151
				if ($user->slug == $slug) {
152
					$users[$key] = $userObj;
153
				}
154
			}
155
			$this->repository->users = $users;
156
			$this->save();
157
		}
158
159
		/**
160
		 * Add user
161
		 *
162
		 * @param $postValues
163
		 *
164
		 * @throws \Exception
165
		 */
166
		public function addUser($postValues)
167
		{
168
			$userObj = UserFactory::createUserFromPostValues($postValues);
169
170
			$doesItExist = $this->getUserBySlug($userObj->slug);
171
			if (!empty($doesItExist)) {
172
				throw new \Exception('Trying to add username that already exists.');
173
			}
174
			$users = $this->repository->users;
175
			$users[] = $userObj;
176
			$this->repository->users = $users;
177
			$this->save();
178
		}
179
180
		/**
181
		 * Delete user by slug
182
		 *
183
		 * @param $slug
184
		 *
185
		 * @throws \Exception
186
		 */
187
		public function deleteUserBySlug($slug)
188
		{
189
			$userToDelete = $this->getUserBySlug($slug);
190
			if (empty($userToDelete)) {
191
				throw new \Exception('Trying to delete a user that doesn\'t exist.');
192
			}
193
			$users = $this->getUsers();
194
			foreach ($users as $key => $user) {
195
				if ($user->slug == $userToDelete->slug) {
196
					unset($users[$key]);
197
					$this->repository->users = array_values($users);
198
				}
199
			}
200
			$this->save();
201
		}
202
203
		/*
204
		 *
205
		 * Documents
206
		 *
207
		 */
208
		/**
209
		 * Get documents
210
		 *
211
		 * @return array
212
		 */
213
		public function getDocuments()
214
		{
215
			return $this->repository->getDocuments();
216
		}
217
218
		public function getTotalDocumentCount()
219
		{
220
			return $this->repository->getTotalDocumentCount();
221
		}
222
223
		/**
224
		 * @param string $slug
225
		 *
226
		 * @return mixed
227
		 * @throws \Exception
228
		 */
229
		public function getDocumentBySlug($slug)
230
		{
231
			$path = '/' . $slug;
232
233
			return $this->repository->getDocumentByPath($path);
234
		}
235
236
		/**
237
		 * @param $postValues
238
		 */
239
		public function saveDocument($postValues)
240
		{
241
			$oldPath = '/' . $postValues['path'];
242
243
			$container = $this->getDocumentContainerByPath($oldPath);
244
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
245
			if ($container->path === '/') {
246
				$newPath = $container->path . $documentObject->slug;
247
			} else {
248
				$newPath = $container->path . '/' . $documentObject->slug;
249
			}
250
			$documentObject->path = $newPath;
251
			$this->repository->saveDocument($documentObject);
252
		}
253
254 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...
255
		{
256
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
257
			if ($postValues['path'] === '/') {
258
				$documentObject->path = $postValues['path'] . $documentObject->slug;
259
			} else {
260
				$documentObject->path = $postValues['path'] . '/' . $documentObject->slug;
261
			}
262
263
			$this->repository->saveDocument($documentObject);
264
		}
265
266
		public function deleteDocumentBySlug($slug)
267
		{
268
			$path = '/' . $slug;
269
			$this->repository->deleteDocumentByPath($path);
270
		}
271
272
		/**
273
		 * Add new document in given path
274
		 *
275
		 * @param array $postValues
276
		 *
277
		 * @throws \Exception
278
		 */
279 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...
280
		{
281
			$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
282
			if ($postValues['path'] === '/') {
283
				$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
284
			} else {
285
				$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
286
			}
287
			$this->repository->saveDocument($documentFolderObject);
288
		}
289
290
		/**
291
		 * Delete a folder by its compound slug
292
		 *
293
		 * @param $slug
294
		 *
295
		 * @throws \Exception
296
		 */
297
		public function deleteDocumentFolderBySlug($slug)
298
		{
299
			$path = '/' . $slug;
300
			$this->repository->deleteDocumentByPath($path);
301
		}
302
303
		/**
304
		 * Retrieve a folder by its compound slug
305
		 *
306
		 * @param $slug
307
		 *
308
		 * @return mixed
309
		 * @throws \Exception
310
		 */
311
		public function getDocumentFolderBySlug($slug)
312
		{
313
			$path = '/' . $slug;
314
315
			return $this->repository->getDocumentByPath($path);
316
		}
317
318
		/**
319
		 * Save changes to folder
320
		 *
321
		 * @param $postValues
322
		 *
323
		 * @throws \Exception
324
		 */
325
		public function saveDocumentFolder($postValues)
326
		{
327
			$this->addDocumentFolder($postValues);
328
		}
329
330
		/**
331
		 * Convert path to indeces
332
		 *
333
		 * @param $path
334
		 *
335
		 * @return array
336
		 * @throws \Exception
337
		 */
338
		private function getDocumentContainerByPath($path)
339
		{
340
			return $this->repository->getDocumentContainerByPath($path);
341
		}
342
343
		/**
344
		 * @return SitemapStorage
345
		 */
346
		public function getSitemap()
347
		{
348
			if (!$this->sitemap instanceof SitemapStorage) {
349
				$this->sitemap = new SitemapStorage($this->repository);
350
			}
351
			return $this->sitemap;
352
		}
353
354
		/*
355
		 *
356
		 * Images
357
		 *
358
		 */
359
		/**
360
		 * Get all images
361
		 *
362
		 * @return ImagesStorage
363
		 */
364
		public function getImages()
365
		{
366
			if (!$this->images instanceof ImagesStorage) {
367
				$this->images = new ImagesStorage($this->repository);
368
			}
369
			return $this->images;
370
		}
371
372
		/*
373
		 *
374
		 * Files
375
		 *
376
		 */
377
		/**
378
		 * Get all files
379
		 *
380
		 * @return array
381
		 */
382
		public function getFiles()
383
		{
384
			$files = $this->repository->files;
385
			usort($files, array($this, 'compareFiles'));
386
387
			return $files;
388
		}
389
390
		/**
391
		 * @return string
392
		 */
393
		public function getStorageDir()
394
		{
395
			return $this->storageDir;
396
		}
397
398
		public function getContentDbHandle()
399
		{
400
			return $this->repository->getContentDbHandle();
401
		}
402
403
		private function compareFiles($a, $b)
404
		{
405
			return strcmp($a->file, $b->file);
406
		}
407
408
		public function addFile($postValues)
409
		{
410
			$destinationPath = realpath(__DIR__ . '/../../www/files/');
411
412
			$filename = $this->validateFilename($postValues['name'], $destinationPath);
0 ignored issues
show
Bug introduced by
The method validateFilename() does not seem to exist on object<library\storage\Storage>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
413
			$destination = $destinationPath . '/' . $filename;
414
415
			if ($postValues['error'] != '0') {
416
				throw new \Exception('Error uploading file. Error code: ' . $postValues['error']);
417
			}
418
419
			if (move_uploaded_file($postValues['tmp_name'], $destination)) {
420
				$file = new \stdClass();
421
				$file->file = $filename;
422
				$file->type = $postValues['type'];
423
				$file->size = $postValues['size'];
424
425
				$files = $this->repository->files;
426
				$files[] = $file;
427
				$this->repository->files = $files;
428
				$this->save();
429
			} else {
430
				throw new \Exception('Error moving uploaded file');
431
			}
432
		}
433
434
		/**
435
		 * @param $filename
436
		 *
437
		 * @return null
438
		 */
439
		public function getFileByName($filename)
440
		{
441
			$files = $this->getFiles();
442
			foreach ($files as $file) {
443
				if ($filename == $file->file) {
444
					return $file;
445
				}
446
			}
447
448
			return null;
449
		}
450
451
		/**
452
		 * @param $filename
453
		 *
454
		 * @throws \Exception
455
		 */
456 View Code Duplication
		public function deleteFileByName($filename)
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...
457
		{
458
			$destinationPath = realpath(__DIR__ . '/../../www/files/');
459
			$destination = $destinationPath . '/' . $filename;
460
461
			if (file_exists($destination)) {
462
				$files = $this->getFiles();
463
				foreach ($files as $key => $file) {
464
					if ($file->file == $filename) {
465
						unlink($destination);
466
						unset($files[$key]);
467
					}
468
				}
469
470
				$files = array_values($files);
471
				$this->repository->files = $files;
472
				$this->save();
473
			}
474
		}
475
476
		/*
477
		 * 
478
		 * Configuration
479
		 *
480
		 */
481
		/**
482
		 * @return array
483
		 */
484
		public function getDocumentTypes()
485
		{
486
			return $this->repository->documentTypes;
487
		}
488
489
		/**
490
		 * Add a document type from post values
491
		 *
492
		 * @param $postValues
493
		 *
494
		 * @throws \Exception
495
		 */
496
		public function addDocumentType($postValues)
497
		{
498
			$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues);
499
500
			$documentTypes = $this->repository->documentTypes;
501
			$documentTypes[] = $documentTypeObject;
502
			$this->repository->documentTypes = $documentTypes;
503
504
			$this->save();
505
		}
506
507
		/**
508
		 * Delete document type
509
		 *
510
		 * @param $slug
511
		 *
512
		 * @throws \Exception
513
		 */
514 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...
515
		{
516
			$documentTypes = $this->repository->documentTypes;
517
			foreach ($documentTypes as $key => $documentTypeObject) {
518
				if ($documentTypeObject->slug == $slug) {
519
					unset($documentTypes[$key]);
520
				}
521
			}
522
			$documentTypes = array_values($documentTypes);
523
			$this->repository->documentTypes = $documentTypes;
524
			$this->save();
525
		}
526
527
		/**
528
		 * Get document type by its slug
529
		 *
530
		 * @param      $slug
531
		 * @param bool $getBricks
532
		 *
533
		 * @return mixed
534
		 */
535
		public function getDocumentTypeBySlug($slug, $getBricks = false)
536
		{
537
			$documentTypes = $this->repository->documentTypes;
538
			foreach ($documentTypes as $documentType) {
539
				if ($documentType->slug == $slug) {
540
					if ($getBricks === true) {
541
						foreach ($documentType->bricks as $key => $brick) {
542
							$brickStructure = $this->getBrickBySlug($brick->brickSlug);
543
							$documentType->bricks[$key]->structure = $brickStructure;
544
						}
545
						foreach ($documentType->dynamicBricks as $key => $brickSlug) {
546
							$brickStructure = $this->getBrickBySlug($brickSlug);
547
							$documentType->dynamicBricks[$key] = $brickStructure;
548
						}
549
					}
550
551
					return $documentType;
552
				}
553
			}
554
555
			return null;
556
		}
557
558
		/**
559
		 * Save changes to a document type
560
		 *
561
		 * @param $slug
562
		 * @param $postValues
563
		 *
564
		 * @throws \Exception
565
		 */
566 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...
567
		{
568
			$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues);
569
570
			$documentTypes = $this->repository->documentTypes;
571
			foreach ($documentTypes as $key => $documentType) {
572
				if ($documentType->slug == $slug) {
573
					$documentTypes[$key] = $documentTypeObject;
574
				}
575
			}
576
			$this->repository->documentTypes = $documentTypes;
577
			$this->save();
578
		}
579
580
		/*
581
		 *
582
		 * Bricks
583
		 *
584
		 */
585
		/**
586
		 * @return array
587
		 */
588
		public function getBricks()
589
		{
590
			return $this->repository->bricks;
591
		}
592
593
		/**
594
		 * Add a brick
595
		 *
596
		 * @param $postValues
597
		 *
598
		 * @throws \Exception
599
		 */
600
		public function addBrick($postValues)
601
		{
602
			$brickObject = BrickFactory::createBrickFromPostValues($postValues);
603
604
			$bricks = $this->repository->bricks;
605
			$bricks[] = $brickObject;
606
			$this->repository->bricks = $bricks;
607
608
			$this->save();
609
		}
610
611
		/**
612
		 * Get a brick by its slug
613
		 *
614
		 * @param $slug
615
		 *
616
		 * @return \stdClass
617
		 */
618
		public function getBrickBySlug($slug)
619
		{
620
			$bricks = $this->repository->bricks;
621
			foreach ($bricks as $brick) {
622
				if ($brick->slug == $slug) {
623
					return $brick;
624
				}
625
			}
626
627
			return null;
628
		}
629
630
		/**
631
		 * Save changes to a brick
632
		 *
633
		 * @param $slug
634
		 * @param $postValues
635
		 *
636
		 * @throws \Exception
637
		 */
638
		public function saveBrick($slug, $postValues)
639
		{
640
			$brickObject = BrickFactory::createBrickFromPostValues($postValues);
641
642
			$bricks = $this->repository->bricks;
643
			foreach ($bricks as $key => $brick) {
644
				if ($brick->slug == $slug) {
645
					$bricks[$key] = $brickObject;
646
				}
647
			}
648
			$this->repository->bricks = $bricks;
649
			$this->save();
650
		}
651
652
		/**
653
		 * Delete a brick by its slug
654
		 *
655
		 * @param $slug
656
		 *
657
		 * @throws \Exception
658
		 */
659
		public function deleteBrickBySlug($slug)
660
		{
661
			$bricks = $this->repository->bricks;
662
			foreach ($bricks as $key => $brickObject) {
663
				if ($brickObject->slug == $slug) {
664
					unset($bricks[$key]);
665
				}
666
			}
667
668
			$bricks = array_values($bricks);
669
			$this->repository->bricks = $bricks;
670
			$this->save();
671
		}
672
673
		/*
674
		 * 
675
		 * Misc
676
		 *
677
		 */
678
		/**
679
		 * Save changes made to the repository
680
		 *
681
		 * @throws \Exception
682
		 */
683
		private function save()
684
		{
685
			$this->repository->save();
686
		}
687
688
		/*
689
		 *
690
		 * Image Set
691
		 *
692
		 */
693
694
		/**
695
		 * Get the image set
696
		 *
697
		 * @return ImageSetStorage
698
		 */
699 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...
700
		{
701
			if (!$this->imageSet instanceof ImageSetStorage) {
702
				$this->imageSet = new ImageSetStorage($this->repository);
703
			}
704
			return $this->imageSet;
705
		}
706
707
		/**
708
		 * @return array
709
		 */
710
		public function getApplicationComponents()
711
		{
712
			return $this->repository->applicationComponents;
713
		}
714
715
		public function addApplicationComponent($postValues)
716
		{
717
			$applicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
718
			$applicationComponents = $this->repository->applicationComponents;
719
			$applicationComponents[] = $applicationComponent;
720
			$this->repository->applicationComponents = $applicationComponents;
721
722
			$this->save();
723
		}
724
725
		public function getApplicationComponentBySlug($slug)
726
		{
727
			$applicationComponents = $this->getApplicationComponents();
728
			foreach ($applicationComponents as $applicationComponent) {
729
				if ($applicationComponent->slug == $slug) {
730
					return $applicationComponent;
731
				}
732
			}
733
734
			return null;
735
		}
736
737
		public function saveApplicationComponent($slug, $postValues)
738
		{
739
			$newApplicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
740
741
			$applicationComponents = $this->getApplicationComponents();
742
			foreach ($applicationComponents as $key => $applicationComponent) {
743
				if ($applicationComponent->slug == $slug) {
744
					$applicationComponents[$key] = $newApplicationComponent;
745
				}
746
			}
747
			$this->repository->applicationComponents = $applicationComponents;
748
			$this->save();
749
		}
750
751
		public function deleteApplicationComponentBySlug($slug)
752
		{
753
			$applicationComponents = $this->getApplicationComponents();
754
			foreach ($applicationComponents as $key => $applicationComponent) {
755
				if ($applicationComponent->slug == $slug) {
756
					unset($applicationComponents[$key]);
757
				}
758
			}
759
			$applicationComponents = array_values($applicationComponents);
760
			$this->repository->applicationComponents = $applicationComponents;
761
			$this->save();
762
		}
763
764
	}
765
}