Passed
Push — develop ( 16c70c...87f23a )
by Jens
02:57
created

DocumentFactory   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 120
Duplicated Lines 17.5 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 21
loc 120
rs 10
c 1
b 0
f 0
wmc 17
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createDocumentFromPostValues() 0 17 2
A createInitialDocumentObject() 0 15 3
C createBrickArrayForDocument() 11 40 8
A createDynamicBrickArrayForDocument() 10 17 4

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
/**
3
 * User: jensk
4
 * Date: 13-3-2017
5
 * Time: 16:24
6
 */
7
8
namespace library\storage;
9
10
11
class DocumentFactory
12
{
13
	/**
14
	 * @param array $postValues
15
	 * @param JsonStorage $jsonStorage
16
	 *
17
	 * @return \library\storage\Document
18
	 */
19
	public static function createDocumentFromPostValues($postValues, $jsonStorage)
20
	{
21
		$postValues = utf8Convert($postValues);
22
		$documentType = $jsonStorage->getDocumentTypeBySlug($postValues['documentType']);
23
24
		$staticBricks = $documentType->bricks;
25
26
		$documentObj = self::createInitialDocumentObject($postValues, $documentType);
27
28
		$documentObj->fields = isset($postValues['fields']) ? $postValues['fields'] : array();
29
		$documentObj->bricks = array();
30
31
		$documentObj = self::createBrickArrayForDocument($postValues, $documentObj, $staticBricks);
32
		$documentObj = self::createDynamicBrickArrayForDocument($postValues, $documentObj);
33
34
		return $documentObj;
35
	}
36
37
	/**
38
	 * @param array $postValues
39
	 * @param \stdClass $documentType
40
	 *
41
	 * @return Document
42
	 */
43
	private static function createInitialDocumentObject($postValues, $documentType)
44
	{
45
		$documentObj = new Document();
46
		$documentObj->title = $postValues['title'];
47
		$documentObj->slug = slugify($postValues['title']);
48
		$documentObj->type = 'document';
49
		$documentObj->documentType = $documentType->title;
50
		$documentObj->documentTypeSlug = $documentType->slug;
51
		$documentObj->state = isset($postValues['state']) ? 'published' : 'unpublished';
52
		$documentObj->lastModificationDate = time();
53
		$documentObj->creationDate = isset($postValues['creationDate']) ? intval($postValues['creationDate']) : time();
54
		$documentObj->lastModifiedBy = $_SESSION['cloudcontrol']->username;
55
56
		return $documentObj;
57
	}
58
59
	/**
60
	 * @param array $postValues
61
	 * @param Document $documentObj
62
	 * @param array $staticBricks
63
	 *
64
	 * @return Document
65
	 */
66
	private static function createBrickArrayForDocument($postValues, $documentObj, $staticBricks)
67
	{
68
		if (isset($postValues['bricks'])) {
69
			foreach ($postValues['bricks'] as $brickSlug => $brick) {
70
				// Check if its multiple
71
				$multiple = false;
72
				$staticBrick = null;
73
				foreach ($staticBricks as $staticBrick) {
74
					if ($staticBrick->slug === $brickSlug) {
75
						$multiple = $staticBrick->multiple;
76
						break;
77
					}
78
				}
79
80
				if ($multiple) {
81
					$brickArray = array();
82 View Code Duplication
					foreach ($brick as $brickInstance) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
83
						$brickObj = new \stdClass();
84
						$brickObj->fields = new \stdClass();
85
						$brickObj->type = $staticBrick->brickSlug;
86
87
						foreach ($brickInstance['fields'] as $fieldName => $fieldValues) {
88
							$brickObj->fields->$fieldName = $fieldValues;
89
						}
90
91
						$brickArray[] = $brickObj;
92
					}
93
94
					$bricks = $documentObj->bricks;
95
					$bricks[$brickSlug] = $brickArray;
96
					$documentObj->bricks = $bricks;
97
				} else {
98
					$bricks = $documentObj->bricks;
99
					$bricks[$brickSlug] = $brick;
100
					$documentObj->bricks = $bricks;
101
				}
102
			}
103
		}
104
		return $documentObj;
105
	}
106
107
	/**
108
	 * @param array $postValues
109
	 * @param Document $documentObj
110
	 *
111
	 * @return Document
112
	 */
113
	private static function createDynamicBrickArrayForDocument($postValues, $documentObj)
114
	{
115
		$documentObj->dynamicBricks = array();
116
		if (isset($postValues['dynamicBricks'])) {
117 View Code Duplication
			foreach ($postValues['dynamicBricks'] as $brickTypeSlug => $brick) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
118
				foreach ($brick as $brickContent) {
119
					$brickObj = new \stdClass();
120
					$brickObj->type = $brickTypeSlug;
121
					$brickObj->fields = $brickContent;
122
					$dynamicBricks = $documentObj->dynamicBricks;
123
					$dynamicBricks[] = $brickObj;
124
					$documentObj->dynamicBricks = $dynamicBricks;
125
				}
126
			}
127
		}
128
		return $documentObj;
129
	}
130
}