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

Accounts::import()   F

Complexity

Conditions 16
Paths 288

Size

Total Lines 56
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 56
rs 3.6333
c 1
b 0
f 0
cc 16
nc 288
nop 0

How to fix   Long Method    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 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 Accounts extends \App\Integrations\Comarch\Synchronizer
22
{
23
	/** @var string The name of the configuration parameter for rows limit */
24
	const LIMIT_NAME = 'accounts_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_accounts');
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('Customer/GetAll?&page=' . $page . '&' . $this->getFromApiCond())) {
82
					foreach ($rows as $id => $row) {
83
						if ('JEDNORAZOWY' === $row['knt_Akronim']) {
84
							continue;
85
						}
86
						$this->importItem($row);
87
						$this->config->setScan('import' . $this->name, 'id', $id);
88
						++$i;
89
					}
90
					++$page;
91
					if (\is_callable($this->controller->bathCallback)) {
92
						$load = \call_user_func($this->controller->bathCallback, 'import' . $this->name);
93
					}
94
					if ($limit !== \count($rows)) {
95
						$finish = true;
96
					}
97
				} else {
98
					$finish = true;
99
				}
100
				if ($finish || !$load) {
101
					$load = false;
102
					if ($finish) {
103
						$this->config->setEndScan('import' . $this->name, $this->lastScan['start_date']);
104
					} else {
105
						$this->config->setScan('import' . $this->name, 'page', $page);
106
					}
107
				}
108
			}
109
		} catch (\Throwable $ex) {
110
			$this->controller->log('Import ' . $this->name, null, $ex);
111
			\App\Log::error("Error during import {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY);
112
		}
113
		if ($this->config->get('log_all')) {
114
			$this->controller->log('End import ' . $this->name, ['imported' => $i]);
115
		}
116
	}
117
118
	/** {@inheritdoc} */
119
	public function importById(int $apiId): int
120
	{
121
		$id = 0;
122
		try {
123
			$row = $this->getFromApi('Customer/GetById/' . $apiId);
124
			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...
125
				$this->importItem($row);
126
				$mapModel = $this->getMapModel();
127
				$id = $this->imported[$row[$mapModel::API_NAME_ID]] ?? 0;
128
			} else {
129
				$this->controller->log("Import {$this->name} by id [Empty details]", ['apiId' => $apiId]);
130
				\App\Log::error("Import during export {$this->name}: Empty details", self::LOG_CATEGORY);
131
			}
132
		} catch (\Throwable $ex) {
133
			$this->controller->log("Import {$this->name} by id", ['apiId' => $apiId, 'API' => $row ?? []], $ex);
134
			\App\Log::error("Error during import by id {$this->name}: \n{$ex->__toString()}", self::LOG_CATEGORY);
135
		}
136
		return $id;
137
	}
138
}
139