Test Setup Failed
Push — developer ( 4955d5...3912ea )
by Mariusz
16:57
created

PaymentMethods   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 44
c 1
b 0
f 0
dl 0
loc 100
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 9 2
A getApiValue() 0 10 3
A getAllFromApi() 0 16 5
A getPicklistValues() 0 16 4
A loadCacheList() 0 4 2
A getYfValue() 0 5 1
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
	use \App\Integrations\Traits\SynchronizerPicklist;
24
25
	/** @var array Cache for data from the API */
26
	private $cache;
27
	/** @var array ID by name cache from the API */
28
	private $cacheList = [];
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
			'payment_methods',
37
			\Vtiger_Module_Model::getInstance('Accounts')
38
		);
39
		if ($this->fieldModel->isActiveField()) {
40
			$this->getAllFromApi();
41
			$this->import();
42
		}
43
	}
44
45
	/** {@inheritdoc} */
46
	public function getYfValue($apiValue, array $field)
47
	{
48
		$this->loadCacheList();
49
		$key = array_search($apiValue, $this->cacheList);
50
		return $key ?? '';
51
	}
52
53
	/** {@inheritdoc} */
54
	public function getApiValue($yfValue, array $field)
55
	{
56
		$this->loadCacheList();
57
		if ($value = $this->cacheList[$yfValue] ?? null) {
58
			return $value;
59
		}
60
		if ($value = $this->cacheList[\App\Language::translate($yfValue, 'Accounts', 'pl-PL')] ?? null) {
61
			return $value;
62
		}
63
		return null;
64
	}
65
66
	/**
67
	 * Get picklist values.
68
	 *
69
	 * @return array
70
	 */
71
	private function getPicklistValues(): array
0 ignored issues
show
Unused Code introduced by
The method getPicklistValues() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
72
	{
73
		$picklistValues = \App\Fields\Picklist::getValues($this->fieldModel->getName());
74
		$values = [];
75
		foreach ($picklistValues as $value) {
76
			$values[mb_strtolower($value['picklistValue'])] = $value['picklistValue'];
77
			$values[mb_strtolower(\App\Language::translate(
78
				$value['picklistValue'],
79
				'Accounts',
80
				'pl-PL'
81
			))] = $value['picklistValue'];
82
		}
83
		if (\in_array('Przelew', array_column($this->cache, 'kon_Wartosc')) && isset($values['pll_transfer'])) {
84
			$values['przelew'] = $values['pll_transfer'];
85
		}
86
		return $values;
87
	}
88
89
	/**
90
	 * Get all account type from API.
91
	 *
92
	 * @return array
93
	 */
94
	private function getAllFromApi(): array
95
	{
96
		if (null === $this->cache) {
97
			$this->cache = [];
98
			try {
99
				foreach ($this->getFromApi('PaymentMethod/Get') as $value) {
100
					if (empty($value['kon_Wartosc'])) {
101
						continue;
102
					}
103
					$this->cache[$value['kon_Lp']] = $value['kon_Wartosc'];
104
				}
105
			} catch (\Throwable $ex) {
106
				$this->logError('getAllFromApi ' . $this->name, null, $ex);
107
			}
108
		}
109
		return $this->cache;
110
	}
111
112
	/**
113
	 * Load cache list.
114
	 *
115
	 * @return void
116
	 */
117
	private function loadCacheList(): void
118
	{
119
		if (empty($this->cacheList)) {
120
			$this->process();
121
		}
122
	}
123
}
124