Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add media import #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/data_converters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ data_converters:
- App\Processor\Converter\ReferenceEntitySingleLinkConverter
- App\Processor\Converter\ReferenceEntityMultipleLinksConverter
# Feel free to add your own converters here:
- App\Processor\Converter\MediaConverter
16 changes: 10 additions & 6 deletions src/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ protected function configure()
->addOption('apiPassword', null, InputOption::VALUE_OPTIONAL, 'The password of the user.', getenv('AKENEO_API_PASSWORD'))
->addOption('apiClientId', null, InputOption::VALUE_OPTIONAL, '', getenv('AKENEO_API_CLIENT_ID'))
->addOption('apiClientSecret', null, InputOption::VALUE_OPTIONAL, '', getenv('AKENEO_API_CLIENT_SECRET'))
->addOption('importMedia', 'i', InputOption::VALUE_NONE, 'Allow to import media or not. Cell content of the CSV must have an encoded json format with "filePath":"/path/to/folder/picture.png"')
;
}

Expand All @@ -104,6 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$referenceEntityCode = $input->getArgument('referenceEntityCode');
$filePath = $input->getArgument('filePath');
$importMedia = (bool)$input->getOption('importMedia');

try {
$this->reader = new CsvReader(
Expand Down Expand Up @@ -152,7 +154,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->io->newLine(2);

// Import records
$this->importRecords($output, $filePath, $referenceEntityCode, $validValueKeys, $attributes, $channels);
$this->importRecords($output, $filePath, $referenceEntityCode, $validValueKeys, $attributes, $channels, $importMedia);

$this->io->newLine(2);
$this->io->success(sprintf(
Expand Down Expand Up @@ -244,7 +246,8 @@ private function importRecords(
string $referenceEntityCode,
array $validValueKeys,
array $attributes,
array $channels
array $channels,
$importMedia = false
): void {
$progressBar = new ProgressBar($output, $this->reader->count());
$progressBar->start();
Expand Down Expand Up @@ -279,7 +282,7 @@ private function importRecords(
$linesToWrite[] = $line;

if (count($recordsToWrite) === self::BATCH_SIZE) {
$this->writeRecords($filePath, $referenceEntityCode, $recordWriter, $recordsToWrite, $linesToWrite);
$this->writeRecords($filePath, $referenceEntityCode, $recordWriter, $recordsToWrite, $linesToWrite, $importMedia);

$recordsToWrite = [];
$linesToWrite = [];
Expand All @@ -288,7 +291,7 @@ private function importRecords(
}

if (!empty($recordsToWrite)) {
$this->writeRecords($filePath, $referenceEntityCode, $recordWriter, $recordsToWrite, $linesToWrite);
$this->writeRecords($filePath, $referenceEntityCode, $recordWriter, $recordsToWrite, $linesToWrite, $importMedia);
$progressBar->advance(count($recordsToWrite));
}

Expand All @@ -300,9 +303,10 @@ private function writeRecords(
string $referenceEntityCode,
RecordWriter $recordWriter,
array $recordsToWrite,
array $linesToWrite
array $linesToWrite,
$importMedia = false
): void {
$responses = $recordWriter->write($referenceEntityCode, $recordsToWrite);
$responses = $recordWriter->write($referenceEntityCode, $recordsToWrite, $importMedia);

$this->logger->logResponses($responses);
$this->invalidFileGenerator->fromResponses($responses, $linesToWrite, $filePath, $this->reader->getHeaders());
Expand Down
47 changes: 47 additions & 0 deletions src/Processor/Converter/MediaConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Diglin GmbH - Switzerland
*
* @author Sylvain Rayé <support at diglin.com>
* @category Diglin
* @package csv_to_reference_entity
* @copyright Copyright (c) Diglin (https://www.diglin.com)
*/

declare(strict_types=1);

namespace App\Processor\Converter;

class MediaConverter implements DataConverterInterface
{
/**
* Does this data converter support the given $attribute
*
* @param array $attribute
*
* @return bool
*/
public function support(array $attribute): bool
{
return isset($attribute['type']) && 'image' === $attribute['type'];
}

/**
* Convert the given $data for the given $attribute to the correct format expected by the Akeneo API
*
* @param array $attribute
* @param string $data
*
* @return string|null
*/
public function convert(array $attribute, string $data): ?string
{
if (empty($data)) {
return null;
}

$jsonArray = json_decode($data, true);

return (isset($jsonArray['filePath']) ? $jsonArray['filePath'] : null);
}
}
33 changes: 32 additions & 1 deletion src/Writer/RecordWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,39 @@ public function __construct(AkeneoPimEnterpriseClientInterface $client)
$this->client = $client;
}

public function write(string $referenceEntityCode, array $records): array
/**
* @param string $referenceEntityCode
* @param array $records
* @param bool $importMedia
*
* @return array
*/
public function write(string $referenceEntityCode, array $records, $importMedia = false): array
{
if ($importMedia) {
// Create MediaFile if not already exist before to import to reference entity record
foreach ($records as $key => $record) {

foreach ($record['values'] as $i => $values) {
foreach ($values as $j => $value) {
if (!empty($value['data'])) {

$pathinfo = pathinfo($value['data'], PATHINFO_EXTENSION);

if (!empty($pathinfo) && @file_exists($value['data'])) {
$code = $this->client->getReferenceEntityMediaFileApi()->create($value['data']);
$records[$key]['values'][$i][$j] = [
'channel' => $value['channel'],
'locale' => $value['locale'],
'data' => $code,
];
}
}
}
}
}
}

return $this->client->getReferenceEntityRecordApi()->upsertList($referenceEntityCode, $records);
}
}