Passed
Push — develop ( 18e359...ca5759 )
by Jens
02:44
created

Storage::deleteDocumentFolderBySlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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