Passed
Push — develop ( bbf2e4...169afe )
by Jens
02:49
created

JsonStorage::throwJsonException()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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