Passed
Push — develop ( 34ad04...e3baf2 )
by Jens
03:06
created

Storage::saveApplicationComponent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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