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

Comarch   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
eloc 62
dl 0
loc 143
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testConnection() 0 12 3
A __construct() 0 17 4
A getInfo() 0 9 2
A getConnector() 0 3 1
A getSync() 0 7 2
B log() 0 24 7
1
<?php
2
/**
3
 * Main file to 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;
16
17
use App\Exceptions\AppException;
18
19
/**
20
 * Main class to integration with Comarch.
21
 */
22
class Comarch
23
{
24
	/** @var string Servers table name */
25
	public const TABLE_NAME = 'i_#__comarch_servers';
26
	/** @var string Basic table name */
27
	public const LOG_TABLE_NAME = 'l_#__comarch';
28
	/** @var string Config table name */
29
	public const CONFIG_TABLE_NAME = 'i_#__comarch_config';
30
	/** @var string Map class table name */
31
	public const MAP_TABLE_NAME = 'i_#__comarch_map_class';
32
	/** @var string Config table name */
33
	public const QUEUE_TABLE_NAME = 'i_#__comarch_queue';
34
35
	/** @var callable|null Bath iteration callback */
36
	public $bathCallback;
37
	/** @var \App\Integrations\Comarch\Config Config. */
38
	public $config;
39
	/** @var \App\Integrations\Comarch\Connector\Base Connector with Comarch. */
40
	public $connector;
41
	/** @var \App\Integrations\Comarch\Synchronizer\Base[] Synchronizers instance */
42
	public $synchronizer = [];
43
44
	/**
45
	 * Constructor. Connect with Comarch and authorize.
46
	 *
47
	 * @param int           $serverId
48
	 * @param callable|null $bathCallback
49
	 */
50
	public function __construct(int $serverId, ?callable $bathCallback = null)
51
	{
52
		$this->bathCallback = $bathCallback;
53
		$this->config = Comarch\Config::getInstance($serverId);
54
		$className = '\\App\\Integrations\\Comarch\\Connector\\' . $this->config->get('connector');
55
		if (!class_exists($className)) {
56
			throw new AppException('ERR_CLASS_NOT_FOUND');
57
		}
58
		$this->connector = new $className($this->config);
59
		if (!$this->connector instanceof Comarch\Connector\Base) {
60
			throw new AppException('ERR_CLASS_MUST_BE||\App\Integrations\Comarch\Connector\Base');
61
		}
62
		try {
63
			$this->connector->authorize();
64
		} catch (\Throwable $ex) {
65
			$this->log('Error during authorize', null, $ex);
66
			\App\Log::error("Error during authorize: \n{$ex->__toString()}", 'Comarch');
67
		}
68
	}
69
70
	/**
71
	 * Get connector.
72
	 *
73
	 * @return \App\Integrations\Comarch\Connector\Base
74
	 */
75
	public function getConnector(): Comarch\Connector\Base
76
	{
77
		return $this->connector;
78
	}
79
80
	/**
81
	 * Get synchronizer object instance.
82
	 *
83
	 * @param string $name
84
	 *
85
	 * @return \App\Integrations\Comarch\Synchronizer
86
	 */
87
	public function getSync(string $name): Comarch\Synchronizer
88
	{
89
		if (isset($this->synchronizer[$name])) {
90
			return $this->synchronizer[$name];
91
		}
92
		$className = 'App\\Integrations\\Comarch\\' . $this->config->get('connector') . "\\Synchronizer\\{$name}";
93
		return $this->synchronizer[$name] = new $className($this);
94
	}
95
96
	/**
97
	 * Get information about Comarch ERP.
98
	 *
99
	 * @return array
100
	 */
101
	public function getInfo(): array
102
	{
103
		try {
104
			return $this->getConnector()->getInfo();
105
		} catch (\Throwable $th) {
106
			$this->log('Get connection info', null, $th);
107
			return ['info' => $th->getMessage(), 'count' => []];
108
		}
109
		return $this->getConnector()->getInfo();
0 ignored issues
show
Unused Code introduced by
return $this->getConnector()->getInfo() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
110
	}
111
112
	/**
113
	 * Test connection.
114
	 *
115
	 * @return string
116
	 */
117
	public function testConnection(): string
118
	{
119
		$status = '';
120
		try {
121
			if (!$this->getConnector()->isAuthorized()) {
122
				throw new AppException('No authorization');
123
			}
124
		} catch (\Throwable $th) {
125
			$this->log('Test connection error', null, $th);
126
			$status = '[TestConnection]: ' . $th->getMessage();
127
		}
128
		return $status;
129
	}
130
131
	/**
132
	 * Add log to YetiForce system.
133
	 *
134
	 * @param string      $category
135
	 * @param array       $params
136
	 * @param ?\Throwable $ex
137
	 * @param bool        $error
138
	 *
139
	 * @return void
140
	 */
141
	public function log(string $category, ?array $params, ?\Throwable $ex = null, bool $error = false): void
142
	{
143
		if ($ex) {
144
			$params = $params ?? [];
145
			$message = $ex->getMessage();
146
			array_unshift($params, $category);
147
		} else {
148
			$message = $category;
149
		}
150
		$params = print_r($params, true);
151
		if ($ex && ($raw = \App\RequestHttp::getRawException($ex))) {
152
			$params .= PHP_EOL . $raw;
153
		}
154
		\App\DB::getInstance('log')->createCommand()
155
			->insert(self::LOG_TABLE_NAME, [
156
				'server_id' => $this->config->get('id'),
157
				'time' => date('Y-m-d H:i:s'),
158
				'error' => $ex ? 1 : ((int) $error),
159
				'message' => \App\TextUtils::textTruncate($message, 255),
160
				'params' => $params ? \App\TextUtils::textTruncate($params, 65535) : null,
161
				'trace' => $ex ? \App\TextUtils::textTruncate(
162
					rtrim(str_replace(ROOT_DIRECTORY . \DIRECTORY_SEPARATOR, '', $ex->__toString()), PHP_EOL), 65535
163
				) : null,
164
			])->execute();
165
	}
166
}
167