Passed
Push — master ( 9c6499...c22bc5 )
by Jens
04:52 queued 02:21
created

Storage   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 331
Duplicated Lines 4.23 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
dl 14
loc 331
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 14

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A config() 0 16 3
A getUsers() 0 7 2
A getDocuments() 0 7 2
A addDocumentFolder() 0 11 2
A deleteDocumentFolderBySlug() 0 7 1
A publishDocumentBySlug() 0 5 1
A unpublishDocumentBySlug() 0 5 1
A getDocumentFolderBySlug() 0 6 1
A saveDocumentFolder() 0 4 1
A getSitemap() 0 7 2
A getImages() 0 8 2
A getFiles() 0 7 2
A getStorageDir() 0 4 1
A getContentDbHandle() 0 4 1
A getDocumentTypes() 0 7 2
A getBricks() 7 7 2
A getImageSet() 7 7 2
A getApplicationComponents() 0 7 2
A getRepository() 0 4 1
A getValuelists() 0 7 2
A getRedirects() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
				if (Repository::create($storagePath)) {
112
					$repository = new Repository($storagePath);
113
					$repository->init();
0 ignored issues
show
Bug introduced by
The call to init() misses some required arguments starting with $baseStorageDefaultPath.
Loading history...
114
					$this->repository = $repository;
115
				} else {
116
					throw new \Exception('Could not create repository directory: ' . $storagePath);
117
				}
118
			} else {
119
				$this->repository = new Repository($storagePath);
120
			}
121
122
		}
123
124
		/**
125
		 * @return \CloudControl\Cms\storage\storage\UsersStorage
126
		 */
127
		public function getUsers()
128
		{
129
			if (!$this->users instanceof UsersStorage) {
130
				$this->users = new UsersStorage($this->repository);
131
			}
132
			return $this->users;
133
		}
134
135
		/**
136
		 * Get documents
137
		 *
138
		 * @return DocumentStorage
139
		 */
140
		public function getDocuments()
141
		{
142
			if (!$this->documents instanceof DocumentStorage) {
143
				$this->documents = new DocumentStorage($this->repository);
144
			}
145
			return $this->documents;
146
		}
147
148
		/**
149
		 * Add new document in given path
150
		 *
151
		 * @param array $postValues
152
		 *
153
		 * @throws \Exception
154
		 */
155
		public function addDocumentFolder($postValues)
156
		{
157
			$documentFolderObject = DocumentFolderFactory::createDocumentFolderFromPostValues($postValues);
158
			if ($postValues['path'] === '/') {
159
				$documentFolderObject->path = $postValues['path'] . $documentFolderObject->slug;
160
			} else {
161
				$documentFolderObject->path = $postValues['path'] . '/' . $documentFolderObject->slug;
162
			}
163
			$this->repository->saveDocument($documentFolderObject, 'published');
164
			$this->repository->saveDocument($documentFolderObject, 'unpublished');
165
		}
166
167
		/**
168
		 * Delete a folder by its compound slug
169
		 *
170
		 * @param $slug
171
		 *
172
		 * @throws \Exception
173
		 */
174
		public function deleteDocumentFolderBySlug($slug)
175
		{
176
			$path = '/' . $slug;
177
			$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...
178
			$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...
179
			$this->repository->cleanPublishedDeletedDocuments();
180
		}
181
182
		public function publishDocumentBySlug($slug)
183
		{
184
			$path = '/' . $slug;
185
			$this->repository->publishDocumentByPath($path);
186
		}
187
188
		public function unpublishDocumentBySlug($slug)
189
		{
190
			$path = '/' . $slug;
191
			$this->repository->unpublishDocumentByPath($path);
192
		}
193
194
		/**
195
		 * Retrieve a folder by its compound slug
196
		 *
197
		 * @param $slug
198
		 *
199
		 * @return mixed
200
		 * @throws \Exception
201
		 */
202
		public function getDocumentFolderBySlug($slug)
203
		{
204
			$path = '/' . $slug;
205
206
			return $this->repository->getDocumentByPath($path);
207
		}
208
209
		/**
210
		 * Save changes to folder
211
		 *
212
		 * @param $postValues
213
		 *
214
		 * @throws \Exception
215
		 */
216
		public function saveDocumentFolder($postValues)
217
		{
218
			$this->addDocumentFolder($postValues);
219
		}
220
221
		/**
222
		 * @return SitemapStorage
223
		 */
224
		public function getSitemap()
225
		{
226
			if (!$this->sitemap instanceof SitemapStorage) {
227
				$this->sitemap = new SitemapStorage($this->repository);
228
			}
229
			return $this->sitemap;
230
		}
231
232
		/**
233
		 * Get all images
234
		 *
235
		 * @return ImagesStorage
236
		 */
237
		public function getImages()
238
		{
239
			if (!$this->images instanceof ImagesStorage) {
240
241
                $this->images = new ImagesStorage($this->repository, $this->imagesDir);
242
			}
243
			return $this->images;
244
		}
245
246
		/**
247
		 * Get all files
248
		 *
249
		 * @return FilesStorage
250
		 */
251
		public function getFiles()
252
		{
253
			if (!$this->files instanceof FilesStorage) {
254
				$this->files = new FilesStorage($this->repository);
255
			}
256
			return $this->files;
257
		}
258
259
		/**
260
		 * @return string
261
		 */
262
		public function getStorageDir()
263
		{
264
			return $this->storageDir;
265
		}
266
267
		/**
268
		 * @return \PDO
269
		 */
270
		public function getContentDbHandle()
271
		{
272
			return $this->repository->getContentDbHandle();
273
		}
274
275
		/**
276
		 * @return DocumentTypesStorage
277
		 */
278
		public function getDocumentTypes()
279
		{
280
			if (!$this->documentTypes instanceof DocumentTypesStorage) {
281
				$this->documentTypes = new DocumentTypesStorage($this->repository);
282
			}
283
			return $this->documentTypes;
284
		}
285
286
		/**
287
		 * @return BricksStorage
288
		 */
289 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...
290
		{
291
			if (!$this->bricks instanceof BricksStorage) {
292
				$this->bricks = new BricksStorage($this->repository);
293
			}
294
			return $this->bricks;
295
		}
296
297
		/**
298
		 * Get the image set
299
		 *
300
		 * @return ImageSetStorage
301
		 */
302 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...
303
		{
304
			if (!$this->imageSet instanceof ImageSetStorage) {
305
				$this->imageSet = new ImageSetStorage($this->repository);
306
			}
307
			return $this->imageSet;
308
		}
309
310
		/**
311
		 * @return ApplicationComponentsStorage
312
		 */
313
		public function getApplicationComponents()
314
		{
315
			if (!$this->applicationComponents instanceof ApplicationComponentsStorage) {
316
				$this->applicationComponents = new ApplicationComponentsStorage($this->repository);
317
			}
318
			return $this->applicationComponents;
319
		}
320
321
		/**
322
		 * @return \CloudControl\Cms\storage\Repository
323
		 */
324
		public function getRepository()
325
		{
326
			return $this->repository;
327
		}
328
329
		/**
330
		 * @return \CloudControl\Cms\storage\storage\ValuelistsStorage
331
		 */
332
		public function getValuelists()
333
		{
334
			if (!$this->valuelists instanceof ValuelistsStorage) {
335
				$this->valuelists = new ValuelistsStorage($this->repository);
336
			}
337
			return $this->valuelists;
338
		}
339
340
        /**
341
         * @return \CloudControl\Cms\storage\storage\RedirectsStorage
342
         */
343
        public function getRedirects()
344
        {
345
            if (!$this->redirects instanceof RedirectsStorage) {
346
                $this->redirects = new RedirectsStorage($this->repository);
347
            }
348
            return $this->redirects;
349
        }
350
351
	}
352
}