Passed
Push — develop ( f5d3ab...1cfb3a )
by Jens
03:36
created

Storage::getDocuments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace library\storage {
3
4
	use library\storage\factories\DocumentFolderFactory;
5
	use library\storage\storage\ApplicationComponentsStorage;
6
	use library\storage\storage\BricksStorage;
7
	use library\storage\storage\DocumentStorage;
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 DocumentStorage
55
		 */
56
		protected $documents;
57
		/**
58
		 * @var String
59
		 */
60
		private $storageDir;
61
		/**
62
		 * @var Repository
63
		 */
64
		private $repository;
65
66
		/**
67
		 * JsonStorage constructor.
68
		 *
69
		 * @param string $storageDir
70
		 */
71
		public function __construct($storageDir)
72
		{
73
			$this->storageDir = $storageDir;
74
			$this->config();
75
		}
76
77
		/**
78
		 * Retrieve the data from the storagepath
79
		 * so it can be interacted with
80
		 *
81
		 * @throws \Exception
82
		 */
83
		private function config()
84
		{
85
			$storagePath = __DIR__ . '/../../' . $this->storageDir;
86
			if (realpath($storagePath) === false) {
87
				initFramework();
88
				if (Repository::create($storagePath)) {
89
					$repository = new Repository($storagePath);
90
					$repository->init();
91
					$this->repository = $repository;
92
				} else {
93
					throw new \Exception('Could not create repository directory: ' . $storagePath);
94
				}
95
			} else {
96
				$this->repository = new Repository($storagePath);
97
			}
98
99
		}
100
101
		/**
102
		 * @return \library\storage\storage\UsersStorage
103
		 */
104
		public function getUsers()
105
		{
106
			if (!$this->users instanceof UsersStorage) {
107
				$this->users = new UsersStorage($this->repository);
108
			}
109
			return $this->users;
110
		}
111
112
		/**
113
		 * Get documents
114
		 *
115
		 * @return DocumentStorage
116
		 */
117
		public function getDocuments()
118
		{
119
			if (!$this->documents instanceof DocumentStorage) {
120
				$this->documents = new DocumentStorage($this->repository);
121
			}
122
			return $this->documents;
123
		}
124
125
		/**
126
		 * Add new document in given path
127
		 *
128
		 * @param array $postValues
129
		 *
130
		 * @throws \Exception
131
		 */
132
		public function addDocumentFolder($postValues)
133
		{
134
			$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
135
			if ($postValues['path'] === '/') {
136
				$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
137
			} else {
138
				$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
139
			}
140
			$this->repository->saveDocument($documentFolderObject, 'published');
141
			$this->repository->saveDocument($documentFolderObject, 'unpublished');
142
		}
143
144
		/**
145
		 * Delete a folder by its compound slug
146
		 *
147
		 * @param $slug
148
		 *
149
		 * @throws \Exception
150
		 */
151
		public function deleteDocumentFolderBySlug($slug)
152
		{
153
			$path = '/' . $slug;
154
			$this->repository->deleteDocumentByPath($path, 'published');
0 ignored issues
show
Unused Code introduced by
The call to Repository::deleteDocumentByPath() has too many arguments starting with 'published'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
155
			$this->repository->deleteDocumentByPath($path, 'unpublished');
0 ignored issues
show
Unused Code introduced by
The call to Repository::deleteDocumentByPath() has too many arguments starting with 'unpublished'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
156
		}
157
158
		public function publishDocumentBySlug($slug)
159
		{
160
			$path = '/' . $slug;
161
			$this->repository->publishDocumentByPath($path);
162
		}
163
164
		public function unpublishDocumentBySlug($slug)
165
		{
166
			$path = '/' . $slug;
167
			$this->repository->unpublishDocumentByPath($path);
168
		}
169
170
		/**
171
		 * Retrieve a folder by its compound slug
172
		 *
173
		 * @param $slug
174
		 *
175
		 * @return mixed
176
		 * @throws \Exception
177
		 */
178
		public function getDocumentFolderBySlug($slug)
179
		{
180
			$path = '/' . $slug;
181
182
			return $this->repository->getDocumentByPath($path);
183
		}
184
185
		/**
186
		 * Save changes to folder
187
		 *
188
		 * @param $postValues
189
		 *
190
		 * @throws \Exception
191
		 */
192
		public function saveDocumentFolder($postValues)
193
		{
194
			$this->addDocumentFolder($postValues);
195
		}
196
197
		/**
198
		 * @return SitemapStorage
199
		 */
200
		public function getSitemap()
201
		{
202
			if (!$this->sitemap instanceof SitemapStorage) {
203
				$this->sitemap = new SitemapStorage($this->repository);
204
			}
205
			return $this->sitemap;
206
		}
207
208
		/**
209
		 * Get all images
210
		 *
211
		 * @return ImagesStorage
212
		 */
213
		public function getImages()
214
		{
215
			if (!$this->images instanceof ImagesStorage) {
216
				$this->images = new ImagesStorage($this->repository);
217
			}
218
			return $this->images;
219
		}
220
221
		/**
222
		 * Get all files
223
		 *
224
		 * @return FilesStorage
225
		 */
226
		public function getFiles()
227
		{
228
			if (!$this->files instanceof FilesStorage) {
229
				$this->files = new FilesStorage($this->repository);
230
			}
231
			return $this->files;
232
		}
233
234
		/**
235
		 * @return string
236
		 */
237
		public function getStorageDir()
238
		{
239
			return $this->storageDir;
240
		}
241
242
		/**
243
		 * @return \PDO
244
		 */
245
		public function getContentDbHandle()
246
		{
247
			return $this->repository->getContentDbHandle();
248
		}
249
250
		/**
251
		 * @return DocumentTypesStorage
252
		 */
253
		public function getDocumentTypes()
254
		{
255
			if (!$this->documentTypes instanceof DocumentTypesStorage) {
256
				$this->documentTypes = new DocumentTypesStorage($this->repository);
257
			}
258
			return $this->documentTypes;
259
		}
260
261
		/**
262
		 * @return BricksStorage
263
		 */
264 View Code Duplication
		public function getBricks()
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...
265
		{
266
			if (!$this->bricks instanceof BricksStorage) {
267
				$this->bricks = new BricksStorage($this->repository);
268
			}
269
			return $this->bricks;
270
		}
271
272
		/**
273
		 * Get the image set
274
		 *
275
		 * @return ImageSetStorage
276
		 */
277 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...
278
		{
279
			if (!$this->imageSet instanceof ImageSetStorage) {
280
				$this->imageSet = new ImageSetStorage($this->repository);
281
			}
282
			return $this->imageSet;
283
		}
284
285
		/**
286
		 * @return ApplicationComponentsStorage
287
		 */
288
		public function getApplicationComponents()
289
		{
290
			if (!$this->applicationComponents instanceof ApplicationComponentsStorage) {
291
				$this->applicationComponents = new ApplicationComponentsStorage($this->repository);
292
			}
293
			return $this->applicationComponents;
294
		}
295
296
		/**
297
		 * @return \library\storage\Repository
298
		 */
299
		public function getRepository()
300
		{
301
			return $this->repository;
302
		}
303
304
305
	}
306
}