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

Config   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 84
dl 0
loc 192
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllServers() 0 15 3
A setScan() 0 19 3
A getConnectors() 0 13 5
A setEndScan() 0 31 4
A getServer() 0 11 3
A getInstance() 0 18 1
A getLastScan() 0 7 2
A reload() 0 5 1
1
<?php
2
/**
3
 * File to read and save configuration for integration with Comarch.
4
 *
5
 * The file is part of the paid functionality. Using the file is allowed only after purchasing a subscription.
6
 * File modification allowed only with the consent of the system producer.
7
 *
8
 * @package Integration
9
 *
10
 * @copyright YetiForce S.A.
11
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
12
 * @author    Mariusz Krzaczkowski <[email protected]>
13
 */
14
15
namespace App\Integrations\Comarch;
16
17
use App\Db\Query;
18
use App\Integrations\Comarch;
19
20
/**
21
 * Class to read and save configuration for integration with Comarch.
22
 */
23
class Config extends \App\Base
24
{
25
	/**
26
	 * Get all servers.
27
	 *
28
	 * @return array
29
	 */
30
	public static function getAllServers(): array
31
	{
32
		if (\App\Cache::has('Comarch|getAllServers', '')) {
33
			return \App\Cache::get('Comarch|getAllServers', '');
34
		}
35
		$servers = [];
36
		$dataReader = (new Query())->from(Comarch::TABLE_NAME)->where(['status' => 1])
37
			->createCommand(\App\Db::getInstance('admin'))->query();
38
		while ($row = $dataReader->read()) {
39
			$row['password'] = \App\Encryption::getInstance()->decrypt($row['password']);
40
			$servers[$row['id']] = $row;
41
		}
42
		$dataReader->close();
43
		\App\Cache::save('Comarch|getAllServers', '', $servers);
44
		return $servers;
45
	}
46
47
	/**
48
	 * Get server configuration by id.
49
	 *
50
	 * @param int $id
51
	 *
52
	 * @return array
53
	 */
54
	public static function getServer(int $id): array
55
	{
56
		$servers = self::getAllServers();
57
		if (empty($servers[$id])) {
58
			throw new \App\Exceptions\AppException('Comarch Server not found: ' . $id);
59
		}
60
		$server = $servers[$id];
61
		if (!empty($server['attributes'])) {
62
			$server['attributes'] = \App\Json::decode($server['attributes']);
63
		}
64
		return $server;
65
	}
66
67
	/**
68
	 * Function to get object to read configuration.
69
	 *
70
	 * @param int $serverId
71
	 *
72
	 * @return self
73
	 */
74
	public static function getInstance(int $serverId): self
75
	{
76
		$db = \App\Db::getInstance('admin');
77
		$instance = new self();
78
		$instance->setData(array_merge(
79
			self::getServer($serverId),
80
			\App\Config::component('IntegrationComarch', null, []),
81
			(new Query())->select(['name', 'value'])->from(Comarch::CONFIG_TABLE_NAME)
82
				->where(['server_id' => $serverId])
83
				->createCommand($db)->queryAllByGroup(),
84
			[
85
				'maps' => (new Query())->select(['map', 'class'])->from(Comarch::MAP_TABLE_NAME)
86
					->where(['server_id' => $serverId])
87
					->createCommand($db)->queryAllByGroup()
88
			]
89
		),
90
		);
91
		return $instance;
92
	}
93
94
	/**
95
	 * Save in db last scanned id.
96
	 *
97
	 * @param string      $type
98
	 * @param string|null $name
99
	 * @param int|null    $id
100
	 *
101
	 * @throws \yii\db\Exception
102
	 */
103
	public function setScan(string $type, ?string $name = null, ?int $id = null): void
104
	{
105
		$dbCommand = \App\Db::getInstance('admin')->createCommand();
106
		if (null !== $name) {
107
			$data = ['name' => "{$type}_last_{$name}",	'value' => $id];
108
		} else {
109
			$data = ['name' => $type . '_start_date', 'value' => date('Y-m-d H:i:s')];
110
		}
111
		if (!(new Query())->from(Comarch::CONFIG_TABLE_NAME)
112
			->where(['server_id' => $this->get('id'), 'name' => $data['name']])->exists()) {
113
			$data['server_id'] = $this->get('id');
114
			$dbCommand->insert(Comarch::CONFIG_TABLE_NAME, $data)->execute();
115
		}
116
		$dbCommand->update(
117
			Comarch::CONFIG_TABLE_NAME,
118
			$data,
119
			['server_id' => $this->get('id'), 'name' => $data['name']]
120
		)->execute();
121
		$this->set($data['name'], $data['value']);
122
	}
123
124
	/**
125
	 * Set end scan.
126
	 *
127
	 * @param string $type
128
	 * @param string $date
129
	 *
130
	 * @throws \yii\db\Exception
131
	 */
132
	public function setEndScan(string $type, string $date): void
133
	{
134
		$dbCommand = \App\Db::getInstance('admin')->createCommand();
135
		if (!$date) {
136
			$date = date('Y-m-d H:i:s');
137
		}
138
		$saveData = [
139
			[
140
				'name' => $type . '_end_date',
141
				'value' => $date,
142
			], [
143
				'name' => $type . '_last_id',
144
				'value' => 0,
145
			], [
146
				'name' => $type . '_last_page',
147
				'value' => null,
148
			],
149
		];
150
		foreach ($saveData as $data) {
151
			if (!(new Query())->from(Comarch::CONFIG_TABLE_NAME)
152
				->where(['server_id' => $this->get('id'), 'name' => $data['name']])->exists()) {
153
				$data['server_id'] = $this->get('id');
154
				$dbCommand->insert(Comarch::CONFIG_TABLE_NAME, $data)->execute();
155
			} else {
156
				$dbCommand->update(
157
					Comarch::CONFIG_TABLE_NAME,
158
					$data,
159
					['server_id' => $this->get('id'), 'name' => $data['name']]
160
				)->execute();
161
			}
162
			$this->set($data['name'], $data['value']);
163
		}
164
	}
165
166
	/**
167
	 * Get last scan information.
168
	 *
169
	 * @param string $type
170
	 *
171
	 * @return array
172
	 */
173
	public function getLastScan(string $type): array
174
	{
175
		return [
176
			'id' => (int) $this->get($type . '_last_id') ?? 0,
177
			'page' => $this->get($type . '_last_page') ?: null,
178
			'start_date' => $this->get($type . '_start_date') ?? false,
179
			'end_date' => $this->get($type . '_end_date') ?? false,
180
		];
181
	}
182
183
	/**
184
	 * Reload integration with Comarch.
185
	 *
186
	 * @param int $id
187
	 *
188
	 * @return void
189
	 */
190
	public static function reload(int $id): void
191
	{
192
		\App\Db::getInstance('admin')->createCommand()
193
			->delete(Comarch::CONFIG_TABLE_NAME, ['server_id' => $id])
194
			->execute();
195
	}
196
197
	/**
198
	 * Get available connectors.
199
	 *
200
	 * @return string[]
201
	 */
202
	public static function getConnectors(): array
203
	{
204
		$connectors = [];
205
		$iterator = new \DirectoryIterator(__DIR__ . '/Connector');
206
		foreach ($iterator as $item) {
207
			if ($item->isFile() && 'Base.php' !== $item->getFilename() && 'php' === $item->getExtension()
208
			) {
209
				$name = $item->getBasename('.php');
210
				$className = '\\App\\Integrations\\Comarch\\Connector\\' . $name;
211
				$connectors[$name] = $className::NAME;
212
			}
213
		}
214
		return $connectors;
215
	}
216
}
217