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

ProductUnit::import()   B

Complexity

Conditions 10
Paths 144

Size

Total Lines 43
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 43
rs 7.2999
c 1
b 0
f 0
cc 10
nc 144
nop 0

How to fix   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
/**
4
 * Comarch product unit synchronization file.
5
 *
6
 * The file is part of the paid functionality. Using the file is allowed only after purchasing a subscription.
7
 * File modification allowed only with the consent of the system producer.
8
 *
9
 * @package Integration
10
 *
11
 * @copyright YetiForce S.A.
12
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
13
 * @author    Mariusz Krzaczkowski <[email protected]>
14
 */
15
16
namespace App\Integrations\Comarch\Xl\Synchronizer;
17
18
/**
19
 * Comarch product unit synchronization class.
20
 */
21
class ProductUnit extends \App\Integrations\Comarch\Synchronizer
22
{
23
	/** @var array Cache for data from the API */
24
	private $cache;
25
	/** @var array ID by name cache from the API */
26
	private $cacheList = [];
27
	/** @var int[] */
28
	private $roleIdList = [];
29
	/** @var \Settings_Picklist_Field_Model */
30
	private $fieldModel;
31
32
	/** {@inheritdoc} */
33
	public function process(): void
34
	{
35
		$this->fieldModel = \Settings_Picklist_Field_Model::getInstance(
36
			'usageunit',
37
			\Vtiger_Module_Model::getInstance('Products')
38
		);
39
		if ($this->fieldModel->isActiveField()) {
40
			$this->getAllFromApi();
41
			if (null !== $this->cache) {
42
				$this->import();
43
			} else {
44
				$this->controller->log('Skip import ' . $this->name, []);
45
			}
46
		}
47
	}
48
49
	/**
50
	 * Import account type from API.
51
	 *
52
	 * @return void
53
	 */
54
	public function import(): void
55
	{
56
		if ($this->config->get('log_all')) {
57
			$this->controller->log('Start import ' . $this->name, []);
58
		}
59
		$isRoleBased = $this->fieldModel->isRoleBased();
60
		$fieldName = $this->fieldModel->getName();
61
		$picklistValues = \App\Fields\Picklist::getValues($fieldName);
62
		$values = [];
63
		foreach ($picklistValues as $value) {
64
			$values[trim(mb_strtolower($value['picklistValue']), '.')] = $value['picklistValue'];
65
			$values[trim(mb_strtolower(\App\Language::translate($value['picklistValue'], 'Products')), '.')] = $value['picklistValue'];
66
		}
67
		$i = 0;
68
		foreach ($this->cache as $value) {
69
			if (empty($value['key'])) {
70
				continue;
71
			}
72
			$name = mb_strtolower(trim($value['key'], '.'));
73
			if (empty($values[$name])) {
74
				try {
75
					$itemModel = $this->fieldModel->getItemModel();
76
					$itemModel->validateValue('name', $value['key']);
77
					$itemModel->set('name', $value['key']);
78
					if ($isRoleBased) {
79
						if (empty($this->roleIdList)) {
80
							$this->roleIdList = array_keys(\Settings_Roles_Record_Model::getAll());
81
						}
82
						$itemModel->set('roles', $this->roleIdList);
83
					}
84
					$itemModel->save();
85
					$this->cacheList[$value['key']] = $value['key'];
86
					++$i;
87
				} catch (\Throwable $ex) {
88
					$this->controller->log('Import ' . $this->name, ['API' => $value], $ex);
89
					\App\Log::error("Error during import {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY);
90
				}
91
			} else {
92
				$this->cacheList[$values[$name]] = $value['key'];
93
			}
94
		}
95
		if ($this->config->get('log_all')) {
96
			$this->controller->log('End import ' . $this->name, ['imported' => $i]);
97
		}
98
	}
99
100
	/**
101
	 * Get all unit measure from API.
102
	 *
103
	 * @return array|null
104
	 */
105
	private function getAllFromApi(): ?array
106
	{
107
		if (null === $this->cache) {
108
			try {
109
				$this->cache = $this->getFromApi('Dictionary/UnitMeasure');
110
			} catch (\Throwable $ex) {
111
				$this->controller->log('Get ' . $this->name, null, $ex);
112
				\App\Log::error("Error during getAllFromApi {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY);
113
			}
114
		}
115
		return $this->cache;
116
	}
117
118
	/** {@inheritdoc} */
119
	public function getYfValue($apiValue, array $field)
120
	{
121
		$this->loadCacheList();
122
		$key = array_search($apiValue, $this->cacheList);
123
		return $key ?? null;
124
	}
125
126
	/** {@inheritdoc} */
127
	public function getApiValue($yfValue, array $field)
128
	{
129
		$this->loadCacheList();
130
		if ($value = $this->cacheList[$yfValue] ?? null) {
131
			return $value;
132
		}
133
		if ($value = $this->cacheList[\App\Language::translate($yfValue, 'Products')] ?? null) {
134
			return $value;
135
		}
136
		return null;
137
	}
138
139
	/**
140
	 * Load cache list.
141
	 *
142
	 * @return void
143
	 */
144
	private function loadCacheList(): void
145
	{
146
		if (empty($this->cacheList)) {
147
			$this->process();
148
		}
149
	}
150
}
151