Passed
Push — develop ( 95966b...34ad04 )
by Jens
02:58
created

Storage::addBrick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Storage::save() 0 4 1
1
<?php
2
namespace library\storage {
3
4
	use library\storage\factories\ApplicationComponentFactory;
5
	use library\storage\factories\DocumentFolderFactory;
6
	use library\storage\storage\BricksStorage;
7
	use library\storage\storage\DocumentTypesStorage;
8
	use library\storage\storage\FilesStorage;
9
	use library\storage\storage\ImageSetStorage;
10
	use library\storage\storage\ImagesStorage;
11
	use library\storage\storage\SitemapStorage;
12
	use library\storage\storage\UsersStorage;
13
14
	/**
15
	 * Class JsonStorage
16
	 * @package library\storage
17
	 */
18
	class Storage
19
	{
20
		/**
21
		 * @var SitemapStorage
22
		 */
23
		protected $sitemap;
24
		/**
25
		 * @var ImagesStorage
26
		 */
27
		protected $images;
28
		/**
29
		 * @var ImageSetStorage
30
		 */
31
		protected $imageSet;
32
		/**
33
		 * @var FilesStorage
34
		 */
35
		protected $files;
36
		/**
37
		 * @var UsersStorage
38
		 */
39
		protected $users;
40
		/**
41
		 * @var DocumentTypesStorage
42
		 */
43
		protected $documentTypes;
44
		/**
45
		 * @var BricksStorage
46
		 */
47
		protected $bricks;
48
		/**
49
		 * @var String
50
		 */
51
		private $storageDir;
52
		/**
53
		 * @var Repository
54
		 */
55
		private $repository;
56
57
		/**
58
		 * JsonStorage constructor.
59
		 *
60
		 * @param string $storageDir
61
		 */
62
		public function __construct($storageDir)
63
		{
64
			$this->storageDir = $storageDir;
65
			$this->config();
66
		}
67
68
		/**
69
		 * Retrieve the data from the storagepath
70
		 * so it can be interacted with
71
		 *
72
		 * @throws \Exception
73
		 */
74
		private function config()
75
		{
76
			$storagePath = __DIR__ . '/../../' . $this->storageDir;
77
			if (realpath($storagePath) === false) {
78
				initFramework();
79
				if (Repository::create($storagePath)) {
80
					$repository = new Repository($storagePath);
81
					$repository->init();
82
					$this->repository = $repository;
83
				} else {
84
					throw new \Exception('Could not create repository directory: ' . $storagePath);
85
				}
86
			} else {
87
				$this->repository = new Repository($storagePath);
88
			}
89
90
		}
91
92
		/**
93
		 * @return \library\storage\storage\UsersStorage
94
		 */
95
		public function getUsers()
96
		{
97
			if (!$this->users instanceof UsersStorage) {
98
				$this->users = new UsersStorage($this->repository);
99
			}
100
			return $this->users;
101
		}
102
103
		/*
104
		 *
105
		 * Documents
106
		 *
107
		 */
108
		/**
109
		 * Get documents
110
		 *
111
		 * @return array
112
		 */
113
		public function getDocuments()
114
		{
115
			return $this->repository->getDocuments();
116
		}
117
118
		public function getTotalDocumentCount()
119
		{
120
			return $this->repository->getTotalDocumentCount();
121
		}
122
123
		/**
124
		 * @param string $slug
125
		 *
126
		 * @return mixed
127
		 * @throws \Exception
128
		 */
129
		public function getDocumentBySlug($slug)
130
		{
131
			$path = '/' . $slug;
132
133
			return $this->repository->getDocumentByPath($path);
134
		}
135
136
		/**
137
		 * @param $postValues
138
		 */
139
		public function saveDocument($postValues)
140
		{
141
			$oldPath = '/' . $postValues['path'];
142
143
			$container = $this->getDocumentContainerByPath($oldPath);
144
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
145
			if ($container->path === '/') {
146
				$newPath = $container->path . $documentObject->slug;
147
			} else {
148
				$newPath = $container->path . '/' . $documentObject->slug;
149
			}
150
			$documentObject->path = $newPath;
151
			$this->repository->saveDocument($documentObject);
152
		}
153
154 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...
155
		{
156
			$documentObject = DocumentFactory::createDocumentFromPostValues($postValues, $this);
157
			if ($postValues['path'] === '/') {
158
				$documentObject->path = $postValues['path'] . $documentObject->slug;
159
			} else {
160
				$documentObject->path = $postValues['path'] . '/' . $documentObject->slug;
161
			}
162
163
			$this->repository->saveDocument($documentObject);
164
		}
165
166
		public function deleteDocumentBySlug($slug)
167
		{
168
			$path = '/' . $slug;
169
			$this->repository->deleteDocumentByPath($path);
170
		}
171
172
		/**
173
		 * Add new document in given path
174
		 *
175
		 * @param array $postValues
176
		 *
177
		 * @throws \Exception
178
		 */
179 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...
180
		{
181
			$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
182
			if ($postValues['path'] === '/') {
183
				$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
184
			} else {
185
				$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
186
			}
187
			$this->repository->saveDocument($documentFolderObject);
188
		}
189
190
		/**
191
		 * Delete a folder by its compound slug
192
		 *
193
		 * @param $slug
194
		 *
195
		 * @throws \Exception
196
		 */
197
		public function deleteDocumentFolderBySlug($slug)
198
		{
199
			$path = '/' . $slug;
200
			$this->repository->deleteDocumentByPath($path);
201
		}
202
203
		/**
204
		 * Retrieve a folder by its compound slug
205
		 *
206
		 * @param $slug
207
		 *
208
		 * @return mixed
209
		 * @throws \Exception
210
		 */
211
		public function getDocumentFolderBySlug($slug)
212
		{
213
			$path = '/' . $slug;
214
215
			return $this->repository->getDocumentByPath($path);
216
		}
217
218
		/**
219
		 * Save changes to folder
220
		 *
221
		 * @param $postValues
222
		 *
223
		 * @throws \Exception
224
		 */
225
		public function saveDocumentFolder($postValues)
226
		{
227
			$this->addDocumentFolder($postValues);
228
		}
229
230
		/**
231
		 * Convert path to indeces
232
		 *
233
		 * @param $path
234
		 *
235
		 * @return bool|\library\storage\Document
236
		 * @throws \Exception
237
		 */
238
		private function getDocumentContainerByPath($path)
239
		{
240
			return $this->repository->getDocumentContainerByPath($path);
241
		}
242
243
		/**
244
		 * @return SitemapStorage
245
		 */
246
		public function getSitemap()
247
		{
248
			if (!$this->sitemap instanceof SitemapStorage) {
249
				$this->sitemap = new SitemapStorage($this->repository);
250
			}
251
			return $this->sitemap;
252
		}
253
254
		/**
255
		 * Get all images
256
		 *
257
		 * @return ImagesStorage
258
		 */
259
		public function getImages()
260
		{
261
			if (!$this->images instanceof ImagesStorage) {
262
				$this->images = new ImagesStorage($this->repository);
263
			}
264
			return $this->images;
265
		}
266
267
		/**
268
		 * Get all files
269
		 *
270
		 * @return FilesStorage
271
		 */
272
		public function getFiles()
273
		{
274
			if (!$this->files instanceof FilesStorage) {
275
				$this->files = new FilesStorage($this->repository);
276
			}
277
			return $this->files;
278
		}
279
280
		/**
281
		 * @return string
282
		 */
283
		public function getStorageDir()
284
		{
285
			return $this->storageDir;
286
		}
287
288
		public function getContentDbHandle()
289
		{
290
			return $this->repository->getContentDbHandle();
291
		}
292
293
		/**
294
		 * @return DocumentTypesStorage
295
		 */
296
		public function getDocumentTypes()
297
		{
298
			if (!$this->documentTypes instanceof DocumentTypesStorage) {
299
				$this->documentTypes = new DocumentTypesStorage($this->repository);
300
			}
301
			return $this->documentTypes;
302
		}
303
304
		/*
305
		 *
306
		 * Bricks
307
		 *
308
		 */
309
		/**
310
		 * @return BricksStorage
311
		 */
312
		public function getBricks()
313
		{
314
			if (!$this->bricks instanceof BricksStorage) {
315
				$this->bricks = new BricksStorage($this->repository);
316
			}
317
			return $this->bricks;
318
		}
319
320
		/**
321
		 * Save changes made to the repository
322
		 *
323
		 * @throws \Exception
324
		 */
325
		private function save()
326
		{
327
			$this->repository->save();
328
		}
329
330
		/*
331
		 *
332
		 * Image Set
333
		 *
334
		 */
335
336
		/**
337
		 * Get the image set
338
		 *
339
		 * @return ImageSetStorage
340
		 */
341 View Code Duplication
		public function getImageSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
342
		{
343
			if (!$this->imageSet instanceof ImageSetStorage) {
344
				$this->imageSet = new ImageSetStorage($this->repository);
345
			}
346
			return $this->imageSet;
347
		}
348
349
		/**
350
		 * @return array
351
		 */
352
		public function getApplicationComponents()
353
		{
354
			return $this->repository->applicationComponents;
355
		}
356
357
		public function addApplicationComponent($postValues)
358
		{
359
			$applicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
360
			$applicationComponents = $this->repository->applicationComponents;
361
			$applicationComponents[] = $applicationComponent;
362
			$this->repository->applicationComponents = $applicationComponents;
363
364
			$this->save();
365
		}
366
367
		public function getApplicationComponentBySlug($slug)
368
		{
369
			$applicationComponents = $this->getApplicationComponents();
370
			foreach ($applicationComponents as $applicationComponent) {
371
				if ($applicationComponent->slug == $slug) {
372
					return $applicationComponent;
373
				}
374
			}
375
376
			return null;
377
		}
378
379
		public function saveApplicationComponent($slug, $postValues)
380
		{
381
			$newApplicationComponent = ApplicationComponentFactory::createApplicationComponentFromPostValues($postValues);
382
383
			$applicationComponents = $this->getApplicationComponents();
384
			foreach ($applicationComponents as $key => $applicationComponent) {
385
				if ($applicationComponent->slug == $slug) {
386
					$applicationComponents[$key] = $newApplicationComponent;
387
				}
388
			}
389
			$this->repository->applicationComponents = $applicationComponents;
390
			$this->save();
391
		}
392
393
		public function deleteApplicationComponentBySlug($slug)
394
		{
395
			$applicationComponents = $this->getApplicationComponents();
396
			foreach ($applicationComponents as $key => $applicationComponent) {
397
				if ($applicationComponent->slug == $slug) {
398
					unset($applicationComponents[$key]);
399
				}
400
			}
401
			$applicationComponents = array_values($applicationComponents);
402
			$this->repository->applicationComponents = $applicationComponents;
403
			$this->save();
404
		}
405
406
	}
407
}