Passed
Push — develop ( bdd28d...3cabcb )
by Jens
04:17
created

Storage::getDocumentFolderBySlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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