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

PaymentMethods::import()   B

Complexity

Conditions 10
Paths 144

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 39
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 payment methods 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 payment methods synchronization class.
20
 */
21
class PaymentMethods 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 \Settings_Picklist_Field_Model */
28
	private $fieldModel;
29
30
	/** {@inheritdoc} */
31
	public function process(): void
32
	{
33
		$this->fieldModel = \Settings_Picklist_Field_Model::getInstance(
34
			'payment_methods',
35
			\Vtiger_Module_Model::getInstance('Accounts')
36
		);
37
		if ($this->fieldModel->isActiveField()) {
38
			$this->getAllFromApi();
39
			$this->import();
40
		}
41
	}
42
43
	/**
44
	 * Import account type from API.
45
	 *
46
	 * @return void
47
	 */
48
	public function import(): void
49
	{
50
		if ($this->config->get('log_all')) {
51
			$this->controller->log('Start import ' . $this->name, []);
52
		}
53
		$fieldName = $this->fieldModel->getName();
54
		$picklistValues = \App\Fields\Picklist::getValues($fieldName);
55
		$values = [];
56
		foreach ($picklistValues as $value) {
57
			$values[mb_strtolower($value['picklistValue'])] = $value['picklistValue'];
58
			$values[mb_strtolower(\App\Language::translate($value['picklistValue'], 'Accounts'))] = $value['picklistValue'];
59
		}
60
		if (\in_array('Przelew', array_column($this->cache, 'kon_Wartosc')) && isset($values['pll_transfer'])) {
61
			$values['przelew'] = $values['pll_transfer'];
62
		}
63
		$i = 0;
64
		foreach ($this->cache as $value) {
65
			if (empty($value['kon_Wartosc'])) {
66
				continue;
67
			}
68
			$name = mb_strtolower($value['kon_Wartosc']);
69
			if (empty($values[$name])) {
70
				try {
71
					$itemModel = $this->fieldModel->getItemModel();
72
					$itemModel->validateValue('name', $value['kon_Wartosc']);
73
					$itemModel->set('name', $value['kon_Wartosc']);
74
					$itemModel->save();
75
					$this->cacheList[$value['kon_Wartosc']] = $value['kon_Lp'];
76
					++$i;
77
				} catch (\Throwable $ex) {
78
					$this->controller->log('Import ' . $this->name, ['API' => $value], $ex);
79
					\App\Log::error("Error during import {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY);
80
				}
81
			} else {
82
				$this->cacheList[$values[$name]] = $value['kon_Lp'];
83
			}
84
		}
85
		if ($this->config->get('log_all')) {
86
			$this->controller->log('End import ' . $this->name, ['imported' => $i]);
87
		}
88
	}
89
90
	/**
91
	 * Get all account type from API.
92
	 *
93
	 * @return array
94
	 */
95
	private function getAllFromApi(): array
96
	{
97
		if (null === $this->cache) {
98
			$this->cache = [];
99
			try {
100
				$this->cache = $this->getFromApi('PaymentMethod/Get');
101
			} catch (\Throwable $ex) {
102
				$this->controller->log('Get ' . $this->name, null, $ex);
103
				\App\Log::error("Error during getAllFromApi {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY);
104
			}
105
		}
106
		return $this->cache;
107
	}
108
109
	/** {@inheritdoc} */
110
	public function getYfValue($apiValue, array $field)
111
	{
112
		$this->loadCacheList();
113
		$key = array_search($apiValue, $this->cacheList);
114
		return $key ?? '';
115
	}
116
117
	/** {@inheritdoc} */
118
	public function getApiValue($yfValue, array $field)
119
	{
120
		$this->loadCacheList();
121
		if ($value = $this->cacheList[$yfValue] ?? null) {
122
			return $value;
123
		}
124
		if ($value = $this->cacheList[\App\Language::translate($yfValue, 'Accounts')] ?? null) {
125
			return $value;
126
		}
127
		return null;
128
	}
129
130
	/**
131
	 * Load cache list.
132
	 *
133
	 * @return void
134
	 */
135
	private function loadCacheList(): void
136
	{
137
		if (empty($this->cacheList)) {
138
			$this->process();
139
		}
140
	}
141
}
142