-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from akeneo/PIM-8320
PIM-8320: Upload media files during import
- Loading branch information
Showing
21 changed files
with
230 additions
and
98 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,43 +4,34 @@ | |
|
||
namespace App\Processor\Converter; | ||
|
||
use App\YamlReader; | ||
|
||
/** | ||
* Prepare the data to be sent to the API, using registered dedicated converters | ||
* | ||
* @author Adrien Pétremann <[email protected]> | ||
* @copyright 2019 Akeneo SAS (https://www.akeneo.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
class DataConverter implements DataConverterInterface | ||
class DataConverter | ||
{ | ||
/** @var DataConverterInterface[] */ | ||
private $converters; | ||
|
||
/** @var YamlReader */ | ||
private $yamlReader; | ||
private $converters = []; | ||
|
||
public function __construct(YamlReader $yamlReader) | ||
public function __construct(iterable $dataConverters) | ||
{ | ||
$this->yamlReader = $yamlReader; | ||
$dataConvertersConfig = $this->yamlReader->parseFile('config/data_converters.yml'); | ||
$dataConverterClasses = $dataConvertersConfig['data_converters']; | ||
|
||
foreach ($dataConverterClasses as $dataConverterClass) { | ||
$this->converters[] = new $dataConverterClass(); | ||
} | ||
$array = []; | ||
array_push($array, ...$dataConverters); | ||
$this->converters = $array; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function convert(array $attribute, string $data) | ||
public function convert(array $attribute, string $data, array $context) | ||
{ | ||
/** @var DataConverterInterface $converter */ | ||
foreach ($this->converters as $converter) { | ||
if ($converter->support($attribute)) { | ||
return $converter->convert($attribute, $data); | ||
return $converter->convert($attribute, $data, $context); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Processor\Converter; | ||
|
||
use Akeneo\PimEnterprise\ApiClient\AkeneoPimEnterpriseClientInterface; | ||
use Symfony\Component\Filesystem\Filesystem; | ||
|
||
/** | ||
* @author Samir Boulil <[email protected]> | ||
* @copyright 2019 Akeneo SAS (http://www.akeneo.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
class MediaAttributeConverter implements DataConverterInterface | ||
{ | ||
private const IMAGE_ATTRIBUTE_TYPE = 'image'; | ||
|
||
/** @var AkeneoPimEnterpriseClientInterface */ | ||
private $pimClient; | ||
|
||
/** @var Filesystem */ | ||
private $filesystem; | ||
|
||
public function __construct(AkeneoPimEnterpriseClientInterface $pimClient, Filesystem $filesystem) | ||
{ | ||
$this->filesystem = $filesystem; | ||
$this->pimClient = $pimClient; | ||
} | ||
|
||
public function support(array $attribute): bool | ||
{ | ||
return self::IMAGE_ATTRIBUTE_TYPE === $attribute['type']; | ||
} | ||
|
||
public function convert(array $attribute, string $data, array $context) | ||
{ | ||
$mediaFilePath = $this->mediaFilePath($data, $context); | ||
$this->checkMediaExists($mediaFilePath); | ||
$mediaIdentifier = $this->uploadMediaToPIM($mediaFilePath); | ||
|
||
return $mediaIdentifier; | ||
} | ||
|
||
private function mediaFilePath(string $relativeMediaPath, array $context): string | ||
{ | ||
$fileToImportPath = $context['filePath']; | ||
|
||
return sprintf('%s%s%s', dirname($fileToImportPath), DIRECTORY_SEPARATOR, $relativeMediaPath); | ||
} | ||
|
||
private function checkMediaExists(string $mediaFilePath): void | ||
{ | ||
if (!$this->filesystem->exists($mediaFilePath)) { | ||
throw new \RuntimeException(sprintf('media file at path "%s" was not found.', $mediaFilePath)); | ||
} | ||
} | ||
|
||
private function uploadMediaToPIM(string $mediaFilePath): string | ||
{ | ||
try { | ||
$mediaIdentifier = $this->pimClient->getReferenceEntityMediaFileApi()->create($mediaFilePath); | ||
|
||
return $mediaIdentifier; | ||
} catch (\Exception $exception) { | ||
$message = sprintf( | ||
'An error occured while uploading the media at path "%s" to the PIM: %s', | ||
$mediaFilePath, | ||
$exception->getMessage() | ||
); | ||
|
||
throw new \RuntimeException($message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.