classes/helper/AbstractImportModel.php
File `AbstractImportModel.php` has 269 lines of code (exceeds 250 allowed). Consider refactoring.<?php namespace Lovata\Toolbox\Classes\Helper; use Event;use Queue;use System\Models\File;use Lovata\Toolbox\Models\Settings;use Lovata\Toolbox\Classes\Queue\ImportItemQueue;use Lovata\Toolbox\Traits\Helpers\TraitInitActiveLang; /** * Class AbstractImportModel * @package Lovata\Toolbox\Classes\Helper * @author Andrey Kharanenka, a.khoronenko@lovata.com, LOVATA Group */The class AbstractImportModel has an overall complexity of 77 which is very high. The configured complexity threshold is 50.abstract class AbstractImportModel{ use TraitInitActiveLang; const EVENT_BEFORE_IMPORT = 'model.beforeImport'; const EVENT_AFTER_IMPORT = 'model.afterImport'; const MODEL_CLASS = ''; /** @var array */ protected $arImportData = []; /** @var \Model */ protected $obModel; /** @var null|string */ protected $sExternalID = null; protected $bWithTrashed = false; /** @var array */ protected $arImageList = []; protected $sPreviewImage; protected $bNeedUpdateImageList = false; protected $bNeedUpdatePreviewImage = false; /** @var bool */ protected $bDeactivate = true; /** @var array */ protected $arExistIDList = []; /** @var array */ protected $arProcessedIDList = []; /** * ImportBrandModelFromCSV constructor. */ public function __construct() { $this->initActiveLang(); } /** * Deactivate active elements */ public function deactivateElements() { if (!$this->bDeactivate) { return; } $arDeactivateIDList = array_diff((array) $this->arExistIDList, (array) $this->arProcessedIDList); if (empty($arDeactivateIDList)) { return; } //Get element list $sModelClass = static::MODEL_CLASS; $obElementList = $sModelClass::whereIn('external_id', $arDeactivateIDList)->get(); foreach ($obElementList as $obElement) { $obElement->active = false; $obElement->save(); } } /** * Create new item */ abstract protected function createItem(); /** * Update item */ abstract protected function updateItem(); /** * Run import item */ protected function run() { $this->prepareImportData(); $this->fireBeforeImportEvent(); $this->prepareImportDataBeforeSave(); $this->findByExternalID(); if (!empty($this->obModel)) { $this->updateItem(); } else { $this->createItem(); } if (empty($this->obModel)) { return; } $this->processModelObject(); Event::fire(self::EVENT_AFTER_IMPORT, [$this->obModel, $this->arImportData]); } /** * Find item by external ID */ protected function findByExternalID() { $sModelClass = static::MODEL_CLASS; if ($this->bWithTrashed) { $this->obModel = $sModelClass::withTrashed()->getByExternalID($this->sExternalID)->first(); } else { $this->obModel = $sModelClass::getByExternalID($this->sExternalID)->first(); } } /** * Prepare array of import data */ protected function prepareImportData() { } /** * Prepare array of import data */ protected function prepareImportDataBeforeSave() { if (empty($this->arImportData)) { return; } $arResult = []; foreach ($this->arImportData as $sKey => $sValue) { if (is_string($sValue)) { $sValue = trim($sValue); } elseif (is_array($sValue)) { $sValue = array_filter($sValue); } array_set($arResult, $sKey, $sValue); } $this->arImportData = $arResult; } /** * Process model object after creation/updating */Function `processModelObject` has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. protected function processModelObject() { $arActiveLangList = $this->getActiveLangList(); if (empty($arActiveLangList) || empty($this->obModel)) { return; } foreach ($arActiveLangList as $sLangCode) { if (!array_key_exists($sLangCode, $this->arImportData)) { continue; } foreach ($this->arImportData[$sLangCode] as $sField => $sValue) { $this->obModel->setAttributeTranslated($sField, $sValue, $sLangCode); } } $this->obModel->save(); } /** * Fire beforeImport event and update import data array */Function `fireBeforeImportEvent` has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. protected function fireBeforeImportEvent() { $arEventData = [static::MODEL_CLASS, $this->arImportData]; $arEventData = Event::fire(self::EVENT_BEFORE_IMPORT, $arEventData); if (empty($arEventData)) { return; } foreach ($arEventData as $arModelData) { if (empty($arModelData)) { continue; } foreach ($arModelData as $sKey => $sValue) { $this->arImportData[$sKey] = $sValue; } } } /** * Import obProductModel images */Function `importImageList` has a Cognitive Complexity of 33 (exceeds 5 allowed). Consider refactoring.
Method `importImageList` has 42 lines of code (exceeds 25 allowed). Consider refactoring.
The method importImageList() has an NPath complexity of 320. The configured NPath complexity threshold is 200.
The method importImageList() has a Cyclomatic Complexity of 16. The configured cyclomatic complexity threshold is 10.
Refactor this function to reduce its Cognitive Complexity from 33 to the 15 allowed. protected function importImageList() { if (!$this->bNeedUpdateImageList) { return; } if (empty($this->arImageList)) { $this->removeAllImages(); return; } //Update old images $obImageList = $this->obModel->images; if (!$obImageList->isEmpty()) { /** @var File $obImage */ foreach ($obImageList as $obImage) { $sFilePath = array_shift($this->arImageList); $sFileHash = null; try { $sFileHash = md5_file($sFilePath);Avoid using empty try-catch blocks in importImageList.
Either remove or fill this block of code.
Closing brace must be on a line by itself
Newline required after opening brace } catch (\Exception $obException) {} //Check image if (!empty($sFilePath) && (!file_exists($obImage->getLocalPath()) || $sFileHash != md5_file($obImage->getLocalPath()))) { try { $obImage->deleteThumbs();Avoid using empty try-catch blocks in importImageList.
Either remove or fill this block of code.
Closing brace must be on a line by itself
Newline required after opening brace } catch (\Exception $obException) {} $obNewImage = $this->createFileEntity($sFilePath, $obImage); if (empty($obNewImage)) { $obImage->delete(); } else { $obImage = $obNewImage; $obImage->save(); } } elseif (empty($sFilePath)) { try { $obImage->deleteThumbs(); $obImage->delete();Avoid using empty try-catch blocks in importImageList.
Either remove or fill this block of code.
Closing brace must be on a line by itself
Newline required after opening brace } catch (\Exception $obException) {} } } } //Create new images if (!empty($this->arImageList)) { foreach ($this->arImageList as $sFilePath) { $obImage = $this->createFileEntity($sFilePath); if (!empty($obImage)) { $this->obModel->images()->add($obImage); } } } } /** * Import preview image */Function `importPreviewImage` has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
Method `importPreviewImage` has 38 lines of code (exceeds 25 allowed). Consider refactoring.
The method importPreviewImage() has an NPath complexity of 480. The configured NPath complexity threshold is 200.
The method importPreviewImage() has a Cyclomatic Complexity of 17. The configured cyclomatic complexity threshold is 10.
Refactor this function to reduce its Cognitive Complexity from 20 to the 15 allowed. protected function importPreviewImage() { if (!$this->bNeedUpdatePreviewImage) { return; } $obPreviewImage = $this->obModel->preview_image; if (empty($obPreviewImage) && empty($this->sPreviewImage)) { return; } if (empty($obPreviewImage) && !empty($this->sPreviewImage)) { //Create new preview $obPreviewImage = $this->createFileEntity($this->sPreviewImage); if (!empty($obPreviewImage)) { $this->obModel->preview_image()->add($obPreviewImage); } return; } $sFileHash = null; try { $sFileHash = md5_file($this->sPreviewImage);Avoid using empty try-catch blocks in importPreviewImage.
Either remove or fill this block of code.
Closing brace must be on a line by itself
Newline required after opening brace } catch (\Exception $obException) {} if (!file_exists($obPreviewImage->getLocalPath())) { $obPreviewImage = $this->createFileEntity($this->sPreviewImage, $obPreviewImage); if (!empty($obPreviewImage)) { $obPreviewImage->save(); } } elseif (!empty($this->sPreviewImage) && file_exists($obPreviewImage->getLocalPath()) && $sFileHash != md5_file($obPreviewImage->getLocalPath())) { //Update preview image $obPreviewImage->deleteThumbs(); $obNewPreviewImage = $this->createFileEntity($this->sPreviewImage, $obPreviewImage); if (empty($obNewPreviewImage)) { $obPreviewImage->delete(); } else { $obPreviewImage = $obNewPreviewImage; $obPreviewImage->save(); } } elseif (!empty($obPreviewImage) && empty($this->sPreviewImage)) { try { $obPreviewImage->deleteThumbs(); $obPreviewImage->delete();Avoid using empty try-catch blocks in importPreviewImage.
Either remove or fill this block of code.
Closing brace must be on a line by itself
Newline required after opening brace } catch (\Exception $obException) {} } } /** * Create new file object from URL or file * @param string|null $sPath * @param File|null $obFile * @return File|null * @throws \Exception */ protected function createFileEntity(?string $sPath, ?File $obFile = null) { if (empty($obFile)) { $obFile = new File(); } if (preg_match('/https?:\/\//', $sPath)) { try { $obFile->fromUrl($sPath); } catch (\Exception $obException) { return null; } } else { $obFile->fromFile($sPath); } return $obFile; } /** * Remove all images */ protected function removeAllImages() { //Delete old images $obImageList = $this->obModel->images; if ($obImageList->isEmpty()) { return; } /** @var \System\Models\File $obFile */ foreach ($obImageList as $obFile) { try { $obFile->deleteThumbs(); $obFile->delete();Avoid using empty try-catch blocks in removeAllImages.
Either remove or fill this block of code.
Closing brace must be on a line by itself
Newline required after opening brace } catch (\Exception $obException) {} } } /** * Set active filed value, if active value is not null */ protected function setActiveField() {Define a constant instead of duplicating this literal "active" 3 times. $bActive = array_get($this->arImportData, 'active'); if ($bActive === null) { $this->arImportData['active'] = true; } else { $this->arImportData['active'] = $this->processBooleanValue($bActive); } } /** * @param string $sValue * @return bool */ protected function processBooleanValue($sValue) : bool { if (is_string($sValue) && $sValue == 'false') { return false; } else { return (bool) $sValue; } } /** * Create queue job with import single item * @throws \Throwable */ protected function createJob() { $sQueueName = Settings::getValue('import_queue_name'); $arQueueData = [ 'class' => static::class, 'data' => $this->arImportData, ]; if (empty($sQueueName)) { Queue::push(ImportItemQueue::class, $arQueueData); } else { Queue::pushOn($sQueueName, ImportItemQueue::class, $arQueueData); } }}