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

Products   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
eloc 72
dl 0
loc 113
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 22 11
D import() 0 53 15
A importById() 0 18 3
1
<?php
2
3
/**
4
 * Comarch accounts 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 accounts synchronization class.
20
 */
21
class Products extends \App\Integrations\Comarch\Synchronizer
22
{
23
	/** @var string The name of the configuration parameter for rows limit */
24
	const LIMIT_NAME = 'products_limit';
25
26
	/** {@inheritdoc} */
27
	public function process(): void
28
	{
29
		$mapModel = $this->getMapModel();
30
		if (\App\Module::isModuleActive($mapModel->getModule())) {
31
			$direction = (int) $this->config->get('direction_products');
32
			if ($this->config->get('master')) {
33
				if (self::DIRECTION_TWO_WAY === $direction || self::DIRECTION_YF_TO_API === $direction) {
34
					$this->runQueue('export');
35
					$this->export();
36
				}
37
				if (self::DIRECTION_TWO_WAY === $direction || self::DIRECTION_API_TO_YF === $direction) {
38
					$this->runQueue('import');
39
					$this->import();
40
				}
41
			} else {
42
				if (self::DIRECTION_TWO_WAY === $direction || self::DIRECTION_API_TO_YF === $direction) {
43
					$this->runQueue('import');
44
					$this->import();
45
				}
46
				if (self::DIRECTION_TWO_WAY === $direction || self::DIRECTION_YF_TO_API === $direction) {
47
					$this->runQueue('export');
48
					$this->export();
49
				}
50
			}
51
		}
52
	}
53
54
	/**
55
	 * Import accounts from Comarch.
56
	 *
57
	 * @return void
58
	 */
59
	public function import(): void
60
	{
61
		$this->lastScan = $this->config->getLastScan('import' . $this->name);
62
		if (
63
			!$this->lastScan['start_date']
64
			|| (0 === $this->lastScan['id'] && $this->lastScan['start_date'] === $this->lastScan['end_date'])
65
		) {
66
			$this->config->setScan('import' . $this->name);
67
			$this->lastScan = $this->config->getLastScan('import' . $this->name);
68
		}
69
		if ($this->config->get('log_all')) {
70
			$this->controller->log('Start import ' . $this->name, [
71
				'lastScan' => $this->lastScan,
72
			]);
73
		}
74
		$i = 0;
75
		try {
76
			$page = $this->lastScan['page'] ?? 1;
77
			$load = true;
78
			$finish = false;
79
			$limit = $this->config->get(self::LIMIT_NAME);
80
			while ($load) {
81
				if ($rows = $this->getFromApi('Product/GetFiltered?&page=' . $page . '&' . $this->getFromApiCond())) {
82
					foreach ($rows as $id => $row) {
83
						$this->importItem($row);
84
						$this->config->setScan('import' . $this->name, 'id', $id);
85
						++$i;
86
					}
87
					++$page;
88
					if (\is_callable($this->controller->bathCallback)) {
89
						$load = \call_user_func($this->controller->bathCallback, 'import' . $this->name);
90
					}
91
					if ($limit !== \count($rows)) {
92
						$finish = true;
93
					}
94
				} else {
95
					$finish = true;
96
				}
97
				if ($finish || !$load) {
98
					$load = false;
99
					if ($finish) {
100
						$this->config->setEndScan('import' . $this->name, $this->lastScan['start_date']);
101
					} else {
102
						$this->config->setScan('import' . $this->name, 'page', $page);
103
					}
104
				}
105
			}
106
		} catch (\Throwable $ex) {
107
			$this->controller->log('Import ' . $this->name, null, $ex);
108
			\App\Log::error("Error during import {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY);
109
		}
110
		if ($this->config->get('log_all')) {
111
			$this->controller->log('End import ' . $this->name, ['imported' => $i]);
112
		}
113
	}
114
115
	/** {@inheritdoc} */
116
	public function importById(int $apiId): int
117
	{
118
		$id = 0;
119
		try {
120
			$row = $this->getFromApi('Product/GetById/' . $apiId);
121
			if ($row) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
				$this->importItem($row);
123
				$mapModel = $this->getMapModel();
124
				$id = $this->imported[$row[$mapModel::API_NAME_ID]] ?? 0;
125
			} else {
126
				$this->controller->log("Import {$this->name} by id [Empty details]", ['apiId' => $apiId]);
127
				\App\Log::error("Import during export {$this->name}: Empty details", self::LOG_CATEGORY);
128
			}
129
		} catch (\Throwable $ex) {
130
			$this->controller->log("Import {$this->name} by id", ['apiId' => $apiId, 'API' => $row ?? []], $ex);
131
			\App\Log::error("Error during import by id {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY);
132
		}
133
		return $id;
134
	}
135
}
136