Passed
Push — develop ( e3baf2...b0eb3e )
by Jens
03:01
created

Storage::getSitemap()   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 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...
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);
141
		}
142
143
		/**
144
		 * Delete a folder by its compound slug
145
		 *
146
		 * @param $slug
147
		 *
148
		 * @throws \Exception
149
		 */
150
		public function deleteDocumentFolderBySlug($slug)
151
		{
152
			$path = '/' . $slug;
153
			$this->repository->deleteDocumentByPath($path);
154
		}
155
156
		/**
157
		 * Retrieve a folder by its compound slug
158
		 *
159
		 * @param $slug
160
		 *
161
		 * @return mixed
162
		 * @throws \Exception
163
		 */
164
		public function getDocumentFolderBySlug($slug)
165
		{
166
			$path = '/' . $slug;
167
168
			return $this->repository->getDocumentByPath($path);
169
		}
170
171
		/**
172
		 * Save changes to folder
173
		 *
174
		 * @param $postValues
175
		 *
176
		 * @throws \Exception
177
		 */
178
		public function saveDocumentFolder($postValues)
179
		{
180
			$this->addDocumentFolder($postValues);
181
		}
182
183
		/**
184
		 * @return SitemapStorage
185
		 */
186
		public function getSitemap()
187
		{
188
			if (!$this->sitemap instanceof SitemapStorage) {
189
				$this->sitemap = new SitemapStorage($this->repository);
190
			}
191
			return $this->sitemap;
192
		}
193
194
		/**
195
		 * Get all images
196
		 *
197
		 * @return ImagesStorage
198
		 */
199
		public function getImages()
200
		{
201
			if (!$this->images instanceof ImagesStorage) {
202
				$this->images = new ImagesStorage($this->repository);
203
			}
204
			return $this->images;
205
		}
206
207
		/**
208
		 * Get all files
209
		 *
210
		 * @return FilesStorage
211
		 */
212
		public function getFiles()
213
		{
214
			if (!$this->files instanceof FilesStorage) {
215
				$this->files = new FilesStorage($this->repository);
216
			}
217
			return $this->files;
218
		}
219
220
		/**
221
		 * @return string
222
		 */
223
		public function getStorageDir()
224
		{
225
			return $this->storageDir;
226
		}
227
228
		/**
229
		 * @return \PDO
230
		 */
231
		public function getContentDbHandle()
232
		{
233
			return $this->repository->getContentDbHandle();
234
		}
235
236
		/**
237
		 * @return DocumentTypesStorage
238
		 */
239
		public function getDocumentTypes()
240
		{
241
			if (!$this->documentTypes instanceof DocumentTypesStorage) {
242
				$this->documentTypes = new DocumentTypesStorage($this->repository);
243
			}
244
			return $this->documentTypes;
245
		}
246
247
		/**
248
		 * @return BricksStorage
249
		 */
250 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...
251
		{
252
			if (!$this->bricks instanceof BricksStorage) {
253
				$this->bricks = new BricksStorage($this->repository);
254
			}
255
			return $this->bricks;
256
		}
257
258
		/**
259
		 * Get the image set
260
		 *
261
		 * @return ImageSetStorage
262
		 */
263 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...
264
		{
265
			if (!$this->imageSet instanceof ImageSetStorage) {
266
				$this->imageSet = new ImageSetStorage($this->repository);
267
			}
268
			return $this->imageSet;
269
		}
270
271
		/**
272
		 * @return ApplicationComponentsStorage
273
		 */
274
		public function getApplicationComponents()
275
		{
276
			if (!$this->applicationComponents instanceof ApplicationComponentsStorage) {
277
				$this->applicationComponents = new ApplicationComponentsStorage($this->repository);
278
			}
279
			return $this->applicationComponents;
280
		}
281
	}
282
}