Passed
Push — develop ( fac880...0713b1 )
by Jens
03:08
created

Storage::getRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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