Passed
Push — develop ( 87f23a...408180 )
by Jens
02:57
created

JsonStorage::addFile()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 1
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace library\storage {
3
4
	use library\images\ImageResizer;
5
	use library\storage\factories\ApplicationComponentFactory;
6
	use library\storage\factories\BrickFactory;
7
	use library\storage\factories\DocumentFolderFactory;
8
	use library\storage\factories\DocumentTypeFactory;
9
	use library\storage\factories\ImageSetFactory;
10
	use library\storage\factories\SitemapItemFactory;
11
	use library\storage\factories\UserFactory;
12
13
	/**
14
	 * Class JsonStorage
15
	 * @package library\storage
16
	 */
17
	class JsonStorage implements Storage
18
	{
19
		/**
20
		 * @var String
21
		 */
22
		private $storageDir;
23
		/**
24
		 * @var Repository
25
		 */
26
		private $repository;
27
28
		/**
29
		 * JsonStorage constructor.
30
		 *
31
		 * @param string $storageDir
32
		 */
33
		public function __construct($storageDir)
34
		{
35
			$this->storageDir = $storageDir;
36
			$this->config();
37
		}
38
39
		/**
40
		 * Retrieve the data from the storagepath
41
		 * so it can be interacted with
42
		 *
43
		 * @throws \Exception
44
		 */
45
		private function config()
46
		{
47
			$storagePath = __DIR__ . '/../../' . $this->storageDir;
48
			if (realpath($storagePath) === false) {
49
				initFramework();
50
				if (Repository::create($storagePath)) {
51
					$repository = new Repository($storagePath);
52
					$repository->init();
53
					$this->repository = $repository;
54
				} else {
55
					throw new \Exception('Could not create repository directory: ' . $storagePath);
56
				}
57
			} else {
58
				$this->repository = new Repository($storagePath);
59
			}
60
61
		}
62
63
64
		/**
65
		 * Get user by username
66
		 *
67
		 * @param $username
68
		 *
69
		 * @return array
70
		 */
71 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...
72
		{
73
			$return = array();
74
75
			$users = $this->repository->users;
76
			foreach ($users as $user) {
77
				if ($user->username == $username) {
78
					$return = $user;
79
					break;
80
				}
81
			}
82
83
			return $return;
84
		}
85
86
		/**
87
		 * Get user by slug
88
		 *
89
		 * @param $slug
90
		 *
91
		 * @return array
92
		 */
93 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...
94
		{
95
			$return = array();
96
97
			$users = $this->repository->users;
98
			foreach ($users as $user) {
99
				if ($user->slug == $slug) {
100
					$return = $user;
101
					break;
102
				}
103
			}
104
105
			return $return;
106
		}
107
108
		/**
109
		 * Get all users
110
		 *
111
		 * @return mixed
112
		 */
113
		public function getUsers()
114
		{
115
			return $this->repository->users;
116
		}
117
118
		/**
119
		 * Save user
120
		 *
121
		 * @param $slug
122
		 * @param $postValues
123
		 *
124
		 * @throws \Exception
125
		 */
126
		public function saveUser($slug, $postValues)
127
		{
128
			$userObj = UserFactory::createUserFromPostValues($postValues);
129
			if ($userObj->slug != $slug) {
130
				// If the username changed, check for duplicates
131
				$doesItExist = $this->getUserBySlug($userObj->slug);
132
				if (!empty($doesItExist)) {
133
					throw new \Exception('Trying to rename user to existing username');
134
				}
135
			}
136
			$users = $this->getUsers();
137
			foreach ($users as $key => $user) {
138
				if ($user->slug == $slug) {
139
					$users[$key] = $userObj;
140
				}
141
			}
142
			$this->repository->users = $users;
143
			$this->save();
144
		}
145
146
		/**
147
		 * Add user
148
		 *
149
		 * @param $postValues
150
		 *
151
		 * @throws \Exception
152
		 */
153
		public function addUser($postValues)
154
		{
155
			$userObj = UserFactory::createUserFromPostValues($postValues);
156
157
			$doesItExist = $this->getUserBySlug($userObj->slug);
158
			if (!empty($doesItExist)) {
159
				throw new \Exception('Trying to add username that already exists.');
160
			}
161
			$users = $this->repository->users;
162
			$users[] = $userObj;
163
			$this->repository->users = $users;
164
			$this->save();
165
		}
166
167
		/**
168
		 * Delete user by slug
169
		 *
170
		 * @param $slug
171
		 *
172
		 * @throws \Exception
173
		 */
174
		public function deleteUserBySlug($slug)
175
		{
176
			$userToDelete = $this->getUserBySlug($slug);
177
			if (empty($userToDelete)) {
178
				throw new \Exception('Trying to delete a user that doesn\'t exist.');
179
			}
180
			$users = $this->getUsers();
181
			foreach ($users as $key => $user) {
182
				if ($user->slug == $userToDelete->slug) {
183
					unset($users[$key]);
184
					$this->repository->users = array_values($users);
185
				}
186
			}
187
			$this->save();
188
		}
189
190
		/*
191
		 *
192
		 * Documents
193
		 *
194
		 */
195
		/**
196
		 * Get documents
197
		 *
198
		 * @return array
199
		 */
200
		public function getDocuments()
201
		{
202
			return $this->repository->getDocuments();
203
		}
204
205
		public function getTotalDocumentCount()
206
		{
207
			return $this->repository->getTotalDocumentCount();
208
		}
209
210
		/**
211
		 * @param string $slug
212
		 *
213
		 * @return mixed
214
		 * @throws \Exception
215
		 */
216
		public function getDocumentBySlug($slug)
217
		{
218
			$path = '/' . $slug;
219
220
			return $this->repository->getDocumentByPath($path);
221
		}
222
223
		/**
224
		 * @param $postValues
225
		 */
226
		public function saveDocument($postValues)
227
		{
228
			$oldPath = '/' . $postValues['path'];
229
230
			$container = $this->getDocumentContainerByPath($oldPath);
231
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
232
			if ($container->path === '/') {
233
				$newPath = $container->path . $documentObject->slug;
234
			} else {
235
				$newPath = $container->path . '/' . $documentObject->slug;
236
			}
237
			$documentObject->path = $newPath;
238
			$this->repository->saveDocument($documentObject);
239
		}
240
241 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...
242
		{
243
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
244
			if ($postValues['path'] === '/') {
245
				$documentObject->path = $postValues['path'] . $documentObject->slug;
246
			} else {
247
				$documentObject->path = $postValues['path'] . '/' . $documentObject->slug;
248
			}
249
250
			$this->repository->saveDocument($documentObject);
251
		}
252
253
		public function deleteDocumentBySlug($slug)
254
		{
255
			$path = '/' . $slug;
256
			$this->repository->deleteDocumentByPath($path);
257
		}
258
259
		/**
260
		 * Add new document in given path
261
		 *
262
		 * @param array $postValues
263
		 *
264
		 * @throws \Exception
265
		 */
266 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...
267
		{
268
			$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
269
			if ($postValues['path'] === '/') {
270
				$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
271
			} else {
272
				$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
273
			}
274
			$this->repository->saveDocument($documentFolderObject);
275
		}
276
277
		/**
278
		 * Delete a folder by its compound slug
279
		 *
280
		 * @param $slug
281
		 *
282
		 * @throws \Exception
283
		 */
284
		public function deleteDocumentFolderBySlug($slug)
285
		{
286
			$path = '/' . $slug;
287
			$this->repository->deleteDocumentByPath($path);
288
		}
289
290
		/**
291
		 * Retrieve a folder by its compound slug
292
		 *
293
		 * @param $slug
294
		 *
295
		 * @return mixed
296
		 * @throws \Exception
297
		 */
298
		public function getDocumentFolderBySlug($slug)
299
		{
300
			$path = '/' . $slug;
301
302
			return $this->repository->getDocumentByPath($path);
303
		}
304
305
		/**
306
		 * Save changes to folder
307
		 *
308
		 * @param $postValues
309
		 *
310
		 * @throws \Exception
311
		 */
312
		public function saveDocumentFolder($postValues)
313
		{
314
			$this->addDocumentFolder($postValues);
315
		}
316
317
		/**
318
		 * Convert path to indeces
319
		 *
320
		 * @param $path
321
		 *
322
		 * @return array
323
		 * @throws \Exception
324
		 */
325
		private function getDocumentContainerByPath($path)
326
		{
327
			return $this->repository->getDocumentContainerByPath($path);
328
		}
329
330
		/*
331
		 * 
332
		 * Sitemap
333
		 *
334
		 */
335
		/**
336
		 * @return array
337
		 */
338
		public function getSitemap()
339
		{
340
			return $this->repository->sitemap;
341
		}
342
343
		/**
344
		 * Add a sitemap item
345
		 *
346
		 * @param $postValues
347
		 *
348
		 * @throws \Exception
349
		 */
350
		public function addSitemapItem($postValues)
351
		{
352
			$sitemapObject = SitemapItemFactory::createSitemapItemFromPostValues($postValues);
353
			$sitemap = $this->repository->sitemap;
354
			$sitemap[] = $sitemapObject;
355
			$this->repository->sitemap = $sitemap;
356
			$this->save();
357
		}
358
359
		/**
360
		 * Save changes to a sitemap item
361
		 *
362
		 * @param $slug
363
		 * @param $postValues
364
		 *
365
		 * @throws \Exception
366
		 */
367
		public function saveSitemapItem($slug, $postValues)
368
		{
369
			$sitemapObject = SitemapItemFactory::createSitemapItemFromPostValues($postValues);
370
371
			$sitemap = $this->repository->sitemap;
372
			foreach ($sitemap as $key => $sitemapItem) {
373
				if ($sitemapItem->slug == $slug) {
374
					$sitemap[$key] = $sitemapObject;
375
				}
376
			}
377
			$this->repository->sitemap = $sitemap;
378
			$this->save();
379
		}
380
381
		/**
382
		 * Delete a sitemap item by its slug
383
		 *
384
		 * @param $slug
385
		 *
386
		 * @throws \Exception
387
		 */
388
		public function deleteSitemapItemBySlug($slug)
389
		{
390
			$sitemap = $this->repository->sitemap;
391
			foreach ($sitemap as $key => $sitemapItem) {
392
				if ($sitemapItem->slug == $slug) {
393
					unset($sitemap[$key]);
394
				}
395
			}
396
			$sitemap = array_values($sitemap);
397
			$this->repository->sitemap = $sitemap;
398
			$this->save();
399
		}
400
401
		/**
402
		 * Save changes to a sitemap item
403
		 *
404
		 * @param $postValues
405
		 *
406
		 * @throws \Exception
407
		 */
408
		public function saveSitemap($postValues)
409
		{
410
			if (isset($postValues['sitemapitem']) && is_array($postValues['sitemapitem'])) {
411
				$sitemap = array();
412
				foreach ($postValues['sitemapitem'] as $sitemapItem) {
413
					$sitemapItemObject = json_decode($sitemapItem);
414
					if (isset($sitemapItemObject->object)) {
415
						unset($sitemapItemObject->object);
416
					}
417
					$sitemap[] = $sitemapItemObject;
418
				}
419
				$this->repository->sitemap = $sitemap;
420
				$this->save();
421
			}
422
		}
423
424
		/**
425
		 * Get a sitemap item by its slug
426
		 *
427
		 * @param $slug
428
		 *
429
		 * @return mixed
430
		 */
431
		public function getSitemapItemBySlug($slug)
432
		{
433
			$sitemap = $this->repository->sitemap;
434
			foreach ($sitemap as $sitemapItem) {
435
				if ($sitemapItem->slug == $slug) {
436
					return $sitemapItem;
437
				}
438
			}
439
440
			return null;
441
		}
442
443
		/*
444
		 *
445
		 * Images
446
		 *
447
		 */
448
		/**
449
		 * Get all images
450
		 *
451
		 * @return array
452
		 */
453
		public function getImages()
454
		{
455
			return $this->repository->images;
456
		}
457
458
		public function addImage($postValues)
459
		{
460
			$destinationPath = realpath(__DIR__ . '/../../www/images/');
461
462
			$filename = $this->validateFilename($postValues['name'], $destinationPath);
463
			$destination = $destinationPath . '/' . $filename;
464
465
			if ($postValues['error'] != '0') {
466
				throw new \Exception('Error uploading file. Error code: ' . $postValues['error']);
467
			}
468
469
			if (move_uploaded_file($postValues['tmp_name'], $destination)) {
470
				$imageResizer = new ImageResizer($this->getImageSet());
471
				$fileNames = $imageResizer->applyImageSetToImage($destination);
472
				$fileNames['original'] = $filename;
473
				$imageObject = new \stdClass();
474
				$imageObject->file = $filename;
475
				$imageObject->type = $postValues['type'];
476
				$imageObject->size = $postValues['size'];
477
				$imageObject->set = $fileNames;
478
479
				$images = $this->repository->images;
480
				$images[] = $imageObject;
481
				$this->repository->images = $images;
482
483
				$this->save();
484
			} else {
485
				throw new \Exception('Error moving uploaded file');
486
			}
487
		}
488
489 View Code Duplication
		public function deleteImageByName($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...
490
		{
491
			$destinationPath = realpath(__DIR__ . '/../../www/images/');
492
493
			$images = $this->getImages();
494
495
			foreach ($images as $key => $image) {
496
				if ($image->file == $filename) {
497
					foreach ($image->set as $imageSetFilename) {
498
						$destination = $destinationPath . '/' . $imageSetFilename;
499
						if (file_exists($destination)) {
500
							unlink($destination);
501
						} else {
502
							dump($destination);
503
						}
504
					}
505
					unset($images[$key]);
506
				}
507
			}
508
509
			$this->repository->images = $images;
510
			$this->save();
511
		}
512
513
		/**
514
		 * @param $filename
515
		 *
516
		 * @return null
517
		 */
518
		public function getImageByName($filename)
519
		{
520
			$images = $this->getImages();
521
			foreach ($images as $image) {
522
				if ($image->file == $filename) {
523
					return $image;
524
				}
525
			}
526
527
			return null;
528
		}
529
530
		/*
531
		 *
532
		 * Files
533
		 *
534
		 */
535
		/**
536
		 * Get all files
537
		 *
538
		 * @return array
539
		 */
540
		public function getFiles()
541
		{
542
			$files = $this->repository->files;
543
			usort($files, array($this, 'compareFiles'));
544
545
			return $files;
546
		}
547
548
		/**
549
		 * @return string
550
		 */
551
		public function getStorageDir()
552
		{
553
			return $this->storageDir;
554
		}
555
556
		public function getContentDbHandle()
557
		{
558
			return $this->repository->getContentDbHandle();
559
		}
560
561
		private function compareFiles($a, $b)
562
		{
563
			return strcmp($a->file, $b->file);
564
		}
565
566
		public function addFile($postValues)
567
		{
568
			$destinationPath = realpath(__DIR__ . '/../../www/files/');
569
570
			$filename = $this->validateFilename($postValues['name'], $destinationPath);
571
			$destination = $destinationPath . '/' . $filename;
572
573
			if ($postValues['error'] != '0') {
574
				throw new \Exception('Error uploading file. Error code: ' . $postValues['error']);
575
			}
576
577
			if (move_uploaded_file($postValues['tmp_name'], $destination)) {
578
				$file = new \stdClass();
579
				$file->file = $filename;
580
				$file->type = $postValues['type'];
581
				$file->size = $postValues['size'];
582
583
				$files = $this->repository->files;
584
				$files[] = $file;
585
				$this->repository->files = $files;
586
				$this->save();
587
			} else {
588
				throw new \Exception('Error moving uploaded file');
589
			}
590
		}
591
592
		private function validateFilename($filename, $path)
593
		{
594
			$fileParts = explode('.', $filename);
595 View Code Duplication
			if (count($fileParts) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
596
				$extension = end($fileParts);
597
				array_pop($fileParts);
598
				$fileNameWithoutExtension = implode('-', $fileParts);
599
				$fileNameWithoutExtension = slugify($fileNameWithoutExtension);
600
				$filename = $fileNameWithoutExtension . '.' . $extension;
601
			} else {
602
				$filename = slugify($filename);
603
			}
604
605 View Code Duplication
			if (file_exists($path . '/' . $filename)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
606
				$fileParts = explode('.', $filename);
607
				if (count($fileParts) > 1) {
608
					$extension = end($fileParts);
609
					array_pop($fileParts);
610
					$fileNameWithoutExtension = implode('-', $fileParts);
611
					$fileNameWithoutExtension .= '-copy';
612
					$filename = $fileNameWithoutExtension . '.' . $extension;
613
				} else {
614
					$filename .= '-copy';
615
				}
616
617
				return $this->validateFilename($filename, $path);
618
			}
619
620
			return $filename;
621
		}
622
623
		/**
624
		 * @param $filename
625
		 *
626
		 * @return null
627
		 */
628
		public function getFileByName($filename)
629
		{
630
			$files = $this->getFiles();
631
			foreach ($files as $file) {
632
				if ($filename == $file->file) {
633
					return $file;
634
				}
635
			}
636
637
			return null;
638
		}
639
640
		/**
641
		 * @param $filename
642
		 *
643
		 * @throws \Exception
644
		 */
645 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...
646
		{
647
			$destinationPath = realpath(__DIR__ . '/../../www/files/');
648
			$destination = $destinationPath . '/' . $filename;
649
650
			if (file_exists($destination)) {
651
				$files = $this->getFiles();
652
				foreach ($files as $key => $file) {
653
					if ($file->file == $filename) {
654
						unlink($destination);
655
						unset($files[$key]);
656
					}
657
				}
658
659
				$files = array_values($files);
660
				$this->repository->files = $files;
661
				$this->save();
662
			}
663
		}
664
665
		/*
666
		 * 
667
		 * Configuration
668
		 *
669
		 */
670
		/**
671
		 * @return array
672
		 */
673
		public function getDocumentTypes()
674
		{
675
			return $this->repository->documentTypes;
676
		}
677
678
		/**
679
		 * Add a document type from post values
680
		 *
681
		 * @param $postValues
682
		 *
683
		 * @throws \Exception
684
		 */
685
		public function addDocumentType($postValues)
686
		{
687
			$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues);
688
689
			$documentTypes = $this->repository->documentTypes;
690
			$documentTypes[] = $documentTypeObject;
691
			$this->repository->documentTypes = $documentTypes;
692
693
			$this->save();
694
		}
695
696
		/**
697
		 * Delete document type
698
		 *
699
		 * @param $slug
700
		 *
701
		 * @throws \Exception
702
		 */
703 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...
704
		{
705
			$documentTypes = $this->repository->documentTypes;
706
			foreach ($documentTypes as $key => $documentTypeObject) {
707
				if ($documentTypeObject->slug == $slug) {
708
					unset($documentTypes[$key]);
709
				}
710
			}
711
			$documentTypes = array_values($documentTypes);
712
			$this->repository->documentTypes = $documentTypes;
713
			$this->save();
714
		}
715
716
		/**
717
		 * Get document type by its slug
718
		 *
719
		 * @param      $slug
720
		 * @param bool $getBricks
721
		 *
722
		 * @return mixed
723
		 */
724
		public function getDocumentTypeBySlug($slug, $getBricks = false)
725
		{
726
			$documentTypes = $this->repository->documentTypes;
727
			foreach ($documentTypes as $documentType) {
728
				if ($documentType->slug == $slug) {
729
					if ($getBricks === true) {
730
						foreach ($documentType->bricks as $key => $brick) {
731
							$brickStructure = $this->getBrickBySlug($brick->brickSlug);
732
							$documentType->bricks[$key]->structure = $brickStructure;
733
						}
734
						foreach ($documentType->dynamicBricks as $key => $brickSlug) {
735
							$brickStructure = $this->getBrickBySlug($brickSlug);
736
							$documentType->dynamicBricks[$key] = $brickStructure;
737
						}
738
					}
739
740
					return $documentType;
741
				}
742
			}
743
744
			return null;
745
		}
746
747
		/**
748
		 * Save changes to a document type
749
		 *
750
		 * @param $slug
751
		 * @param $postValues
752
		 *
753
		 * @throws \Exception
754
		 */
755 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...
756
		{
757
			$documentTypeObject = DocumentTypeFactory::createDocumentTypeFromPostValues($postValues);
758
759
			$documentTypes = $this->repository->documentTypes;
760
			foreach ($documentTypes as $key => $documentType) {
761
				if ($documentType->slug == $slug) {
762
					$documentTypes[$key] = $documentTypeObject;
763
				}
764
			}
765
			$this->repository->documentTypes = $documentTypes;
766
			$this->save();
767
		}
768
769
		/*
770
		 *
771
		 * Bricks
772
		 *
773
		 */
774
		/**
775
		 * @return array
776
		 */
777
		public function getBricks()
778
		{
779
			return $this->repository->bricks;
780
		}
781
782
		/**
783
		 * Add a brick
784
		 *
785
		 * @param $postValues
786
		 *
787
		 * @throws \Exception
788
		 */
789
		public function addBrick($postValues)
790
		{
791
			$brickObject = BrickFactory::createBrickFromPostValues($postValues);
792
793
			$bricks = $this->repository->bricks;
794
			$bricks[] = $brickObject;
795
			$this->repository->bricks = $bricks;
796
797
			$this->save();
798
		}
799
800
		/**
801
		 * Get a brick by its slug
802
		 *
803
		 * @param $slug
804
		 *
805
		 * @return \stdClass
806
		 */
807
		public function getBrickBySlug($slug)
808
		{
809
			$bricks = $this->repository->bricks;
810
			foreach ($bricks as $brick) {
811
				if ($brick->slug == $slug) {
812
					return $brick;
813
				}
814
			}
815
816
			return null;
817
		}
818
819
		/**
820
		 * Save changes to a brick
821
		 *
822
		 * @param $slug
823
		 * @param $postValues
824
		 *
825
		 * @throws \Exception
826
		 */
827
		public function saveBrick($slug, $postValues)
828
		{
829
			$brickObject = BrickFactory::createBrickFromPostValues($postValues);
830
831
			$bricks = $this->repository->bricks;
832
			foreach ($bricks as $key => $brick) {
833
				if ($brick->slug == $slug) {
834
					$bricks[$key] = $brickObject;
835
				}
836
			}
837
			$this->repository->bricks = $bricks;
838
			$this->save();
839
		}
840
841
		/**
842
		 * Delete a brick by its slug
843
		 *
844
		 * @param $slug
845
		 *
846
		 * @throws \Exception
847
		 */
848
		public function deleteBrickBySlug($slug)
849
		{
850
			$bricks = $this->repository->bricks;
851
			foreach ($bricks as $key => $brickObject) {
852
				if ($brickObject->slug == $slug) {
853
					unset($bricks[$key]);
854
				}
855
			}
856
857
			$bricks = array_values($bricks);
858
			$this->repository->bricks = $bricks;
859
			$this->save();
860
		}
861
862
		/*
863
		 * 
864
		 * Misc
865
		 *
866
		 */
867
		/**
868
		 * Save changes made to the repository
869
		 *
870
		 * @throws \Exception
871
		 */
872
		private function save()
873
		{
874
			$this->repository->save();
875
		}
876
877
		/*
878
		 *
879
		 * Image Set
880
		 *
881
		 */
882
883
		/**
884
		 * Get the image set
885
		 *
886
		 * @return array
887
		 */
888
		public function getImageSet()
889
		{
890
			return $this->repository->imageSet;
891
		}
892
893
		/**
894
		 * Get Image by slug
895
		 *
896
		 * @param $slug
897
		 *
898
		 * @return \stdClass
899
		 */
900
		public function getImageSetBySlug($slug)
901
		{
902
			$imageSet = $this->getImageSet();
903
			foreach ($imageSet as $set) {
904
				if ($set->slug == $slug) {
905
					return $set;
906
				}
907
			}
908
909
			return null;
910
		}
911
912
		/**
913
		 * Save Image Set by it's slug
914
		 *
915
		 * @param $slug
916
		 * @param $postValues
917
		 *
918
		 * @throws \Exception
919
		 */
920
		public function saveImageSet($slug, $postValues)
921
		{
922
			$imageSetObject = ImageSetFactory::createImageSetFromPostValues($postValues);
923
924
			$imageSet = $this->repository->imageSet;
925
			foreach ($imageSet as $key => $set) {
926
				if ($set->slug == $slug) {
927
					$imageSet[$key] = $imageSetObject;
928
				}
929
			}
930
			$this->repository->imageSet = $imageSet;
931
			$this->save();
932
		}
933
934
		/**
935
		 * Add image set
936
		 *
937
		 * @param $postValues
938
		 *
939
		 * @throws \Exception
940
		 */
941
		public function addImageSet($postValues)
942
		{
943
			$imageSetObject = ImageSetFactory::createImageSetFromPostValues($postValues);
944
945
			$imageSet = $this->repository->imageSet;
946
			$imageSet[] = $imageSetObject;
947
			$this->repository->imageSet = $imageSet;
948
949
			$this->save();
950
		}
951
952
		/**
953
		 * Delete Image Set by its slug
954
		 *
955
		 * @param $slug
956
		 *
957
		 * @throws \Exception
958
		 */
959
		public function deleteImageSetBySlug($slug)
960
		{
961
			$imageSet = $this->getImageSet();
962
963
			foreach ($imageSet as $key => $set) {
964
				if ($set->slug == $slug) {
965
					unset($imageSet[$key]);
966
				}
967
			}
968
			$imageSet = array_values($imageSet);
969
			$this->repository->imageSet = $imageSet;
970
			$this->save();
971
		}
972
973
		/**
974
		 * Get the image set with the smallest size
975
		 *
976
		 * @return \stdClass
977
		 */
978
		public function getSmallestImageSet()
979
		{
980
			$imageSet = $this->getImageSet();
981
982
			$returnSize = PHP_INT_MAX;
983
			$returnSet = null;
984
985
			foreach ($imageSet as $set) {
986
				$size = $set->width * $set->height;
987
				if ($size < $returnSize) {
988
					$returnSize = $size;
989
					$returnSet = $set;
990
				}
991
			}
992
993
			if ($returnSet === null) {
994
				$returnSet = new \stdClass();
995
				$returnSet->slug = 'original';
996
			}
997
998
			return $returnSet;
999
		}
1000
1001
		/**
1002
		 * @return array
1003
		 */
1004
		public function getApplicationComponents()
1005
		{
1006
			return $this->repository->applicationComponents;
1007
		}
1008
1009
		public function addApplicationComponent($postValues)
1010
		{
1011
			$applicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
1012
			$applicationComponents = $this->repository->applicationComponents;
1013
			$applicationComponents[] = $applicationComponent;
1014
			$this->repository->applicationComponents = $applicationComponents;
1015
1016
			$this->save();
1017
		}
1018
1019
		public function getApplicationComponentBySlug($slug)
1020
		{
1021
			$applicationComponents = $this->getApplicationComponents();
1022
			foreach ($applicationComponents as $applicationComponent) {
1023
				if ($applicationComponent->slug == $slug) {
1024
					return $applicationComponent;
1025
				}
1026
			}
1027
1028
			return null;
1029
		}
1030
1031
		public function saveApplicationComponent($slug, $postValues)
1032
		{
1033
			$newApplicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
1034
1035
			$applicationComponents = $this->getApplicationComponents();
1036
			foreach ($applicationComponents as $key => $applicationComponent) {
1037
				if ($applicationComponent->slug == $slug) {
1038
					$applicationComponents[$key] = $newApplicationComponent;
1039
				}
1040
			}
1041
			$this->repository->applicationComponents = $applicationComponents;
1042
			$this->save();
1043
		}
1044
1045
		public function deleteApplicationComponentBySlug($slug)
1046
		{
1047
			$applicationComponents = $this->getApplicationComponents();
1048
			foreach ($applicationComponents as $key => $applicationComponent) {
1049
				if ($applicationComponent->slug == $slug) {
1050
					unset($applicationComponents[$key]);
1051
				}
1052
			}
1053
			$applicationComponents = array_values($applicationComponents);
1054
			$this->repository->applicationComponents = $applicationComponents;
1055
			$this->save();
1056
		}
1057
1058
	}
1059
}