refactoring
This commit is contained in:
@@ -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}.");
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Tools\Import;
|
||||
|
||||
use stdClass;
|
||||
|
||||
class Result
|
||||
{
|
||||
private $id = null;
|
||||
|
||||
private $countCreated = 0;
|
||||
|
||||
private $countUpdated = 0;
|
||||
|
||||
private $manualMode = false;
|
||||
|
||||
public function getId(): ?string
|
||||
{
|
||||
return $this->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,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user