From 9d23f3bdd09298ef74a3b550c08e6e6281826dc2 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sun, 5 Sep 2021 18:11:53 +0300 Subject: [PATCH] refactoring --- .../Espo/Classes/ConsoleCommands/Import.php | 10 +- application/Espo/Controllers/Import.php | 4 +- application/Espo/Tools/Import/Import.php | 53 ++++----- application/Espo/Tools/Import/Result.php | 110 ++++++++++++++++++ application/Espo/Tools/Import/Service.php | 9 +- 5 files changed, 144 insertions(+), 42 deletions(-) create mode 100644 application/Espo/Tools/Import/Result.php diff --git a/application/Espo/Classes/ConsoleCommands/Import.php b/application/Espo/Classes/ConsoleCommands/Import.php index b87e6b944c..a2c5d7efb9 100644 --- a/application/Espo/Classes/ConsoleCommands/Import.php +++ b/application/Espo/Classes/ConsoleCommands/Import.php @@ -75,9 +75,9 @@ class Import implements Command try { $result = $this->service->importContentsWithParamsId($contents, $paramsId); - $resultId = $result->id; - $countCreated = $result->countCreated; - $countUpdated = $result->countUpdated; + $resultId = $result->getId(); + $countCreated = $result->getCountCreated(); + $countUpdated = $result->getCountUpdated(); } catch (Throwable $e) { $io->writeLine("Error occurred: ". $e->getMessage() . ""); @@ -119,8 +119,8 @@ class Import implements Command return; } - $countCreated = $result->countCreated; - $countUpdated = $result->countUpdated; + $countCreated = $result->getCountCreated(); + $countUpdated = $result->getCountUpdated(); $io->writeLine("Finished. Created: {$countCreated}. Updated: {$countUpdated}."); diff --git a/application/Espo/Controllers/Import.php b/application/Espo/Controllers/Import.php index c843f1e97c..6d4edb50c3 100644 --- a/application/Espo/Controllers/Import.php +++ b/application/Espo/Controllers/Import.php @@ -116,12 +116,14 @@ class Import extends Record $params = ImportParams::fromRaw($data); - return $this->getImportService()->import( + $result = $this->getImportService()->import( $entityType, $attributeList, $attachmentId, $params ); + + return $result->getValueMap(); } public function postActionUnmarkAsDuplicate(Request $request): bool diff --git a/application/Espo/Tools/Import/Import.php b/application/Espo/Tools/Import/Import.php index 92392ed9dd..acbcff4f9d 100644 --- a/application/Espo/Tools/Import/Import.php +++ b/application/Espo/Tools/Import/Import.php @@ -180,7 +180,7 @@ class Import return $this; } - private function validate() + private function validate(): void { if (!$this->entityType) { throw new Error("Entity type is not set."); @@ -193,14 +193,8 @@ class Import /** * Run import. - * - * @return stdClass [ - * id: (string), - * countCreated: (int), - * countUpdated: (int), - * ] */ - public function run(): stdClass + public function run(): Result { $this->validate(); @@ -284,12 +278,9 @@ class Import $this->entityManager->saveEntity($import); if (!$this->id && $params->isManualMode()) { - return (object) [ - 'id' => $import->getId(), - 'countCreated' => 0, - 'countUpdated' => 0, - 'manualMode' => true, - ]; + return Result::create() + ->withId($import->getId()) + ->withManualMode(); } if ($params->isIdleMode()) { @@ -306,11 +297,7 @@ class Import ]) ->schedule(); - return (object) [ - 'id' => $import->getId(), - 'countCreated' => 0, - 'countUpdated' => 0, - ]; + return Result::create()->withId($import->getId()); } try { @@ -325,6 +312,7 @@ class Import $contents = str_replace("\r\n", "\n", $contents); while ($row = $this->readCsvString($contents, $delimiter, $enclosure)) { + print_r($row); $i++; if ($i == 0 && $params->headerRow()) { @@ -383,11 +371,10 @@ class Import $this->entityManager->saveEntity($import); - return (object) [ - 'id' => $import->getId(), - 'countCreated' => count($result->importedIds), - 'countUpdated' => count($result->updatedIds), - ]; + return Result::create() + ->withId($import->getId()) + ->withCountCreated(count($result->importedIds)) + ->withCountUpdated(count($result->updatedIds)); } private function importRow(array $attributeList, array $row): ?stdClass @@ -558,7 +545,7 @@ class Import return (object) $result; } - private function processForeignName(Entity $entity, string $attribute) + private function processForeignName(Entity $entity, string $attribute): void { $relation = $entity->getAttributeParam($attribute, 'relation'); @@ -630,7 +617,7 @@ class Import } } - private function processRowItem(Entity $entity, string $attribute, $value, stdClass $valueMap) + private function processRowItem(Entity $entity, string $attribute, $value, stdClass $valueMap): void { $params = $this->params; @@ -803,6 +790,9 @@ class Import } } + /** + * @return mixed + */ private function parseValue(Entity $entity, string $attribute, $value) { $params = $this->params; @@ -892,22 +882,23 @@ class Import return $this->prepareAttributeValue($entity, $attribute, $value); } + /** + * @return mixed + */ private function prepareAttributeValue(Entity $entity, string $attribute, $value) { if ($entity->getAttributeType($attribute) === $entity::VARCHAR) { $maxLength = $entity->getAttributeParam($attribute, 'len'); - if ($maxLength) { - if (mb_strlen($value) > $maxLength) { - $value = substr($value, 0, $maxLength); - } + if ($maxLength && mb_strlen($value) > $maxLength) { + $value = substr($value, 0, $maxLength); } } return $value; } - protected function parsePersonName($value, string $format): array + private function parsePersonName($value, string $format): array { $firstName = null; $lastName = $value; diff --git a/application/Espo/Tools/Import/Result.php b/application/Espo/Tools/Import/Result.php new file mode 100644 index 0000000000..4b7d60855d --- /dev/null +++ b/application/Espo/Tools/Import/Result.php @@ -0,0 +1,110 @@ +id; + } + + public function getCountCreated(): int + { + return $this->countCreated; + } + + public function getCountUpdated(): int + { + return $this->countUpdated; + } + + public function isManualMode(): bool + { + return $this->manualMode; + } + + public static function create(): self + { + return new self(); + } + + public function withId(?string $id): self + { + $obj = clone $this; + $obj->id = $id; + + return $obj; + } + + public function withCountCreated(int $value): self + { + $obj = clone $this; + $obj->countCreated = $value; + + return $obj; + } + + public function withCountUpdated(int $value): self + { + $obj = clone $this; + $obj->countUpdated = $value; + + return $obj; + } + + public function withManualMode(bool $manualMode = true): self + { + $obj = clone $this; + $obj->manualMode = $manualMode; + + return $obj; + } + + public function getValueMap(): stdClass + { + return (object) [ + 'id' => $this->id, + 'countCreated' => $this->countCreated, + 'countUpdated' => $this->countUpdated, + 'manualMode' => $this->manualMode, + ]; + } +} diff --git a/application/Espo/Tools/Import/Service.php b/application/Espo/Tools/Import/Service.php index f1e716b233..0b449e0eff 100644 --- a/application/Espo/Tools/Import/Service.php +++ b/application/Espo/Tools/Import/Service.php @@ -42,7 +42,6 @@ use Espo\ORM\EntityManager; use Espo\Entities\Import as ImportEntity; use Espo\Entities\Attachment; -use stdClass; use DateTime; class Service @@ -74,7 +73,7 @@ class Service array $attributeList, string $attachmentId, Params $params - ): stdClass { + ): Result { if (!$this->acl->check($entityType, Table::ACTION_CREATE)) { throw new Forbidden("No create access for '{$entityType}'."); @@ -88,7 +87,7 @@ class Service ->setParams($params) ->run(); - $id = $result->id ?? null; + $id = $result->getId(); if ($id) { $import = $this->entityManager->getEntity(ImportEntity::ENTITY_TYPE, $id); @@ -103,7 +102,7 @@ class Service return $result; } - public function importContentsWithParamsId(string $contents, string $importParamsId): stdClass + public function importContentsWithParamsId(string $contents, string $importParamsId): Result { if (!$contents) { throw new Error("Contents is empty."); @@ -128,7 +127,7 @@ class Service return $this->import($entityType, $attributeList, $attachmentId, $params); } - public function importById(string $id, bool $startFromLastIndex = false, bool $forceResume = false): stdClass + public function importById(string $id, bool $startFromLastIndex = false, bool $forceResume = false): Result { /** @var ImportEntity $import */ $import = $this->entityManager->getEntity(ImportEntity::ENTITY_TYPE, $id);