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

ProductUnit::loadCacheList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
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
	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
			'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
	/** {@inheritdoc} */
50
	public function getYfValue($apiValue, array $field)
51
	{
52
		$this->loadCacheList();
53
		$key = array_search($apiValue, $this->cacheList);
54
		return $key ?? null;
55
	}
56
57
	/** {@inheritdoc} */
58
	public function getApiValue($yfValue, array $field)
59
	{
60
		$this->loadCacheList();
61
		if ($value = $this->cacheList[$yfValue] ?? null) {
62
			return $value;
63
		}
64
		if ($value = $this->cacheList[\App\Language::translate($yfValue, 'Products', 'pl-PL')] ?? null) {
65
			return $value;
66
		}
67
		return null;
68
	}
69
70
	/**
71
	 * Get picklist values.
72
	 *
73
	 * @return array
74
	 */
75
	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...
76
	{
77
		$picklistValues = \App\Fields\Picklist::getValues($this->fieldModel->getName());
78
		$values = [];
79
		foreach ($picklistValues as $value) {
80
			$values[trim(mb_strtolower($value['picklistValue']), '.')] = $value['picklistValue'];
81
			$values[trim(mb_strtolower(\App\Language::translate(
82
				$value['picklistValue'],
83
				'Products',
84
				'pl-PL'
85
			)), '.')] = $value['picklistValue'];
86
		}
87
		return $values;
88
	}
89
90
	/**
91
	 * Get all unit measure from API.
92
	 *
93
	 * @return array|null
94
	 */
95
	private function getAllFromApi(): ?array
96
	{
97
		if (null === $this->cache) {
98
			$this->cache = [];
99
			try {
100
				foreach ($this->getFromApi('Dictionary/UnitMeasure') as $value) {
101
					if (empty($value['key'])) {
102
						continue;
103
					}
104
					$this->cache[$value['key']] = trim($value['key'], '.');
105
				}
106
			} catch (\Throwable $ex) {
107
				$this->logError('getAllFromApi ' . $this->name, null, $ex);
108
			}
109
		}
110
		return $this->cache;
111
	}
112
113
	/**
114
	 * Load cache list.
115
	 *
116
	 * @return void
117
	 */
118
	private function loadCacheList(): void
119
	{
120
		if (empty($this->cacheList)) {
121
			$this->process();
122
		}
123
	}
124
}
125