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

JsonStorage::addDocumentFolder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

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