Passed
Push — developer ( b6ebe7...0bf5e9 )
by Mariusz
47:54 queued 29:05
created

Settings_Comarch_Activation_Model::activate()   C

Complexity

Conditions 11
Paths 176

Size

Total Lines 105
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 89
dl 0
loc 105
rs 5.5866
c 1
b 0
f 0
cc 11
nc 176
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Activation file for Comarch integration model.
4
 *
5
 * @package Settings.Model
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
use App\Integrations\Comarch;
13
14
/**
15
 * Activation class for Comarch integration model.
16
 */
17
class Settings_Comarch_Activation_Model
18
{
19
	/** @var array Map relation table name */
20
	private const FIELDS = [
21
		'Accounts' => [
22
			'block' => ['name' => 'LBL_COMARCH_BLOCK', 'create' => false],
23
			'fields' => [
24
				'comarch_server_id', 'comarch_id', 'account_short_name', 'account_second_name',
25
				'account_third_name', 'payment_methods'
26
			],
27
			'fieldsData' => ['comarch_server_id' => ['displaytype' => 1]],
28
		],
29
		'Products' => [
30
			'block' => ['name' => 'LBL_COMARCH_BLOCK', 'create' => true],
31
			'fields' => [
32
				'comarch_server_id', 'comarch_id',
33
			],
34
			'fieldsData' => ['comarch_server_id' => ['displaytype' => 1]],
35
		],
36
	];
37
38
	/**
39
	 * Get fields structure.
40
	 *
41
	 * @return array
42
	 */
43
	private static function getFieldsStructure(): array
44
	{
45
		$importerType = new \App\Db\Importers\Base();
46
		return [
47
			'comarch_server_id' => [
48
				'columntype' => $importerType->integer(10)->defaultValue(0)->notNull()->unsigned(),
49
				'label' => 'FL_COMARCH_SERVER',
50
				'uitype' => 334,
51
				'maximumlength' => '4294967295',
52
				'typeofdata' => 'I~O'
53
			],
54
			'comarch_id' => [
55
				'columntype' => $importerType->integer(10)->unsigned(),
56
				'label' => 'FL_COMARCH_ID',
57
				'uitype' => 7, 'displaytype' => 2,
58
				'maximumlength' => '4294967295', 'typeofdata' => 'I~O'
59
			],
60
			'account_short_name' => [
61
				'label' => 'FL_ACCOUNT_SHORT_NAME', 'columntype' => $importerType->stringType(255)->defaultValue(''),
62
				'uitype' => 1, 'maximumlength' => '255', 'typeofdata' => 'V~M'
63
			],
64
			'account_second_name' => [
65
				'label' => 'FL_ACCOUNT_SECOND_NAME', 'columntype' => $importerType->stringType(255)->defaultValue(''),
66
				'uitype' => 1, 'maximumlength' => '255', 'typeofdata' => 'V~O'
67
			],
68
			'account_third_name' => [
69
				'label' => 'FL_ACCOUNT_THIRD_NAME', 'columntype' => $importerType->stringType(255)->defaultValue(''),
70
				'uitype' => 1, 'maximumlength' => '255', 'typeofdata' => 'V~O'
71
			],
72
			'payment_methods' => [
73
				'label' => 'FL_PAYMENTS_METHOD', 'columntype' => $importerType->stringType(255)->defaultValue(''),
74
				'column' => 'accounts_formpayment', 'uitype' => 16, 'maximumlength' => '255', 'typeofdata' => 'V~O'
75
			],
76
		];
77
	}
78
79
	/**
80
	 * Check if the functionality has been activated.
81
	 *
82
	 * @return bool
83
	 */
84
	public static function check(): bool
85
	{
86
		$condition = ['or'];
87
		$i = 0;
88
		foreach (self::FIELDS as $moduleName => $value) {
89
			foreach ($value['fields'] as $fieldName) {
90
				++$i;
91
				$condition[] = ['tabid' => \App\Module::getModuleId($moduleName), 'fieldname' => $fieldName];
92
			}
93
		}
94
		return \App\Db::getInstance('log')->isTableExists(Comarch::LOG_TABLE_NAME)
95
		&& \App\Db::getInstance('admin')->isTableExists(Comarch::MAP_TABLE_NAME)
96
		&& \App\Db::getInstance('admin')->isTableExists(Comarch::CONFIG_TABLE_NAME)
97
		&& \App\Db::getInstance('admin')->isTableExists(Comarch::QUEUE_TABLE_NAME)
98
		&& $i === (new \App\Db\Query())->from('vtiger_field')->where($condition)->count()
99
		&& \App\EventHandler::checkActive('Products_DuplicateEan_Handler', 'EditViewPreSave')
100
		&& \App\Cron::checkActive('Vtiger_Comarch_Cron');
101
	}
102
103
	/**
104
	 * Activate integration, requires creation of additional integration data.
105
	 *
106
	 * @return bool
107
	 */
108
	public static function activate(): int
109
	{
110
		$fields = self::getFieldsStructure();
111
		$i = 0;
112
		foreach (self::FIELDS as $moduleName => $value) {
113
			$fieldsExists = (new \App\Db\Query())->select(['fieldname'])->from('vtiger_field')
114
				->where(['tabid' => \App\Module::getModuleId($moduleName), 'fieldname' => array_keys($fields)])->column();
115
			if ($fieldsToAdd = array_diff_key(array_intersect_key($fields, array_flip($value['fields'])), array_flip($fieldsExists))) {
116
				$blockModel = vtlib\Block::getInstance($value['block']['name'], $moduleName);
117
				if (!$blockModel) {
118
					if ($value['block']['create']) {
119
						$blockModel = new vtlib\Block();
120
						$blockModel->label = $value['block']['name'];
121
						vtlib\Module::getInstance($moduleName)->addBlock($blockModel);
122
					} else {
123
						$blocks = vtlib\Block::getAllForModule(vtlib\Module::getInstance($moduleName));
124
						$blockModel = current($blocks);
125
					}
126
				}
127
				foreach ($fieldsToAdd as $fieldName => &$fieldData) {
128
					if (isset($value['fieldsData'][$fieldName])) {
129
						$fieldData = array_merge($fieldData, $value['fieldsData'][$fieldName]);
130
					}
131
				}
132
				self::addFields($fieldsToAdd, $blockModel);
133
				$i += \count($fieldsToAdd);
134
			}
135
		}
136
		$importer = new \App\Db\Importers\Base();
137
		$dbLog = \App\Db::getInstance('log');
138
		if (!$dbLog->isTableExists(Comarch::LOG_TABLE_NAME)) {
139
			$dbLog->createTable(Comarch::LOG_TABLE_NAME, [
140
				'id' => $importer->primaryKeyUnsigned(),
141
				'server_id' => $importer->integer(10)->unsigned()->notNull(),
142
				'time' => $importer->dateTime()->notNull(),
143
				'error' => $importer->tinyInteger(1)->unsigned()->defaultValue(0),
144
				'message' => $importer->stringType(255),
145
				'params' => $importer->text(),
146
				'trace' => $importer->text(),
147
			]);
148
			++$i;
149
		}
150
		$db = \App\Db::getInstance('admin');
151
		$tableServer = $db->convertTablePrefix(Comarch::TABLE_NAME);
152
		if (!$db->isTableExists(Comarch::MAP_TABLE_NAME)) {
153
			$table = $db->convertTablePrefix(Comarch::MAP_TABLE_NAME);
154
			$db->createTable(Comarch::MAP_TABLE_NAME, [
155
				'server_id' => $importer->integer(10)->unsigned()->notNull(),
156
				'map' => $importer->stringType(50)->notNull(),
157
				'class' => $importer->stringType(100)->notNull(),
158
			]);
159
			$db->createCommand()
160
				->createIndex($table . '_server_id_idx', Comarch::MAP_TABLE_NAME, 'server_id')
161
				->execute();
162
			$db->createCommand()->addForeignKey(
163
				$table . '_ibfk_1',
164
				Comarch::MAP_TABLE_NAME, 'server_id',
165
				$tableServer, 'id',
166
				'CASCADE', null
167
			)->execute();
168
			++$i;
169
		}
170
		if (!$db->isTableExists(Comarch::CONFIG_TABLE_NAME)) {
171
			$table = $db->convertTablePrefix(Comarch::CONFIG_TABLE_NAME);
172
			$db->createTable(Comarch::CONFIG_TABLE_NAME, [
173
				'server_id' => $importer->integer(10)->unsigned()->notNull(),
174
				'name' => $importer->stringType(50)->notNull(),
175
				'value' => $importer->stringType(50)->null(),
176
			]);
177
			$db->createCommand()
178
				->createIndex($table . '_server_id_idx', Comarch::CONFIG_TABLE_NAME, 'server_id')
179
				->execute();
180
			$db->createCommand()->addForeignKey(
181
				$table . '_id_ibfk_1',
182
				Comarch::CONFIG_TABLE_NAME, 'server_id',
183
				$tableServer, 'id',
184
				'CASCADE', null
185
			)->execute();
186
			++$i;
187
		}
188
		if (!$db->isTableExists(Comarch::QUEUE_TABLE_NAME)) {
189
			$table = $db->convertTablePrefix(Comarch::QUEUE_TABLE_NAME);
190
			$db->createTable(Comarch::QUEUE_TABLE_NAME, [
191
				'id' => $importer->primaryKeyUnsigned(),
192
				'server_id' => $importer->integer(10)->unsigned()->notNull(),
193
				'name' => $importer->stringType(50)->notNull(),
194
				'value' => $importer->stringType(50)->null(),
195
				'type' => $importer->stringType(50)->notNull(),
196
				'counter' => $importer->smallInteger(1)->notNull()->defaultValue(1),
197
			]);
198
			$db->createCommand()
199
				->createIndex($table . '_server_type_idx', Comarch::QUEUE_TABLE_NAME, ['server_id', 'name', 'type'])
200
				->execute();
201
			$db->createCommand()->addForeignKey(
202
				$table . '_ibfk_1',
203
				Comarch::QUEUE_TABLE_NAME, 'server_id',
204
				$tableServer, 'id',
205
				'CASCADE', null
206
			)->execute();
207
			++$i;
208
		}
209
		\App\EventHandler::setActive('Products_DuplicateEan_Handler', 'EditViewPreSave');
210
		\App\EventHandler::setActive('Accounts_DuplicateShortName_Handler', 'EditViewPreSave');
211
		\App\Cron::updateStatus(\App\Cron::STATUS_ENABLED, 'LBL_COMARCH');
212
		return $i;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $i returns the type integer which is incompatible with the documented return type boolean.
Loading history...
213
	}
214
215
	/**
216
	 * Add fields.
217
	 *
218
	 * @param array       $fieldsToAdd
219
	 * @param vtlib\Block $blockModel
220
	 *
221
	 * @return void
222
	 */
223
	public static function addFields(array $fieldsToAdd, vtlib\Block $blockModel): void
224
	{
225
		foreach ($fieldsToAdd as $fieldName => $fieldData) {
226
			if (empty($fieldData['table'])) {
227
				$fieldData['table'] = $blockModel->module->basetable;
228
			}
229
			$fieldInstance = \Vtiger_Field_Model::init($blockModel->module->name, $fieldData, $fieldName);
230
			$fieldInstance->save($blockModel);
231
			if (isset($fieldData['values'])) {
232
				$fieldInstance->setNoRolePicklistValues($fieldData['values']);
233
			}
234
			if (isset($fieldData['referenceModule'])) {
235
				if (!\is_array($fieldData['referenceModule'])) {
236
					$moduleList[] = $fieldData['referenceModule'];
237
				} else {
238
					$moduleList = $fieldData['referenceModule'];
239
				}
240
				$fieldInstance->setRelatedModules($moduleList);
241
				foreach ($moduleList as $module) {
242
					$targetModule = vtlib\Module::getInstance($module);
243
					$targetModule->setRelatedList(
244
						$blockModel->module,
245
						$blockModel->module->name,
246
						['Add'], 'getDependentsList', $fieldName
247
					);
248
				}
249
			}
250
		}
251
	}
252
}
253