diff --git a/Transferor/Csv/GetHeadersFromMappings.php b/Transferor/Csv/GetHeadersFromMappings.php new file mode 100644 index 0000000..7d29089 --- /dev/null +++ b/Transferor/Csv/GetHeadersFromMappings.php @@ -0,0 +1,27 @@ +getHead(); + } + return $headers; + } +} diff --git a/Transferor/CsvTransferor.php b/Transferor/CsvTransferor.php new file mode 100644 index 0000000..d4348a3 --- /dev/null +++ b/Transferor/CsvTransferor.php @@ -0,0 +1,247 @@ +logger = $logger; + $this->fileName = $fileName; + $this->filePath = $filePath; + $this->mappings = $mappings; + foreach ($mappings as $mapping) { + if (!$mapping instanceof MappingTypeInterface) { + throw new CrtException(__("Invalid type for mappings")); + } + } + + $this->entityRepository = $entityRepository; + $this->csv = $csv; + $this->directoryList = $directoryList; + $this->getHeadersFromMappings = $getHeadersFromMappings; + $this->config = $config; + $this->addActivityIdToPath = $addActivityIdToPath; + $this->driver = $driver; + } + + /** + * @param int $activityId + * @param string $transferorType + * @throws CrtException + */ + public function execute(int $activityId, string $transferorType): void + { + try { + $file = $this->getFileNameWithPath($activityId); + $this->emptyFileAndWriteHeaders($file); + } catch (FileSystemException $e) { + throw new CrtException(__( + 'activityId:%1 ~ Transferor ~ transferorType:%2 ~ ERROR ~ error:%3', + $activityId, + $transferorType, + $e->getMessage() + )); + } + + $allActivityEntities = $this->entityRepository->getAllDataRefinedByActivityIdGroupedByIdentifier($activityId); + foreach ($allActivityEntities as $entityIdentifier => $entities) { + $this->logger->info(__( + 'activityId:%1 ~ Transferor ~ transferorType:%2 ~ entityIdentifier:%3 ~ START', + $activityId, + $transferorType, + $entityIdentifier + )); + + try { + $this->appendChildrenFirst($file, $entities); + } catch (FileSystemException $e) { + $this->logger->error(__( + 'activityId:%1 ~ Transferor ~ transferorType:%2 ~ entityIdentifier:%3 ~ ERROR ~ error:%4', + $activityId, + $transferorType, + $entityIdentifier, + $e->getMessage() + )); + + if (!$this->config->continueInCaseOfErrors()) { + throw new CrtException(__( + 'activityId:%1 ~ Transferor ~ transferorType:%2 ~ entityIdentifier:%3 ~ END ~'. + ' Because of continueInCaseOfErrors = false', + $activityId, + $transferorType, + $entityIdentifier + )); + } + } + + $this->logger->info(__( + 'activityId:%1 ~ Transferor ~ transferorType:%2 ~ entityIdentifier:%3 ~ END', + $activityId, + $transferorType, + $entityIdentifier + )); + } + } + + /** + * @param int $activityId + * @return string + * @throws FileSystemException + */ + private function getFileNameWithPath(int $activityId): string + { + $fileName = $this->fileName . '_' . (string)$activityId . '_01.csv'; + $directory = $this->directoryList->getPath(\Magento\Framework\App\Filesystem\DirectoryList::ROOT); + $path = $directory . DIRECTORY_SEPARATOR . $this->filePath . DIRECTORY_SEPARATOR; + if ($this->addActivityIdToPath) { + $path .= $activityId . DIRECTORY_SEPARATOR; + $this->driver->createDirectory($path, 0755); + } + $path .= $fileName; + return $path; + } + + /** + * @param string $file + * @throws FileSystemException + */ + private function emptyFileAndWriteHeaders(string $file) + { + $headers = [$this->getHeadersFromMappings->execute($this->mappings)]; + $this->csv->appendData($file, $headers, 'w+'); + } + + /** + * @param string $file + * @param array $entities + */ + /** @codingStandardsIgnoreStart */ + protected function appendChildrenFirst(string $file, array $entities) + { + //redy to extend + } + /** @codingStandardsIgnoreEnd */ + + /** + * @param array $entities + * @return array[] + * @throw CrtException + */ + protected function getData(array $entities): array + { + $data = []; + foreach ($this->mappings as $mapping) { + $data[] = $mapping->execute($entities); + } + return [$data]; + } + + /** + * @param string $file + * @param array $data + * @throws FileSystemException + */ + protected function appendToFile(string $file, array $data) + { + $this->csv->appendData($file, $data, 'a+'); + } +} diff --git a/Transferor/Mappings/Blank.php b/Transferor/Mappings/Blank.php new file mode 100644 index 0000000..445eb2a --- /dev/null +++ b/Transferor/Mappings/Blank.php @@ -0,0 +1,43 @@ +head = $head; + } + + /** + * @param array $data + * @return string + */ + public function execute(array $data): string + { + return ''; + } + + /** + * @return string + */ + public function getHead(): string + { + return $this->head; + } +} diff --git a/Transferor/Mappings/Fixed.php b/Transferor/Mappings/Fixed.php new file mode 100644 index 0000000..085a619 --- /dev/null +++ b/Transferor/Mappings/Fixed.php @@ -0,0 +1,51 @@ +head = $head; + $this->value = $value; + } + + /** + * @param array $data + * @return string + */ + public function execute(array $data): string + { + return $this->value; + } + + /** + * @return string + */ + public function getHead(): string + { + return $this->head; + } +} diff --git a/Transferor/Mappings/Json/DirectPath.php b/Transferor/Mappings/Json/DirectPath.php new file mode 100644 index 0000000..faf4dfa --- /dev/null +++ b/Transferor/Mappings/Json/DirectPath.php @@ -0,0 +1,64 @@ +head = $head; + $this->path = $path; + $this->dotConvention = $dotConvention; + } + + /** + * @param array $data + * @return string + * @throws CrtException + */ + public function execute(array $data): string + { + return (string)$this->dotConvention->getValue($data, $this->path); + } + + /** + * @return string + */ + public function getHead(): string + { + return $this->head; + } +} diff --git a/Transferor/Mappings/Json/ImplodePath.php b/Transferor/Mappings/Json/ImplodePath.php new file mode 100644 index 0000000..3aee3bb --- /dev/null +++ b/Transferor/Mappings/Json/ImplodePath.php @@ -0,0 +1,95 @@ +dotConvention = $dotConvention; + $this->head = $head; + $this->path = $path; + $this->separator = $separator; + } + + /** + * @param array $data + * @return string + * @throws CrtException + */ + public function execute(array $data): string + { + $identifiers = $this->dotConvention->getAll($this->path); + + $results = []; + $result = ''; + $value = $data; + end($identifiers); + $lastKey = key($identifiers); + foreach ($identifiers as $key => $identifier) { + if (!array_key_exists($identifier, $value)) { + throw new CrtException(__('Non existing field %1', $this->path)); + } + $value = $value[$identifier]; + + if ($lastKey === $key) { + $results = $value; + } + } + + foreach ($results as $key => $value) { + $result .= $this->separator . $key . '=' . $value; + } + + $result = ltrim($result, $this->separator); + return $result; + } + + /** + * @return string + */ + public function getHead(): string + { + return $this->head; + } +} diff --git a/Transferor/Mappings/Json/Path.php b/Transferor/Mappings/Json/Path.php new file mode 100644 index 0000000..34b80cc --- /dev/null +++ b/Transferor/Mappings/Json/Path.php @@ -0,0 +1,64 @@ +head = $head; + $this->path = $path; + $this->dotConvention = $dotConvention; + } + + /** + * @param array $data + * @return string + * @throws CrtException + */ + public function execute(array $data): string + { + return (string)$this->dotConvention->getValue($data, $this->path); + } + + /** + * @return string + */ + public function getHead(): string + { + return $this->head; + } +} diff --git a/Transferor/Mappings/MappingTypeInterface.php b/Transferor/Mappings/MappingTypeInterface.php new file mode 100644 index 0000000..0aa6524 --- /dev/null +++ b/Transferor/Mappings/MappingTypeInterface.php @@ -0,0 +1,21 @@ +