| Total Complexity | 46 |
| Total Lines | 379 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Synchronizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Synchronizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class Synchronizer |
||
| 18 | { |
||
| 19 | /** @var string Provider name | File name. */ |
||
| 20 | const NAME = null; |
||
| 21 | |||
| 22 | /** @var string Module name. */ |
||
| 23 | const MODULE_NAME = null; |
||
| 24 | |||
| 25 | /** @var string Priority order. */ |
||
| 26 | const SEQUENCE = null; |
||
| 27 | |||
| 28 | /** @var string Class name. */ |
||
| 29 | public $className; |
||
| 30 | |||
| 31 | /** @var string[] Map of fields integrating with WAPRO ERP */ |
||
| 32 | protected $fieldMap = []; |
||
| 33 | |||
| 34 | /** @var \App\Integrations\Wapro Controller instance. */ |
||
| 35 | protected $controller; |
||
| 36 | |||
| 37 | /** @var \Vtiger_Record_Model Record model instance. */ |
||
| 38 | protected $recordModel; |
||
| 39 | |||
| 40 | /** @var array Record row. */ |
||
| 41 | protected $row; |
||
| 42 | |||
| 43 | /** @var int WAPRO ERP record ID. */ |
||
| 44 | protected $waproId; |
||
| 45 | |||
| 46 | /** @var bool The flag to skip record creation. */ |
||
| 47 | protected $skip; |
||
| 48 | |||
| 49 | /** @var bool Information on currency configuration. */ |
||
| 50 | protected static $currency; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Synchronizer constructor. |
||
| 54 | * |
||
| 55 | * @param \App\Integrations\Wapro $controller |
||
| 56 | */ |
||
| 57 | public function __construct(\App\Integrations\Wapro $controller) |
||
| 58 | { |
||
| 59 | $this->controller = $controller; |
||
| 60 | $this->className = substr(static::class, strrpos(static::class, '\\') + 1); |
||
| 61 | if (isset($controller->customConfig[$this->className])) { |
||
| 62 | $this->fieldMap = $controller->customConfig[$this->className]; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Main function to execute synchronizer. |
||
| 68 | * |
||
| 69 | * @return int |
||
| 70 | */ |
||
| 71 | abstract public function process(): int; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Import record. |
||
| 75 | * |
||
| 76 | * @return int |
||
| 77 | */ |
||
| 78 | abstract public function importRecord(): int; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get number of records. |
||
| 82 | * |
||
| 83 | * @return int |
||
| 84 | */ |
||
| 85 | abstract public function getCounter(): int; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Function to get provider name. |
||
| 89 | * |
||
| 90 | * @return string provider name |
||
| 91 | */ |
||
| 92 | public function getName(): string |
||
| 93 | { |
||
| 94 | return $this::NAME; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Find the crm ID for the integration record. |
||
| 99 | * |
||
| 100 | * @param int $id |
||
| 101 | * @param string $table |
||
| 102 | * |
||
| 103 | * @return int|null |
||
| 104 | */ |
||
| 105 | public function findInMapTable(int $id, string $table): ?int |
||
| 106 | { |
||
| 107 | $cacheKey = "$id|$table"; |
||
| 108 | if (\App\Cache::has('WaproMapTable', $cacheKey)) { |
||
| 109 | return \App\Cache::get('WaproMapTable', $cacheKey); |
||
| 110 | } |
||
| 111 | $crmId = (new \App\Db\Query())->select(['crmid'])->from(\App\Integrations\Wapro::RECORDS_MAP_TABLE_NAME)->where(['wtable' => $table, 'wid' => $id])->scalar(); |
||
| 112 | \App\Cache::save('WaproMapTable', $cacheKey, $crmId); |
||
| 113 | return $crmId ?: null; |
||
|
|
|||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Add error log to db. |
||
| 118 | * |
||
| 119 | * @param \Throwable $ex |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | public function logError(\Throwable $ex): void |
||
| 124 | { |
||
| 125 | \App\Log::error("Error during import record in {$this->className}:\n{$ex->__toString()}", 'Integrations/Wapro'); |
||
| 126 | \App\DB::getInstance('log')->createCommand() |
||
| 127 | ->insert(\App\Integrations\Wapro::LOG_TABLE_NAME, [ |
||
| 128 | 'time' => date('Y-m-d H:i:s'), |
||
| 129 | 'category' => $this->className, |
||
| 130 | 'message' => \App\TextUtils::textTruncate($ex->getMessage(), 255), |
||
| 131 | 'error' => true, |
||
| 132 | 'trace' => \App\TextUtils::textTruncate("WAPRO ID: {$this->waproId} \n{$ex->__toString()}", 65535) |
||
| 133 | ])->execute(); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Add log to db. |
||
| 138 | * |
||
| 139 | * @param string $message |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public function log(string $message): void |
||
| 144 | { |
||
| 145 | \App\DB::getInstance('log')->createCommand() |
||
| 146 | ->insert(\App\Integrations\Wapro::LOG_TABLE_NAME, [ |
||
| 147 | 'time' => date('Y-m-d H:i:s'), |
||
| 148 | 'category' => $this->className, |
||
| 149 | 'message' => \App\TextUtils::textTruncate($message, 255), |
||
| 150 | 'error' => false, |
||
| 151 | ])->execute(); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Load data from DB based on field map. |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | protected function loadFromFieldMap(): void |
||
| 160 | { |
||
| 161 | foreach ($this->fieldMap as $wapro => $crm) { |
||
| 162 | if (\array_key_exists($wapro, $this->row)) { |
||
| 163 | if (null === $this->row[$wapro]) { |
||
| 164 | continue; |
||
| 165 | } |
||
| 166 | if (\is_array($crm)) { |
||
| 167 | $value = $this->{$crm['fn']}($this->row[$wapro], $crm); // it cannot be on the lower line because it is a reference |
||
| 168 | if ($this->skip) { |
||
| 169 | break; |
||
| 170 | } |
||
| 171 | $this->recordModel->set($crm['fieldName'], trim($value)); |
||
| 172 | } else { |
||
| 173 | $this->recordModel->set($crm, trim($this->row[$wapro])); |
||
| 174 | } |
||
| 175 | } else { |
||
| 176 | \App\Log::error("No column {$wapro} in the {$this->className}", 'Integrations/Wapro'); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Convert phone to system format. |
||
| 183 | * |
||
| 184 | * @param string $value |
||
| 185 | * @param array $params |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | protected function convertPhone(string $value, array &$params): string |
||
| 190 | { |
||
| 191 | if ($fieldModel = $this->recordModel->getField($params['fieldName'])) { |
||
| 192 | $details = $fieldModel->getUITypeModel()->getPhoneDetails($value, 'PL'); |
||
| 193 | $value = $details['number']; |
||
| 194 | if ($params['fieldName'] !== $details['fieldName']) { |
||
| 195 | $params['fieldName'] = $details['fieldName']; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | return $value; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Convert country to system format. |
||
| 203 | * |
||
| 204 | * @param string $value |
||
| 205 | * @param array $params |
||
| 206 | * |
||
| 207 | * @return string|null |
||
| 208 | */ |
||
| 209 | protected function convertCountry(string $value, array $params = []): ?string |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Convert currency to system format. |
||
| 217 | * |
||
| 218 | * @param string $value |
||
| 219 | * @param array $params |
||
| 220 | * |
||
| 221 | * @return int |
||
| 222 | */ |
||
| 223 | protected function convertCurrency(string $value, array $params): int |
||
| 224 | { |
||
| 225 | $currencyId = \App\Fields\Currency::getIdByCode($value); |
||
| 226 | if (empty($currencyId)) { |
||
| 227 | $currencyId = \App\Fields\Currency::addCurrency($value); |
||
| 228 | } |
||
| 229 | return $currencyId ?? 0; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Find relationship. |
||
| 234 | * |
||
| 235 | * @param string $value |
||
| 236 | * @param array $params |
||
| 237 | * |
||
| 238 | * @return int |
||
| 239 | */ |
||
| 240 | protected function findByRelationship(string $value, array $params): int |
||
| 241 | { |
||
| 242 | if ($id = $this->findInMapTable($value, $params['tableName'])) { |
||
| 243 | return $id; |
||
| 244 | } |
||
| 245 | if (!empty($params['skipMode'])) { |
||
| 246 | $this->skip = true; |
||
| 247 | } |
||
| 248 | return 0; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get information about base currency. |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected function getBaseCurrency(): array |
||
| 266 | ]; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Convert unit name to system format. |
||
| 271 | * |
||
| 272 | * @param string $value |
||
| 273 | * @param array $params |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | protected function convertUnitName(string $value, array $params): string |
||
| 278 | { |
||
| 279 | $value = trim($value, '.'); |
||
| 280 | $picklistValues = \App\Fields\Picklist::getValuesName($params['fieldName']); |
||
| 281 | $return = \in_array($value, $picklistValues); |
||
| 282 | if (!$return) { |
||
| 283 | foreach ($picklistValues as $picklistValue) { |
||
| 284 | if (\App\Language::translate($picklistValue, $params['moduleName']) === $value) { |
||
| 285 | $return = true; |
||
| 286 | $value = $picklistValue; |
||
| 287 | break; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | return $return ? $value : ''; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get global tax from value. |
||
| 296 | * |
||
| 297 | * @param string $value |
||
| 298 | * @param bool $addIfNotExist |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | protected function getGlobalTax(string $value, bool $addIfNotExist = false): string |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Convert unit name to system format. |
||
| 328 | * |
||
| 329 | * @param string $value |
||
| 330 | * @param array $params |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | protected function decode(string $value, array $params): string |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Search for user in activity. |
||
| 341 | * |
||
| 342 | * @param int $id |
||
| 343 | * @param string $table |
||
| 344 | * |
||
| 345 | * @return int|null |
||
| 346 | */ |
||
| 347 | public function searchUserInActivity(int $id, string $table): ?int |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Find CRM User ID by ID from ERP. |
||
| 365 | * |
||
| 366 | * @param int $erpId |
||
| 367 | * |
||
| 368 | * @return int|null |
||
| 369 | */ |
||
| 370 | protected function getUser(int $erpId): ?int |
||
| 396 | } |
||
| 397 | } |
||
| 398 |