Passed
Push — master ( 3f6c85...9c6499 )
by Jens
02:40
created

DocumentFactory   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 173
rs 10
c 1
b 0
f 0
wmc 21
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createDocumentFromPostValues() 0 17 2
A createInitialDocumentObject() 0 15 3
A createBrickArrayForDocument() 0 16 4
A createDynamicBrickArrayForDocument() 0 17 4
A getStaticBrickAndSetMultiple() 0 13 3
A createBrick() 0 12 2
A addMultipleBricks() 0 14 2
A addSingleBrick() 0 7 1
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 13-3-2017
5
 * Time: 16:24
6
 */
7
8
namespace library\storage\factories;
9
10
11
use library\cc\StringUtil;
12
use library\storage\Document;
13
use library\storage\storage\DocumentTypesStorage;
14
15
class DocumentFactory
16
{
17
	/**
18
	 * @param array                $postValues
19
	 * @param DocumentTypesStorage $documentTypesStorage
20
	 *
21
	 * @return \library\storage\Document
22
	 */
23
	public static function createDocumentFromPostValues($postValues, DocumentTypesStorage $documentTypesStorage)
24
	{
25
		$postValues = utf8Convert($postValues);
26
		$documentType = $documentTypesStorage->getDocumentTypeBySlug($postValues['documentType']);
27
28
		$staticBricks = $documentType->bricks;
29
30
		$documentObj = self::createInitialDocumentObject($postValues, $documentType);
31
32
		$documentObj->fields = isset($postValues['fields']) ? $postValues['fields'] : array();
33
		$documentObj->bricks = array();
34
35
		$documentObj = self::createBrickArrayForDocument($postValues, $documentObj, $staticBricks);
36
		$documentObj = self::createDynamicBrickArrayForDocument($postValues, $documentObj);
37
38
		return $documentObj;
39
	}
40
41
	/**
42
	 * @param array $postValues
43
	 * @param \stdClass $documentType
44
	 *
45
	 * @return Document
46
	 */
47
	private static function createInitialDocumentObject($postValues, $documentType)
48
	{
49
		$documentObj = new Document();
50
		$documentObj->title = $postValues['title'];
51
		$documentObj->slug = StringUtil::slugify($postValues['title']);
52
		$documentObj->type = 'document';
53
		$documentObj->documentType = $documentType->title;
54
		$documentObj->documentTypeSlug = $documentType->slug;
55
		$documentObj->state = isset($postValues['state']) ? 'published' : 'unpublished';
56
		$documentObj->lastModificationDate = time();
57
		$documentObj->creationDate = isset($postValues['creationDate']) ? intval($postValues['creationDate']) : time();
58
		$documentObj->lastModifiedBy = $_SESSION['cloudcontrol']->username;
59
60
		return $documentObj;
61
	}
62
63
	/**
64
	 * @param array $postValues
65
	 * @param Document $documentObj
66
	 * @param array $staticBricks
67
	 *
68
	 * @return Document
69
	 */
70
	private static function createBrickArrayForDocument($postValues, $documentObj, $staticBricks)
71
	{
72
		if (isset($postValues['bricks'])) {
73
			foreach ($postValues['bricks'] as $brickSlug => $brick) {
74
				// Find the current bricktype and check if its multiple
75
				list($staticBrick, $multiple) = self::getStaticBrickAndSetMultiple($staticBricks, $brickSlug);
76
77
				if ($multiple) {
78
					$documentObj = self::addMultipleBricks($documentObj, $brick, $staticBrick, $brickSlug);
79
				} else {
80
					$documentObj = self::addSingleBrick($documentObj, $brick, $brickSlug);
81
				}
82
			}
83
		}
84
		return $documentObj;
85
	}
86
87
	/**
88
	 * @param array $postValues
89
	 * @param Document $documentObj
90
	 *
91
	 * @return Document
92
	 */
93
	private static function createDynamicBrickArrayForDocument($postValues, $documentObj)
94
	{
95
		$documentObj->dynamicBricks = array();
96
		if (isset($postValues['dynamicBricks'])) {
97
			foreach ($postValues['dynamicBricks'] as $brickTypeSlug => $brick) {
98
				foreach ($brick as $brickContent) {
99
					$brickObj = new \stdClass();
100
					$brickObj->type = $brickTypeSlug;
101
					$brickObj->fields = $brickContent;
102
					$dynamicBricks = $documentObj->dynamicBricks;
103
					$dynamicBricks[] = $brickObj;
104
					$documentObj->dynamicBricks = $dynamicBricks;
105
				}
106
			}
107
		}
108
		return $documentObj;
109
	}
110
111
	/**
112
	 * @param $staticBricks
113
	 * @param $brickSlug
114
	 *
115
	 * @return array
116
	 */
117
	private static function getStaticBrickAndSetMultiple($staticBricks, $brickSlug)
118
	{
119
		$staticBrick = null;
120
		$multiple = false;
121
		foreach ($staticBricks as $staticBrick) {
122
			if ($staticBrick->slug === $brickSlug) {
123
				$multiple = $staticBrick->multiple;
124
				break;
125
			}
126
		}
127
128
		return array($staticBrick, $multiple);
129
	}
130
131
	/**
132
	 * @param $staticBrick
133
	 * @param $brickInstance
134
	 *
135
	 * @return \stdClass
136
	 */
137
	private static function createBrick($staticBrick, $brickInstance)
138
	{
139
		$brickObj = new \stdClass();
140
		$brickObj->fields = new \stdClass();
141
		$brickObj->type = $staticBrick->brickSlug;
142
143
		foreach ($brickInstance['fields'] as $fieldName => $fieldValues) {
144
			$brickObj->fields->$fieldName = $fieldValues;
145
		}
146
147
		return $brickObj;
148
	}
149
150
	/**
151
	 * @param $documentObj
152
	 * @param $brick
153
	 * @param $staticBrick
154
	 * @param $brickSlug
155
	 *
156
	 * @return mixed
157
	 */
158
	private static function addMultipleBricks($documentObj, $brick, $staticBrick, $brickSlug)
159
	{
160
		$brickArray = array();
161
		foreach ($brick as $brickInstance) {
162
			$brickObj = self::createBrick($staticBrick, $brickInstance);
163
			$brickArray[] = $brickObj;
164
		}
165
166
		$bricks = $documentObj->bricks;
167
		$bricks[$brickSlug] = $brickArray;
168
		$documentObj->bricks = $bricks;
169
170
		return $documentObj;
171
	}
172
173
	/**
174
	 * @param $documentObj
175
	 * @param $brick
176
	 * @param $brickSlug
177
	 *
178
	 * @return mixed
179
	 */
180
	private static function addSingleBrick($documentObj, $brick, $brickSlug)
181
	{
182
		$bricks = $documentObj->bricks;
183
		$bricks[$brickSlug] = $brick;
184
		$documentObj->bricks = $bricks;
185
		return $documentObj;
186
	}
187
}