From 16e906cffce5f8c6fbb78fc5aa57e24c4d510863 Mon Sep 17 00:00:00 2001 From: yolouiese Date: Fri, 24 Jul 2020 19:14:14 +0800 Subject: [PATCH 1/9] magento/adobe-stock-integration#1614: [Refactoring] PHP - refactored php files --- .../Model/{Entities.php => GetEntities.php} | 2 +- MediaContentSynchronizationApi/etc/di.xml | 2 +- .../Model/Listing/DataProvider.php | 14 ++++++++------ MediaGalleryRenditions/Model/Config.php | 2 +- .../{Model => Api}/ConfigInterface.php | 2 +- .../Api/SynchronizeFilesInterface.php | 2 +- .../Model/FetchBatchesInterface.php | 2 +- MediaGalleryUi/Plugin/ResizeSyncedFiles.php | 1 + 8 files changed, 15 insertions(+), 12 deletions(-) rename MediaContentSynchronizationApi/Model/{Entities.php => GetEntities.php} (92%) rename MediaGalleryRenditionsApi/{Model => Api}/ConfigInterface.php (91%) diff --git a/MediaContentSynchronizationApi/Model/Entities.php b/MediaContentSynchronizationApi/Model/GetEntities.php similarity index 92% rename from MediaContentSynchronizationApi/Model/Entities.php rename to MediaContentSynchronizationApi/Model/GetEntities.php index cfc86e844a93..38129b2b1c6b 100644 --- a/MediaContentSynchronizationApi/Model/Entities.php +++ b/MediaContentSynchronizationApi/Model/GetEntities.php @@ -10,7 +10,7 @@ /** * Configuration of entities used for media content. */ -class Entities implements GetEntitiesInterface +class GetEntities implements GetEntitiesInterface { /** * @var array diff --git a/MediaContentSynchronizationApi/etc/di.xml b/MediaContentSynchronizationApi/etc/di.xml index 46b88abfde2a..76bdd9b1cb16 100644 --- a/MediaContentSynchronizationApi/etc/di.xml +++ b/MediaContentSynchronizationApi/etc/di.xml @@ -6,5 +6,5 @@ */ --> - + diff --git a/MediaGalleryCatalogUi/Model/Listing/DataProvider.php b/MediaGalleryCatalogUi/Model/Listing/DataProvider.php index 0e2ed48b57f7..0daf63152c6c 100644 --- a/MediaGalleryCatalogUi/Model/Listing/DataProvider.php +++ b/MediaGalleryCatalogUi/Model/Listing/DataProvider.php @@ -28,6 +28,8 @@ */ class DataProvider extends UiComponentDataProvider { + private const ENTITY_ID = 'entity_id'; + /** * @var SearchResultFactory */ @@ -119,7 +121,7 @@ public function getData() ]; } } - + /** * @inheritDoc */ @@ -129,7 +131,7 @@ public function getSearchResult(): SearchResultInterface $searchCriteria = $this->skipRootCategory($searchCriteria); $collection = $this->categoryList->getList($searchCriteria); $items = []; - + foreach ($collection->getItems() as $category) { $items[] = $this->createDocument( [ @@ -144,12 +146,12 @@ public function getSearchResult(): SearchResultInterface ] ); } - + $searchResult = $this->searchResultFactory->create(); $searchResult->setSearchCriteria($searchCriteria); $searchResult->setItems($items); $searchResult->setTotalCount($collection->getTotalCount()); - + return $searchResult; } @@ -164,7 +166,7 @@ private function skipRootCategory(SearchCriteriaInterface $searchCriteria): Sear $filterGroups = $searchCriteria->getFilterGroups(); $filters[] = $this->filterBuilder - ->setField('entity_id') + ->setField(self::ENTITY_ID) ->setConditionType('neq') ->setValue(1) ->create(); @@ -172,7 +174,7 @@ private function skipRootCategory(SearchCriteriaInterface $searchCriteria): Sear $searchCriteria->setFilterGroups($filterGroups); return $searchCriteria; } - + /** * Add attributes to grid result * diff --git a/MediaGalleryRenditions/Model/Config.php b/MediaGalleryRenditions/Model/Config.php index 6cec41ab0289..bde71450dbcd 100644 --- a/MediaGalleryRenditions/Model/Config.php +++ b/MediaGalleryRenditions/Model/Config.php @@ -8,7 +8,7 @@ namespace Magento\MediaGalleryRenditions\Model; -use Magento\MediaGalleryRenditionsApi\Model\ConfigInterface; +use Magento\MediaGalleryRenditionsApi\Api\ConfigInterface; use Magento\Framework\App\Config\ScopeConfigInterface; /** diff --git a/MediaGalleryRenditionsApi/Model/ConfigInterface.php b/MediaGalleryRenditionsApi/Api/ConfigInterface.php similarity index 91% rename from MediaGalleryRenditionsApi/Model/ConfigInterface.php rename to MediaGalleryRenditionsApi/Api/ConfigInterface.php index 373233a21fc2..758cc66c2864 100644 --- a/MediaGalleryRenditionsApi/Model/ConfigInterface.php +++ b/MediaGalleryRenditionsApi/Api/ConfigInterface.php @@ -6,7 +6,7 @@ declare(strict_types=1); -namespace Magento\MediaGalleryRenditionsApi\Model; +namespace Magento\MediaGalleryRenditionsApi\Api; /** * Class responsible for providing API access to Media Gallery Renditions system configuration diff --git a/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php b/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php index 33b8cd62a8c8..5baf68354bdf 100644 --- a/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php +++ b/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php @@ -15,7 +15,7 @@ interface SynchronizeFilesInterface /** * Create media gallery assets based on files information and save them to database * - * @param \SplFileInfo[] $items + * @param array $items * @throws \Magento\Framework\Exception\LocalizedException */ public function execute(array $items): void; diff --git a/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php b/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php index c5ce10caa2c4..812883bf8d25 100644 --- a/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php +++ b/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php @@ -19,7 +19,7 @@ interface FetchBatchesInterface * * @param string $tableName * @param array $columns - * @param string|null $modificationDateColumn + * @param string $modificationDateColumn * @return \Traversable */ public function execute(string $tableName, array $columns, ?string $modificationDateColumn): \Traversable; diff --git a/MediaGalleryUi/Plugin/ResizeSyncedFiles.php b/MediaGalleryUi/Plugin/ResizeSyncedFiles.php index 93e426eb33ce..88ad2e057cf2 100644 --- a/MediaGalleryUi/Plugin/ResizeSyncedFiles.php +++ b/MediaGalleryUi/Plugin/ResizeSyncedFiles.php @@ -32,6 +32,7 @@ public function __construct(Storage $storage) * @param \Closure $closure * @param array $files * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @return mixed */ public function aroundExecute(SynchronizeFiles $subject, \Closure $closure, array $files) { From 5b3456fc9085cb2030985d1fb87ddd7e89524bf0 Mon Sep 17 00:00:00 2001 From: yolouiese Date: Tue, 28 Jul 2020 18:02:54 +0800 Subject: [PATCH 2/9] magento/adobe-stock-integration#1614: [Refactoring] PHP - refactored php files --- .../ResourceModel/GetOutdatedRelations.php | 3 +- .../Model/Synchronize.php | 4 --- .../Api/SynchronizeFilesInterface.php | 2 +- .../Model/FetchBatchesInterface.php | 4 ++- .../Model/SynchronizerPool.php | 10 +++++++ MediaGalleryUi/Plugin/ResizeSyncedFiles.php | 2 +- .../Listing/Filters/Options/UsedIn.php | 28 +++++++++++++++++++ MediaGalleryUi/etc/adminhtml/di.xml | 26 ----------------- 8 files changed, 45 insertions(+), 34 deletions(-) diff --git a/MediaContentSynchronization/Model/ResourceModel/GetOutdatedRelations.php b/MediaContentSynchronization/Model/ResourceModel/GetOutdatedRelations.php index 6f9163a38c7e..95170bbc7f98 100644 --- a/MediaContentSynchronization/Model/ResourceModel/GetOutdatedRelations.php +++ b/MediaContentSynchronization/Model/ResourceModel/GetOutdatedRelations.php @@ -10,6 +10,7 @@ use Magento\Framework\App\ResourceConnection; use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\Exception\CouldNotDeleteException; +use Magento\Framework\Exception\LocalizedException; use Psr\Log\LoggerInterface; use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterface; use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterfaceFactory; @@ -95,7 +96,7 @@ public function execute(string $entityType): array $assets = $connection->fetchAll($select); } catch (\Exception $exception) { $this->logger->critical($exception); - throw new CouldNotDeleteException(__('Could not fetch media content links data'), $exception); + throw new LocalizedException(__('Could not fetch media content links data'), $exception); } foreach ($assets as $asset) { diff --git a/MediaGallerySynchronization/Model/Synchronize.php b/MediaGallerySynchronization/Model/Synchronize.php index cbb6aa7aa056..ba392506695e 100644 --- a/MediaGallerySynchronization/Model/Synchronize.php +++ b/MediaGallerySynchronization/Model/Synchronize.php @@ -64,10 +64,6 @@ public function execute(): void $failed = []; foreach ($this->synchronizerPool->get() as $name => $synchronizer) { - if (!$synchronizer instanceof SynchronizeFilesInterface) { - throw new LocalizedException(__('Synchronizer must implement SynchronizeFilesInterface')); - } - foreach ($this->batchGenerator->execute() as $batch) { try { $synchronizer->execute($batch); diff --git a/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php b/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php index 5baf68354bdf..70903742a7e0 100644 --- a/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php +++ b/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php @@ -15,7 +15,7 @@ interface SynchronizeFilesInterface /** * Create media gallery assets based on files information and save them to database * - * @param array $items + * @param string[] $items * @throws \Magento\Framework\Exception\LocalizedException */ public function execute(array $items): void; diff --git a/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php b/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php index 812883bf8d25..89e624878aff 100644 --- a/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php +++ b/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php @@ -16,10 +16,12 @@ interface FetchBatchesInterface { /** * Fetch the columns from the database table in batches + * $modificationDateColumn contains the entities which were changed since last execution + * to avoid fetching items that have been previously synchronized * * @param string $tableName * @param array $columns - * @param string $modificationDateColumn + * @param string|null $modificationDateColumn * @return \Traversable */ public function execute(string $tableName, array $columns, ?string $modificationDateColumn): \Traversable; diff --git a/MediaGallerySynchronizationApi/Model/SynchronizerPool.php b/MediaGallerySynchronizationApi/Model/SynchronizerPool.php index 3b69f4e0a374..909f6b69a188 100644 --- a/MediaGallerySynchronizationApi/Model/SynchronizerPool.php +++ b/MediaGallerySynchronizationApi/Model/SynchronizerPool.php @@ -7,6 +7,8 @@ namespace Magento\MediaGallerySynchronizationApi\Model; +use Magento\Framework\Exception\LocalizedException; +use Magento\InstantPurchase\Model\ShippingMethodChoose\DeferredShippingMethodChooserInterface; use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface; /** @@ -27,6 +29,14 @@ class SynchronizerPool */ public function __construct(array $synchronizers = []) { + foreach ($synchronizers as $name => $synchronizer) { + if (!$synchronizer instanceof SynchronizeFilesInterface) { + throw new \InvalidArgumentException(sprintf( + 'Synchronizer must implement %s.', + SynchronizeFilesInterface::class + )); + } + } $this->synchronizers = $synchronizers; } diff --git a/MediaGalleryUi/Plugin/ResizeSyncedFiles.php b/MediaGalleryUi/Plugin/ResizeSyncedFiles.php index 32ae67e137eb..c5a65d465951 100644 --- a/MediaGalleryUi/Plugin/ResizeSyncedFiles.php +++ b/MediaGalleryUi/Plugin/ResizeSyncedFiles.php @@ -32,7 +32,7 @@ public function __construct(Storage $storage) * @param \Closure $closure * @param array $filesPaths * @SuppressWarnings(PHPMD.UnusedFormalParameter) - * @return mixed + * @return void */ public function aroundExecute(SynchronizeFilesInterface $subject, \Closure $closure, array $filesPaths) { diff --git a/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php b/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php index d3f758a51088..20501b389c43 100644 --- a/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php +++ b/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php @@ -32,6 +32,34 @@ public function __construct(array $options = []) */ public function toOptionArray(): array { + return $this->usedInOptions(); + } + + public function usedInOptions() + { + $this->options = [ + 'cms_page' => [ + 'value' => 'cms_page', + 'label' => 'Pages' + ], + 'catalog_category' => [ + 'value' => 'catalog_category', + 'label' => 'Categories' + ], + 'cms_block' => [ + 'value' => 'cms_block', + 'label' => 'Blocks' + ], + 'catalog_product' => [ + 'value' => 'catalog_product', + 'label' => 'Products' + ], + 'not_used' => [ + 'value' => 'not_used', + 'label' => 'Not used anywhere' + ] + ]; + return $this->options; } } diff --git a/MediaGalleryUi/etc/adminhtml/di.xml b/MediaGalleryUi/etc/adminhtml/di.xml index bf07512d13d4..552c5364f350 100644 --- a/MediaGalleryUi/etc/adminhtml/di.xml +++ b/MediaGalleryUi/etc/adminhtml/di.xml @@ -41,30 +41,4 @@ Magento\MediaGalleryUi\Model\Api\SearchCriteria\CollectionProcessor - - - - - cms_page - Pages - - - catalog_category - Categories - - - cms_block - Blocks - - - catalog_product - Products - - - not_used - Not used anywhere - - - - From ec43db3d12741d7a1ab55d9c0a24a04a7d5bba5c Mon Sep 17 00:00:00 2001 From: yolouiese Date: Tue, 28 Jul 2020 18:49:24 +0800 Subject: [PATCH 3/9] magento/adobe-stock-integration#1614: [Refactoring] PHP - refactored php files --- .../ResourceModel/GetOutdatedRelations.php | 3 +- .../Model/Synchronize.php | 5 ---- .../Api/SynchronizeFilesInterface.php | 2 +- .../Model/FetchBatchesInterface.php | 4 ++- .../Model/SynchronizerPool.php | 8 ++++++ MediaGalleryUi/Plugin/ResizeSyncedFiles.php | 2 +- .../Listing/Filters/Options/UsedIn.php | 28 +++++++++++++++++++ MediaGalleryUi/etc/adminhtml/di.xml | 26 ----------------- 8 files changed, 43 insertions(+), 35 deletions(-) diff --git a/MediaContentSynchronization/Model/ResourceModel/GetOutdatedRelations.php b/MediaContentSynchronization/Model/ResourceModel/GetOutdatedRelations.php index 6f9163a38c7e..95170bbc7f98 100644 --- a/MediaContentSynchronization/Model/ResourceModel/GetOutdatedRelations.php +++ b/MediaContentSynchronization/Model/ResourceModel/GetOutdatedRelations.php @@ -10,6 +10,7 @@ use Magento\Framework\App\ResourceConnection; use Magento\Framework\EntityManager\MetadataPool; use Magento\Framework\Exception\CouldNotDeleteException; +use Magento\Framework\Exception\LocalizedException; use Psr\Log\LoggerInterface; use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterface; use Magento\MediaContentApi\Api\Data\ContentAssetLinkInterfaceFactory; @@ -95,7 +96,7 @@ public function execute(string $entityType): array $assets = $connection->fetchAll($select); } catch (\Exception $exception) { $this->logger->critical($exception); - throw new CouldNotDeleteException(__('Could not fetch media content links data'), $exception); + throw new LocalizedException(__('Could not fetch media content links data'), $exception); } foreach ($assets as $asset) { diff --git a/MediaGallerySynchronization/Model/Synchronize.php b/MediaGallerySynchronization/Model/Synchronize.php index cbb6aa7aa056..57276b87a4f9 100644 --- a/MediaGallerySynchronization/Model/Synchronize.php +++ b/MediaGallerySynchronization/Model/Synchronize.php @@ -8,7 +8,6 @@ namespace Magento\MediaGallerySynchronization\Model; use Magento\Framework\Exception\LocalizedException; -use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface; use Magento\MediaGallerySynchronizationApi\Api\SynchronizeInterface; use Magento\MediaGallerySynchronizationApi\Model\SynchronizerPool; use Psr\Log\LoggerInterface; @@ -64,10 +63,6 @@ public function execute(): void $failed = []; foreach ($this->synchronizerPool->get() as $name => $synchronizer) { - if (!$synchronizer instanceof SynchronizeFilesInterface) { - throw new LocalizedException(__('Synchronizer must implement SynchronizeFilesInterface')); - } - foreach ($this->batchGenerator->execute() as $batch) { try { $synchronizer->execute($batch); diff --git a/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php b/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php index 5baf68354bdf..70903742a7e0 100644 --- a/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php +++ b/MediaGallerySynchronizationApi/Api/SynchronizeFilesInterface.php @@ -15,7 +15,7 @@ interface SynchronizeFilesInterface /** * Create media gallery assets based on files information and save them to database * - * @param array $items + * @param string[] $items * @throws \Magento\Framework\Exception\LocalizedException */ public function execute(array $items): void; diff --git a/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php b/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php index 812883bf8d25..89e624878aff 100644 --- a/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php +++ b/MediaGallerySynchronizationApi/Model/FetchBatchesInterface.php @@ -16,10 +16,12 @@ interface FetchBatchesInterface { /** * Fetch the columns from the database table in batches + * $modificationDateColumn contains the entities which were changed since last execution + * to avoid fetching items that have been previously synchronized * * @param string $tableName * @param array $columns - * @param string $modificationDateColumn + * @param string|null $modificationDateColumn * @return \Traversable */ public function execute(string $tableName, array $columns, ?string $modificationDateColumn): \Traversable; diff --git a/MediaGallerySynchronizationApi/Model/SynchronizerPool.php b/MediaGallerySynchronizationApi/Model/SynchronizerPool.php index 3b69f4e0a374..3b3d0885e613 100644 --- a/MediaGallerySynchronizationApi/Model/SynchronizerPool.php +++ b/MediaGallerySynchronizationApi/Model/SynchronizerPool.php @@ -27,6 +27,14 @@ class SynchronizerPool */ public function __construct(array $synchronizers = []) { + foreach ($synchronizers as $name => $synchronizer) { + if (!$synchronizer instanceof SynchronizeFilesInterface) { + throw new \InvalidArgumentException(sprintf( + 'Synchronizer must implement %s.', + SynchronizeFilesInterface::class + )); + } + } $this->synchronizers = $synchronizers; } diff --git a/MediaGalleryUi/Plugin/ResizeSyncedFiles.php b/MediaGalleryUi/Plugin/ResizeSyncedFiles.php index 32ae67e137eb..c5a65d465951 100644 --- a/MediaGalleryUi/Plugin/ResizeSyncedFiles.php +++ b/MediaGalleryUi/Plugin/ResizeSyncedFiles.php @@ -32,7 +32,7 @@ public function __construct(Storage $storage) * @param \Closure $closure * @param array $filesPaths * @SuppressWarnings(PHPMD.UnusedFormalParameter) - * @return mixed + * @return void */ public function aroundExecute(SynchronizeFilesInterface $subject, \Closure $closure, array $filesPaths) { diff --git a/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php b/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php index d3f758a51088..20501b389c43 100644 --- a/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php +++ b/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php @@ -32,6 +32,34 @@ public function __construct(array $options = []) */ public function toOptionArray(): array { + return $this->usedInOptions(); + } + + public function usedInOptions() + { + $this->options = [ + 'cms_page' => [ + 'value' => 'cms_page', + 'label' => 'Pages' + ], + 'catalog_category' => [ + 'value' => 'catalog_category', + 'label' => 'Categories' + ], + 'cms_block' => [ + 'value' => 'cms_block', + 'label' => 'Blocks' + ], + 'catalog_product' => [ + 'value' => 'catalog_product', + 'label' => 'Products' + ], + 'not_used' => [ + 'value' => 'not_used', + 'label' => 'Not used anywhere' + ] + ]; + return $this->options; } } diff --git a/MediaGalleryUi/etc/adminhtml/di.xml b/MediaGalleryUi/etc/adminhtml/di.xml index bf07512d13d4..552c5364f350 100644 --- a/MediaGalleryUi/etc/adminhtml/di.xml +++ b/MediaGalleryUi/etc/adminhtml/di.xml @@ -41,30 +41,4 @@ Magento\MediaGalleryUi\Model\Api\SearchCriteria\CollectionProcessor - - - - - cms_page - Pages - - - catalog_category - Categories - - - cms_block - Blocks - - - catalog_product - Products - - - not_used - Not used anywhere - - - - From 527c309701401bc1b0c69489b457c597c47b7c83 Mon Sep 17 00:00:00 2001 From: yolouiese Date: Tue, 28 Jul 2020 22:21:45 +0800 Subject: [PATCH 4/9] magento/adobe-stock-integration#1614: [Refactoring] PHP - refactored php files --- .../Console/Command/Synchronize.php | 16 ++--------- .../{Api => Model}/Config.php | 0 .../Model/SynchronizerPool.php | 2 -- .../Listing/Filters/Options/UsedIn.php | 28 ------------------- MediaGalleryUi/etc/adminhtml/di.xml | 26 +++++++++++++++++ 5 files changed, 28 insertions(+), 44 deletions(-) rename MediaGalleryRenditionsApi/{Api => Model}/Config.php (100%) diff --git a/MediaContentSynchronization/Console/Command/Synchronize.php b/MediaContentSynchronization/Console/Command/Synchronize.php index 9f2e2d3ee52c..e591b4f2339b 100644 --- a/MediaContentSynchronization/Console/Command/Synchronize.php +++ b/MediaContentSynchronization/Console/Command/Synchronize.php @@ -7,8 +7,6 @@ namespace Magento\MediaContentSynchronization\Console\Command; -use Magento\Framework\App\Area; -use Magento\Framework\App\State; use Magento\Framework\Console\Cli; use Magento\MediaContentSynchronizationApi\Api\SynchronizeInterface; use Symfony\Component\Console\Command\Command; @@ -25,21 +23,13 @@ class Synchronize extends Command */ private $synchronizeContent; - /** - * @var State $state - */ - private $state; - /** * @param SynchronizeInterface $synchronizeContent - * @param State $state */ public function __construct( - SynchronizeInterface $synchronizeContent, - State $state + SynchronizeInterface $synchronizeContent ) { $this->synchronizeContent = $synchronizeContent; - $this->state = $state; parent::__construct(); } @@ -58,9 +48,7 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Synchronizing content with assets...'); - $this->state->emulateAreaCode(Area::AREA_ADMINHTML, function () { - $this->synchronizeContent->execute(); - }); + $this->synchronizeContent->execute(); $output->writeln('Completed content synchronization.'); return Cli::RETURN_SUCCESS; } diff --git a/MediaGalleryRenditionsApi/Api/Config.php b/MediaGalleryRenditionsApi/Model/Config.php similarity index 100% rename from MediaGalleryRenditionsApi/Api/Config.php rename to MediaGalleryRenditionsApi/Model/Config.php diff --git a/MediaGallerySynchronizationApi/Model/SynchronizerPool.php b/MediaGallerySynchronizationApi/Model/SynchronizerPool.php index 909f6b69a188..3b3d0885e613 100644 --- a/MediaGallerySynchronizationApi/Model/SynchronizerPool.php +++ b/MediaGallerySynchronizationApi/Model/SynchronizerPool.php @@ -7,8 +7,6 @@ namespace Magento\MediaGallerySynchronizationApi\Model; -use Magento\Framework\Exception\LocalizedException; -use Magento\InstantPurchase\Model\ShippingMethodChoose\DeferredShippingMethodChooserInterface; use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface; /** diff --git a/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php b/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php index 20501b389c43..d3f758a51088 100644 --- a/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php +++ b/MediaGalleryUi/Ui/Component/Listing/Filters/Options/UsedIn.php @@ -32,34 +32,6 @@ public function __construct(array $options = []) */ public function toOptionArray(): array { - return $this->usedInOptions(); - } - - public function usedInOptions() - { - $this->options = [ - 'cms_page' => [ - 'value' => 'cms_page', - 'label' => 'Pages' - ], - 'catalog_category' => [ - 'value' => 'catalog_category', - 'label' => 'Categories' - ], - 'cms_block' => [ - 'value' => 'cms_block', - 'label' => 'Blocks' - ], - 'catalog_product' => [ - 'value' => 'catalog_product', - 'label' => 'Products' - ], - 'not_used' => [ - 'value' => 'not_used', - 'label' => 'Not used anywhere' - ] - ]; - return $this->options; } } diff --git a/MediaGalleryUi/etc/adminhtml/di.xml b/MediaGalleryUi/etc/adminhtml/di.xml index 552c5364f350..bf07512d13d4 100644 --- a/MediaGalleryUi/etc/adminhtml/di.xml +++ b/MediaGalleryUi/etc/adminhtml/di.xml @@ -41,4 +41,30 @@ Magento\MediaGalleryUi\Model\Api\SearchCriteria\CollectionProcessor + + + + + cms_page + Pages + + + catalog_category + Categories + + + cms_block + Blocks + + + catalog_product + Products + + + not_used + Not used anywhere + + + + From 5cd8e6329bed90bb0a162c7ef9e4e22335eceeb0 Mon Sep 17 00:00:00 2001 From: yolouiese Date: Thu, 30 Jul 2020 03:38:19 +0800 Subject: [PATCH 5/9] magento/adobe-stock-integration#1614: [Refactoring] PHP - refactored php files --- MediaGalleryCatalogIntegration/LICENSE.txt | 48 +++++ .../LICENSE_AFL.txt | 48 +++++ .../SaveBaseCategoryImageInformation.php | 10 +- MediaGalleryCatalogIntegration/README.md | 3 + MediaGalleryCatalogIntegration/composer.json | 27 +++ .../etc/adminhtml/di.xml | 12 ++ MediaGalleryCatalogIntegration/etc/module.xml | 10 + MediaGalleryCatalogIntegration/modman | 1 + .../registration.php | 14 ++ MediaGalleryIntegration/etc/adminhtml/di.xml | 3 - .../Model/CreateAssetFromFile.php | 127 +----------- .../Model/UpdateAsset.php | 182 ++++++++++++++++++ _metapackage/composer.json | 3 +- modman | 1 + 14 files changed, 362 insertions(+), 127 deletions(-) create mode 100644 MediaGalleryCatalogIntegration/LICENSE.txt create mode 100644 MediaGalleryCatalogIntegration/LICENSE_AFL.txt rename {MediaGalleryIntegration => MediaGalleryCatalogIntegration}/Plugin/SaveBaseCategoryImageInformation.php (93%) create mode 100644 MediaGalleryCatalogIntegration/README.md create mode 100644 MediaGalleryCatalogIntegration/composer.json create mode 100644 MediaGalleryCatalogIntegration/etc/adminhtml/di.xml create mode 100644 MediaGalleryCatalogIntegration/etc/module.xml create mode 100644 MediaGalleryCatalogIntegration/modman create mode 100644 MediaGalleryCatalogIntegration/registration.php create mode 100644 MediaGallerySynchronization/Model/UpdateAsset.php diff --git a/MediaGalleryCatalogIntegration/LICENSE.txt b/MediaGalleryCatalogIntegration/LICENSE.txt new file mode 100644 index 000000000000..36b2459f6aa6 --- /dev/null +++ b/MediaGalleryCatalogIntegration/LICENSE.txt @@ -0,0 +1,48 @@ + +Open Software License ("OSL") v. 3.0 + +This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: + +Licensed under the Open Software License version 3.0 + + 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: + + 1. to reproduce the Original Work in copies, either alone or as part of a collective work; + + 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; + + 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License; + + 4. to perform the Original Work publicly; and + + 5. to display the Original Work publicly. + + 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. + + 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. + + 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. + + 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). + + 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. + + 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. + + 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. + + 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including 'fair use' or 'fair dealing'). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). + + 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. + + 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. + + 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. + + 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. + + 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + + 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. + + 16. Modification of This License. This License is Copyright (C) 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. diff --git a/MediaGalleryCatalogIntegration/LICENSE_AFL.txt b/MediaGalleryCatalogIntegration/LICENSE_AFL.txt new file mode 100644 index 000000000000..f39d641b18a1 --- /dev/null +++ b/MediaGalleryCatalogIntegration/LICENSE_AFL.txt @@ -0,0 +1,48 @@ + +Academic Free License ("AFL") v. 3.0 + +This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: + +Licensed under the Academic Free License version 3.0 + + 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: + + 1. to reproduce the Original Work in copies, either alone or as part of a collective work; + + 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; + + 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, under any license of your choice that does not contradict the terms and conditions, including Licensor's reserved rights and remedies, in this Academic Free License; + + 4. to perform the Original Work publicly; and + + 5. to display the Original Work publicly. + + 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. + + 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. + + 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. + + 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). + + 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. + + 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. + + 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. + + 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including "fair use" or "fair dealing"). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). + + 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. + + 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. + + 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. + + 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. + + 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + + 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. + + 16. Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Academic Free License" or "AFL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. diff --git a/MediaGalleryIntegration/Plugin/SaveBaseCategoryImageInformation.php b/MediaGalleryCatalogIntegration/Plugin/SaveBaseCategoryImageInformation.php similarity index 93% rename from MediaGalleryIntegration/Plugin/SaveBaseCategoryImageInformation.php rename to MediaGalleryCatalogIntegration/Plugin/SaveBaseCategoryImageInformation.php index f63738404ea5..aa84100aca5a 100644 --- a/MediaGalleryIntegration/Plugin/SaveBaseCategoryImageInformation.php +++ b/MediaGalleryCatalogIntegration/Plugin/SaveBaseCategoryImageInformation.php @@ -5,13 +5,13 @@ */ declare(strict_types=1); -namespace Magento\MediaGalleryIntegration\Plugin; +namespace Magento\MediaGalleryCatalogIntegration\Plugin; use Magento\Catalog\Model\ImageUploader; use Magento\Cms\Model\Wysiwyg\Images\Storage; +use Magento\Framework\Exception\LocalizedException; use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface; use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface; -use Magento\MediaGalleryApi\Api\SaveAssetsInterface; use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface; use Magento\MediaGalleryUiApi\Api\ConfigInterface; @@ -39,7 +39,7 @@ class SaveBaseCategoryImageInformation * @var Storage */ private $storage; - + /** * @var SynchronizeFilesInterface */ @@ -71,13 +71,15 @@ public function __construct( * * @param ImageUploader $subject * @param string $imagePath + * @return string + * @throws LocalizedException */ public function afterMoveFileFromTmp(ImageUploader $subject, string $imagePath): string { if (!$this->config->isEnabled()) { return $imagePath; } - + $absolutePath = $this->storage->getCmsWysiwygImages()->getStorageRoot() . $imagePath; $tmpPath = $subject->getBaseTmpPath() . '/' . substr(strrchr($imagePath, "/"), 1); $tmpAssets = $this->getAssetsByPaths->execute([$tmpPath]); diff --git a/MediaGalleryCatalogIntegration/README.md b/MediaGalleryCatalogIntegration/README.md new file mode 100644 index 000000000000..bcb37bd486da --- /dev/null +++ b/MediaGalleryCatalogIntegration/README.md @@ -0,0 +1,3 @@ +# Magento_MediaGalleryCatalogIntegration + +The purpose of this module is for extending catalog image uploader functionality. diff --git a/MediaGalleryCatalogIntegration/composer.json b/MediaGalleryCatalogIntegration/composer.json new file mode 100644 index 000000000000..f71abbc93537 --- /dev/null +++ b/MediaGalleryCatalogIntegration/composer.json @@ -0,0 +1,27 @@ +{ + "name": "magento/module-media-gallery-catalog-integration", + "description": "Magento module responsible for extending catalog image uploader functionality", + "require": { + "php": "~7.3.0||~7.4.0", + "magento/framework": "*", + "magento/module-cms": "*", + "magento/module-media-gallery-api": "*", + "magento/module-media-gallery-synchronization-api": "*" + }, + "suggest": { + "magento/module-catalog": "*" + }, + "type": "magento2-module", + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Magento\\MediaGalleryCatalogIntegration\\": "" + } + } +} diff --git a/MediaGalleryCatalogIntegration/etc/adminhtml/di.xml b/MediaGalleryCatalogIntegration/etc/adminhtml/di.xml new file mode 100644 index 000000000000..2f8fab34911d --- /dev/null +++ b/MediaGalleryCatalogIntegration/etc/adminhtml/di.xml @@ -0,0 +1,12 @@ + + + + + + + diff --git a/MediaGalleryCatalogIntegration/etc/module.xml b/MediaGalleryCatalogIntegration/etc/module.xml new file mode 100644 index 000000000000..c9f1164121e9 --- /dev/null +++ b/MediaGalleryCatalogIntegration/etc/module.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/MediaGalleryCatalogIntegration/modman b/MediaGalleryCatalogIntegration/modman new file mode 100644 index 000000000000..68c59e4ff3d9 --- /dev/null +++ b/MediaGalleryCatalogIntegration/modman @@ -0,0 +1 @@ +./ app/code/Magento/MediaGalleryCatalogIntegration diff --git a/MediaGalleryCatalogIntegration/registration.php b/MediaGalleryCatalogIntegration/registration.php new file mode 100644 index 000000000000..9495790092df --- /dev/null +++ b/MediaGalleryCatalogIntegration/registration.php @@ -0,0 +1,14 @@ + - - - diff --git a/MediaGallerySynchronization/Model/CreateAssetFromFile.php b/MediaGallerySynchronization/Model/CreateAssetFromFile.php index 41340f2c9632..2a702c21e6e9 100644 --- a/MediaGallerySynchronization/Model/CreateAssetFromFile.php +++ b/MediaGallerySynchronization/Model/CreateAssetFromFile.php @@ -7,19 +7,12 @@ namespace Magento\MediaGallerySynchronization\Model; -use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\ValidatorException; -use Magento\Framework\Filesystem; -use Magento\Framework\Filesystem\Directory\Read; -use Magento\Framework\Filesystem\Driver\File; use Magento\MediaGalleryApi\Api\Data\AssetInterface; use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface; -use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterface; use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface; -use Magento\MediaGallerySynchronizationApi\Model\GetContentHashInterface; /** * Create media asset object based on the file information @@ -27,35 +20,20 @@ */ class CreateAssetFromFile { - /** - * @var Filesystem - */ - private $filesystem; - - /** - * @var File - */ - private $driver; - /** * @var GetAssetsByPathsInterface */ private $getMediaGalleryAssetByPath; - /** - * @var Read - */ - private $mediaDirectory; - /** * @var AssetInterfaceFactory */ private $assetFactory; /** - * @var GetContentHashInterface + * @var UpdateAsset */ - private $getContentHash; + private $updateAsset; /** * @var ExtractMetadataInterface @@ -63,26 +41,20 @@ class CreateAssetFromFile private $extractMetadata; /** - * @param Filesystem $filesystem * @param AssetInterfaceFactory $assetFactory - * @param File $driver * @param GetAssetsByPathsInterface $getMediaGalleryAssetByPath - * @param GetContentHashInterface $getContentHash + * @param UpdateAsset $updateAsset * @param ExtractMetadataInterface $extractMetadata */ public function __construct( - Filesystem $filesystem, AssetInterfaceFactory $assetFactory, - File $driver, GetAssetsByPathsInterface $getMediaGalleryAssetByPath, - GetContentHashInterface $getContentHash, + UpdateAsset $updateAsset, ExtractMetadataInterface $extractMetadata ) { - $this->filesystem = $filesystem; $this->assetFactory = $assetFactory; - $this->driver = $driver; $this->getMediaGalleryAssetByPath = $getMediaGalleryAssetByPath; - $this->getContentHash = $getContentHash; + $this->updateAsset = $updateAsset; $this->extractMetadata = $extractMetadata; } @@ -97,47 +69,11 @@ public function __construct( public function execute(\SplFileInfo $file): AssetInterface { $path = $file->getPath() . '/' . $file->getFileName(); - - [$width, $height] = getimagesize($path); $asset = $this->getAsset($path); - $metadata = $this->extractMetadata->execute($path); + $updatedAsset = $this->updateAsset->execute($file, $asset, $metadata); - return $this->assetFactory->create( - [ - 'id' => $asset ? $asset->getId() : null, - 'path' => $this->getRelativePath($path), - 'title' => $this->getAssetTitle($file, $asset, $metadata), - 'description' => $metadata->getDescription(), - 'createdAt' => $asset ? $asset->getCreatedAt() : - (new \DateTime())->setTimestamp($file->getCTime())->format('Y-m-d H:i:s'), - 'updatedAt' => (new \DateTime())->setTimestamp($file->getMTime())->format('Y-m-d H:i:s'), - 'width' => $width, - 'height' => $height, - 'hash' => $this->getHashImageContent($path), - 'size' => $file->getSize(), - 'contentType' => $asset ? $asset->getContentType() : 'image/' . $file->getExtension(), - 'source' => $asset ? $asset->getSource() : 'Local' - ] - ); - } - - /** - * Returns asset title from metadata if available - * - * @param \SplFileInfo $file - * @param null|AssetInterface $asset - * @param MetadataInterface $metadata - */ - private function getAssetTitle(\SplFileInfo $file, ?AssetInterface $asset, MetadataInterface $metadata): string - { - $title = $file->getBasename('.' . $file->getExtension()); - if ($asset) { - $title = $asset->getTitle(); - } elseif ($metadata->getTitle() !== "") { - $title = $metadata->getTitle(); - } - return $title; + return $updatedAsset; } /** @@ -150,54 +86,7 @@ private function getAssetTitle(\SplFileInfo $file, ?AssetInterface $asset, Metad */ private function getAsset(string $path): ?AssetInterface { - $asset = $this->getMediaGalleryAssetByPath->execute([$this->getRelativePath($path)]); + $asset = $this->getMediaGalleryAssetByPath->execute([$this->updateAsset->getRelativePath($path)]); return !empty($asset) ? $asset[0] : null; } - - /** - * Get correct path for media asset - * - * @param string $file - * @return string - * @throws ValidatorException - */ - private function getRelativePath(string $file): string - { - $path = $this->getMediaDirectory()->getRelativePath($file); - - if ($this->driver->getParentDirectory($path) === '.') { - $path = '/' . $path; - } - - return $path; - } - - /** - * Get hash image content. - * - * @param string $path - * @return string - * @throws ValidatorException - * @throws FileSystemException - */ - private function getHashImageContent(string $path): string - { - $mediaDirectory = $this->getMediaDirectory(); - $imageDirectory = $mediaDirectory->readFile($mediaDirectory->getRelativePath($path)); - $hashedImageContent = $this->getContentHash->execute($imageDirectory); - return $hashedImageContent; - } - - /** - * Retrieve media directory instance with read permissions - * - * @return Read - */ - private function getMediaDirectory(): Read - { - if (!$this->mediaDirectory) { - $this->mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); - } - return $this->mediaDirectory; - } } diff --git a/MediaGallerySynchronization/Model/UpdateAsset.php b/MediaGallerySynchronization/Model/UpdateAsset.php new file mode 100644 index 000000000000..75a007c534ab --- /dev/null +++ b/MediaGallerySynchronization/Model/UpdateAsset.php @@ -0,0 +1,182 @@ +filesystem = $filesystem; + $this->driver = $driver; + $this->date = $date; + $this->assetFactory = $assetFactory; + $this->getContentHash = $getContentHash; + } + + /** + * Create and format media asset object + * + * @param \SplFileInfo $file + * @param AssetInterface|null $asset + * @param MetadataInterface $assetFromFile + * @return AssetInterface + * @throws FileSystemException + * @throws ValidatorException + */ + public function execute(\SplFileInfo $file, ?AssetInterface $asset, MetadataInterface $assetFromFile): AssetInterface + { + $path = $file->getPath() . '/' . $file->getFileName(); + [$width, $height] = getimagesize($path); + + $assetFactory = $this->assetFactory->create( + [ + 'id' => $asset ? $asset->getId() : null, + 'path' => $this->getRelativePath($path), + 'title' => $this->getAssetTitle($file, $asset, $assetFromFile), + 'description' => $assetFromFile->getDescription(), + 'createdAt' => $asset ? $asset->getCreatedAt() : + $this->date->date($file->getCTime())->format(self::DATE_FORMAT), + 'updatedAt' => $this->date->date($file->getMTime())->format(self::DATE_FORMAT), + 'width' => $width, + 'height' => $height, + 'hash' => $this->getHashImageContent($path), + 'size' => $file->getSize(), + 'contentType' => $asset ? $asset->getContentType() : 'image/' . $file->getExtension(), + 'source' => $asset ? $asset->getSource() : 'Local' + ] + ); + + return $assetFactory; + } + + /** + * Returns asset title from metadata if available + * + * @param \SplFileInfo $file + * @param null|AssetInterface $asset + * @param MetadataInterface $metadata + * @return string + */ + private function getAssetTitle(\SplFileInfo $file, ?AssetInterface $asset, MetadataInterface $metadata): string + { + $title = $file->getBasename('.' . $file->getExtension()); + if ($asset) { + $title = $asset->getTitle(); + } elseif ($metadata->getTitle() !== "") { + $title = $metadata->getTitle(); + } + + return $title; + } + + /** + * Get correct path for media asset + * + * @param string $file + * @return string + * @throws ValidatorException + */ + public function getRelativePath(string $file): string + { + $path = $this->getMediaDirectory()->getRelativePath($file); + + if ($this->driver->getParentDirectory($path) === '.') { + $path = '/' . $path; + } + + return $path; + } + + /** + * Get hash image content. + * + * @param string $path + * @return string + * @throws ValidatorException + * @throws FileSystemException + */ + private function getHashImageContent(string $path): string + { + $mediaDirectory = $this->getMediaDirectory(); + $imageDirectory = $mediaDirectory->readFile($mediaDirectory->getRelativePath($path)); + $hashedImageContent = $this->getContentHash->execute($imageDirectory); + return $hashedImageContent; + } + + /** + * Retrieve media directory instance with read permissions + * + * @return Read + */ + private function getMediaDirectory(): Read + { + if (!$this->mediaDirectory) { + $this->mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); + } + return $this->mediaDirectory; + } +} diff --git a/_metapackage/composer.json b/_metapackage/composer.json index 13a15709030c..a6e60238f256 100644 --- a/_metapackage/composer.json +++ b/_metapackage/composer.json @@ -25,6 +25,7 @@ "magento/module-media-gallery-metadata": "*", "magento/module-media-gallery-metadata-api": "*", "magento/module-media-gallery-catalog-ui": "*", - "magento/module-media-gallery-cms-ui": "*" + "magento/module-media-gallery-cms-ui": "*", + "magento/module-media-gallery-catalog-integration": "*" } } diff --git a/modman b/modman index bafe06a31c0a..6d7c953e291b 100644 --- a/modman +++ b/modman @@ -21,3 +21,4 @@ @import MediaGalleryMetadataApi @import MediaGalleryCatalogUi @import MediaGalleryCmsUi +@import MediaGalleryCatalogIntegration From e2f3f490ec4c2ca736df29990bb915c7049dad7b Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Sun, 2 Aug 2020 18:42:29 +0100 Subject: [PATCH 6/9] magento/adobe-stock-integration#1645: Corrected implementation based on recent changes --- .../Console/Command/Synchronize.php | 19 +- .../Api/ConfigInterface.php | 0 .../Model/CreateAssetFromFile.php | 114 +++++++---- .../Model/GetAssetFromPath.php | 106 ++++++++++ .../Model/ImportMediaAsset.php | 22 +-- .../Model/Synchronize.php | 5 + .../Model/UpdateAsset.php | 182 ------------------ 7 files changed, 213 insertions(+), 235 deletions(-) delete mode 100644 MediaGalleryRenditionsApi/Api/ConfigInterface.php create mode 100644 MediaGallerySynchronization/Model/GetAssetFromPath.php delete mode 100644 MediaGallerySynchronization/Model/UpdateAsset.php diff --git a/MediaContentSynchronization/Console/Command/Synchronize.php b/MediaContentSynchronization/Console/Command/Synchronize.php index e591b4f2339b..55f99697c289 100644 --- a/MediaContentSynchronization/Console/Command/Synchronize.php +++ b/MediaContentSynchronization/Console/Command/Synchronize.php @@ -7,6 +7,8 @@ namespace Magento\MediaContentSynchronization\Console\Command; +use Magento\Framework\App\Area; +use Magento\Framework\App\State; use Magento\Framework\Console\Cli; use Magento\MediaContentSynchronizationApi\Api\SynchronizeInterface; use Symfony\Component\Console\Command\Command; @@ -23,13 +25,21 @@ class Synchronize extends Command */ private $synchronizeContent; + /** + * @var State $state + */ + private $state; + /** * @param SynchronizeInterface $synchronizeContent + * @param State $state */ public function __construct( - SynchronizeInterface $synchronizeContent + SynchronizeInterface $synchronizeContent, + State $state ) { $this->synchronizeContent = $synchronizeContent; + $this->state = $state; parent::__construct(); } @@ -48,7 +58,12 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Synchronizing content with assets...'); - $this->synchronizeContent->execute(); + $this->state->emulateAreaCode( + Area::AREA_ADMINHTML, + function () { + $this->synchronizeContent->execute(); + } + ); $output->writeln('Completed content synchronization.'); return Cli::RETURN_SUCCESS; } diff --git a/MediaGalleryRenditionsApi/Api/ConfigInterface.php b/MediaGalleryRenditionsApi/Api/ConfigInterface.php deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/MediaGallerySynchronization/Model/CreateAssetFromFile.php b/MediaGallerySynchronization/Model/CreateAssetFromFile.php index 2a702c21e6e9..4df1def76b23 100644 --- a/MediaGallerySynchronization/Model/CreateAssetFromFile.php +++ b/MediaGallerySynchronization/Model/CreateAssetFromFile.php @@ -7,23 +7,38 @@ namespace Magento\MediaGallerySynchronization\Model; -use Magento\Framework\Exception\LocalizedException; -use Magento\Framework\Exception\ValidatorException; +use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\Exception\FileSystemException; +use Magento\Framework\Filesystem; +use Magento\Framework\Filesystem\Driver\File; +use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\MediaGalleryApi\Api\Data\AssetInterface; use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory; -use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface; use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface; +use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory; +use Magento\MediaGallerySynchronizationApi\Model\GetContentHashInterface; -/** - * Create media asset object based on the file information - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - */ class CreateAssetFromFile { /** - * @var GetAssetsByPathsInterface + * Date format + */ + private const DATE_FORMAT = 'Y-m-d H:i:s'; + + /** + * @var Filesystem + */ + private $filesystem; + + /** + * @var File */ - private $getMediaGalleryAssetByPath; + private $driver; + + /** + * @var TimezoneInterface; + */ + private $date; /** * @var AssetInterfaceFactory @@ -31,9 +46,9 @@ class CreateAssetFromFile private $assetFactory; /** - * @var UpdateAsset + * @var GetContentHashInterface */ - private $updateAsset; + private $getContentHash; /** * @var ExtractMetadataInterface @@ -41,52 +56,81 @@ class CreateAssetFromFile private $extractMetadata; /** + * @var SplFileInfoFactory + */ + private $splFileInfoFactory; + + /** + * @param Filesystem $filesystem + * @param File $driver + * @param TimezoneInterface $date * @param AssetInterfaceFactory $assetFactory - * @param GetAssetsByPathsInterface $getMediaGalleryAssetByPath - * @param UpdateAsset $updateAsset + * @param GetContentHashInterface $getContentHash * @param ExtractMetadataInterface $extractMetadata + * @param SplFileInfoFactory $splFileInfoFactory */ public function __construct( + Filesystem $filesystem, + File $driver, + TimezoneInterface $date, AssetInterfaceFactory $assetFactory, - GetAssetsByPathsInterface $getMediaGalleryAssetByPath, - UpdateAsset $updateAsset, - ExtractMetadataInterface $extractMetadata + GetContentHashInterface $getContentHash, + ExtractMetadataInterface $extractMetadata, + SplFileInfoFactory $splFileInfoFactory ) { + $this->filesystem = $filesystem; + $this->driver = $driver; + $this->date = $date; $this->assetFactory = $assetFactory; - $this->getMediaGalleryAssetByPath = $getMediaGalleryAssetByPath; - $this->updateAsset = $updateAsset; + $this->getContentHash = $getContentHash; $this->extractMetadata = $extractMetadata; + $this->splFileInfoFactory = $splFileInfoFactory; } /** - * Create media asset object based on the file information + * Create and format media asset object * - * @param \SplFileInfo $file + * @param string $path * @return AssetInterface - * @throws LocalizedException - * @throws ValidatorException + * @throws FileSystemException */ - public function execute(\SplFileInfo $file): AssetInterface + public function execute(string $path): AssetInterface { - $path = $file->getPath() . '/' . $file->getFileName(); - $asset = $this->getAsset($path); - $metadata = $this->extractMetadata->execute($path); - $updatedAsset = $this->updateAsset->execute($file, $asset, $metadata); + $file = $this->splFileInfoFactory->create($path); + $absolutePath = $file->getPath() . '/' . $file->getFileName(); + [$width, $height] = getimagesize($absolutePath); + + $metadata = $this->extractMetadata->execute($absolutePath); - return $updatedAsset; + return $this->assetFactory->create( + [ + 'id' => null, + 'path' => $path, + 'title' => $metadata->getTitle() ?: $file->getBasename('.' . $file->getExtension()), + 'description' => $metadata->getDescription(), + 'createdAt' => $this->date->date($file->getCTime())->format(self::DATE_FORMAT), + 'updatedAt' => $this->date->date($file->getMTime())->format(self::DATE_FORMAT), + 'width' => $width, + 'height' => $height, + 'hash' => $this->getHash($path), + 'size' => $file->getSize(), + 'contentType' => 'image/' . $file->getExtension(), + 'source' => 'Local' + ] + ); } /** - * Returns asset if asset already exist by provided path + * Get hash image content. * * @param string $path - * @return AssetInterface|null - * @throws ValidatorException - * @throws LocalizedException + * @return string + * @throws FileSystemException */ - private function getAsset(string $path): ?AssetInterface + private function getHash(string $path): string { - $asset = $this->getMediaGalleryAssetByPath->execute([$this->updateAsset->getRelativePath($path)]); - return !empty($asset) ? $asset[0] : null; + return $this->getContentHash->execute( + $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->readFile($path) + ); } } diff --git a/MediaGallerySynchronization/Model/GetAssetFromPath.php b/MediaGallerySynchronization/Model/GetAssetFromPath.php new file mode 100644 index 000000000000..5e825d57c5ce --- /dev/null +++ b/MediaGallerySynchronization/Model/GetAssetFromPath.php @@ -0,0 +1,106 @@ +assetFactory = $assetFactory; + $this->getAssetsByPaths = $getMediaGalleryAssetByPath; + $this->createAssetFromFile = $createAssetFromFile; + $this->splFileInfoFactory= $splFileInfoFactory; + } + + /** + * Create media asset object based on the file information + * + * @param string $path + * @return AssetInterface + * @throws LocalizedException + * @throws ValidatorException + */ + public function execute(string $path): AssetInterface + { + $asset = $this->getAsset($path); + $assetFromFile = $this->createAssetFromFile->execute($path); + + if (!$asset) { + return $assetFromFile; + } + + return $this->assetFactory->create( + [ + 'id' => $asset->getId(), + 'path' => $path, + 'title' => $asset->getTitle(), + 'description' => $asset->getDescription() ?? $assetFromFile->getDescription(), + 'width' => $assetFromFile->getWidth(), + 'height' => $assetFromFile->getHeight(), + 'hash' => $assetFromFile->getHash(), + 'size' => $assetFromFile->getSize(), + 'contentType' => $asset->getContentType(), + 'source' => $asset->getSource() + ] + ); + } + + /** + * Returns asset if asset already exist by provided path + * + * @param string $path + * @return AssetInterface|null + * @throws ValidatorException + * @throws LocalizedException + */ + private function getAsset(string $path): ?AssetInterface + { + $asset = $this->getAssetsByPaths->execute([$path]); + return !empty($asset) ? $asset[0] : null; + } +} diff --git a/MediaGallerySynchronization/Model/ImportMediaAsset.php b/MediaGallerySynchronization/Model/ImportMediaAsset.php index 98352ef01250..fbc10e7a2f8c 100644 --- a/MediaGallerySynchronization/Model/ImportMediaAsset.php +++ b/MediaGallerySynchronization/Model/ImportMediaAsset.php @@ -9,7 +9,6 @@ use Magento\MediaGallerySynchronizationApi\Api\ImportFileInterface; use Magento\MediaGalleryApi\Api\SaveAssetsInterface; -use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory; /** * Import image file to the media gallery asset table @@ -22,28 +21,20 @@ class ImportMediaAsset implements ImportFileInterface private $saveAssets; /** - * @var SplFileInfoFactory + * @var GetAssetFromPath */ - private $splFileInfoFactory; + private $getAssetFromPath; /** - * @var CreateAssetFromFile - */ - private $createAssetFromFile; - - /** - * @param SplFileInfoFactory $splFileInfoFactory * @param SaveAssetsInterface $saveAssets - * @param CreateAssetFromFile $createAssetFromFile + * @param GetAssetFromPath $getAssetFromPath */ public function __construct( - SplFileInfoFactory $splFileInfoFactory, SaveAssetsInterface $saveAssets, - CreateAssetFromFile $createAssetFromFile + GetAssetFromPath $getAssetFromPath ) { - $this->splFileInfoFactory = $splFileInfoFactory; $this->saveAssets = $saveAssets; - $this->createAssetFromFile = $createAssetFromFile; + $this->getAssetFromPath = $getAssetFromPath; } /** @@ -51,7 +42,6 @@ public function __construct( */ public function execute(string $path): void { - $file = $this->splFileInfoFactory->create($path); - $this->saveAssets->execute([$this->createAssetFromFile->execute($file)]); + $this->saveAssets->execute([$this->getAssetFromPath->execute($path)]); } } diff --git a/MediaGallerySynchronization/Model/Synchronize.php b/MediaGallerySynchronization/Model/Synchronize.php index 57276b87a4f9..4396ea6a7773 100644 --- a/MediaGallerySynchronization/Model/Synchronize.php +++ b/MediaGallerySynchronization/Model/Synchronize.php @@ -8,6 +8,7 @@ namespace Magento\MediaGallerySynchronization\Model; use Magento\Framework\Exception\LocalizedException; +use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface; use Magento\MediaGallerySynchronizationApi\Api\SynchronizeInterface; use Magento\MediaGallerySynchronizationApi\Model\SynchronizerPool; use Psr\Log\LoggerInterface; @@ -63,6 +64,10 @@ public function execute(): void $failed = []; foreach ($this->synchronizerPool->get() as $name => $synchronizer) { + if (!$synchronizer instanceof SynchronizeFilesInterface) { + $failed[] = $name; + continue; + } foreach ($this->batchGenerator->execute() as $batch) { try { $synchronizer->execute($batch); diff --git a/MediaGallerySynchronization/Model/UpdateAsset.php b/MediaGallerySynchronization/Model/UpdateAsset.php deleted file mode 100644 index 75a007c534ab..000000000000 --- a/MediaGallerySynchronization/Model/UpdateAsset.php +++ /dev/null @@ -1,182 +0,0 @@ -filesystem = $filesystem; - $this->driver = $driver; - $this->date = $date; - $this->assetFactory = $assetFactory; - $this->getContentHash = $getContentHash; - } - - /** - * Create and format media asset object - * - * @param \SplFileInfo $file - * @param AssetInterface|null $asset - * @param MetadataInterface $assetFromFile - * @return AssetInterface - * @throws FileSystemException - * @throws ValidatorException - */ - public function execute(\SplFileInfo $file, ?AssetInterface $asset, MetadataInterface $assetFromFile): AssetInterface - { - $path = $file->getPath() . '/' . $file->getFileName(); - [$width, $height] = getimagesize($path); - - $assetFactory = $this->assetFactory->create( - [ - 'id' => $asset ? $asset->getId() : null, - 'path' => $this->getRelativePath($path), - 'title' => $this->getAssetTitle($file, $asset, $assetFromFile), - 'description' => $assetFromFile->getDescription(), - 'createdAt' => $asset ? $asset->getCreatedAt() : - $this->date->date($file->getCTime())->format(self::DATE_FORMAT), - 'updatedAt' => $this->date->date($file->getMTime())->format(self::DATE_FORMAT), - 'width' => $width, - 'height' => $height, - 'hash' => $this->getHashImageContent($path), - 'size' => $file->getSize(), - 'contentType' => $asset ? $asset->getContentType() : 'image/' . $file->getExtension(), - 'source' => $asset ? $asset->getSource() : 'Local' - ] - ); - - return $assetFactory; - } - - /** - * Returns asset title from metadata if available - * - * @param \SplFileInfo $file - * @param null|AssetInterface $asset - * @param MetadataInterface $metadata - * @return string - */ - private function getAssetTitle(\SplFileInfo $file, ?AssetInterface $asset, MetadataInterface $metadata): string - { - $title = $file->getBasename('.' . $file->getExtension()); - if ($asset) { - $title = $asset->getTitle(); - } elseif ($metadata->getTitle() !== "") { - $title = $metadata->getTitle(); - } - - return $title; - } - - /** - * Get correct path for media asset - * - * @param string $file - * @return string - * @throws ValidatorException - */ - public function getRelativePath(string $file): string - { - $path = $this->getMediaDirectory()->getRelativePath($file); - - if ($this->driver->getParentDirectory($path) === '.') { - $path = '/' . $path; - } - - return $path; - } - - /** - * Get hash image content. - * - * @param string $path - * @return string - * @throws ValidatorException - * @throws FileSystemException - */ - private function getHashImageContent(string $path): string - { - $mediaDirectory = $this->getMediaDirectory(); - $imageDirectory = $mediaDirectory->readFile($mediaDirectory->getRelativePath($path)); - $hashedImageContent = $this->getContentHash->execute($imageDirectory); - return $hashedImageContent; - } - - /** - * Retrieve media directory instance with read permissions - * - * @return Read - */ - private function getMediaDirectory(): Read - { - if (!$this->mediaDirectory) { - $this->mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); - } - return $this->mediaDirectory; - } -} From e2cb5a5639e8214b91cfbeaa2f36155bd1938db2 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Sun, 2 Aug 2020 18:51:35 +0100 Subject: [PATCH 7/9] magento/adobe-stock-integration#1645: Updates after merge --- .../SaveBaseCategoryImageInformation.php | 6 ++-- .../Model/ImportMediaAsset.php | 35 ++++--------------- 2 files changed, 10 insertions(+), 31 deletions(-) diff --git a/MediaGalleryCatalogIntegration/Plugin/SaveBaseCategoryImageInformation.php b/MediaGalleryCatalogIntegration/Plugin/SaveBaseCategoryImageInformation.php index f15a4687db20..d439b53c120c 100644 --- a/MediaGalleryCatalogIntegration/Plugin/SaveBaseCategoryImageInformation.php +++ b/MediaGalleryCatalogIntegration/Plugin/SaveBaseCategoryImageInformation.php @@ -5,15 +5,15 @@ */ declare(strict_types=1); -namespace Magento\MediaGalleryIntegration\Plugin; +namespace Magento\MediaGalleryCatalogIntegration\Plugin; use Magento\Catalog\Model\ImageUploader; use Magento\Cms\Model\Wysiwyg\Images\Storage; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Filesystem; use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface; use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface; -use Magento\MediaGalleryApi\Api\SaveAssetsInterface; use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface; use Magento\MediaGalleryUiApi\Api\ConfigInterface; @@ -81,6 +81,8 @@ public function __construct( * * @param ImageUploader $subject * @param string $imagePath + * @return string + * @throws LocalizedException */ public function afterMoveFileFromTmp(ImageUploader $subject, string $imagePath): string { diff --git a/MediaGallerySynchronization/Model/ImportMediaAsset.php b/MediaGallerySynchronization/Model/ImportMediaAsset.php index a5afbf5b6279..3cac99f816d1 100644 --- a/MediaGallerySynchronization/Model/ImportMediaAsset.php +++ b/MediaGallerySynchronization/Model/ImportMediaAsset.php @@ -7,10 +7,7 @@ namespace Magento\MediaGallerySynchronization\Model; -use Magento\Framework\App\Filesystem\DirectoryList; -use Magento\Framework\Filesystem; use Magento\MediaGalleryApi\Api\SaveAssetsInterface; -use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory; use Magento\MediaGallerySynchronizationApi\Model\ImportFilesInterface; /** @@ -24,36 +21,20 @@ class ImportMediaAsset implements ImportFilesInterface private $saveAssets; /** - * @var SplFileInfoFactory + * @var GetAssetFromPath */ - private $splFileInfoFactory; + private $getAssetFromPath; /** - * @var CreateAssetFromFile - */ - private $createAssetFromFile; - - /** - * @var Filesystem - */ - private $filesystem; - - /** - * @param SplFileInfoFactory $splFileInfoFactory * @param SaveAssetsInterface $saveAssets - * @param CreateAssetFromFile $createAssetFromFile - * @param Filesystem $filesystem + * @param GetAssetFromPath $getAssetFromPath */ public function __construct( - SplFileInfoFactory $splFileInfoFactory, SaveAssetsInterface $saveAssets, - CreateAssetFromFile $createAssetFromFile, - Filesystem $filesystem + GetAssetFromPath $getAssetFromPath ) { - $this->splFileInfoFactory = $splFileInfoFactory; $this->saveAssets = $saveAssets; - $this->createAssetFromFile = $createAssetFromFile; - $this->filesystem = $filesystem; + $this->getAssetFromPath = $getAssetFromPath; } /** @@ -64,11 +45,7 @@ public function execute(array $paths): void $assets = []; foreach ($paths as $path) { - $assets[] = $this->createAssetFromFile->execute( - $this->splFileInfoFactory->create( - $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($path) - ) - ); + $assets[] = $this->getAssetFromPath->execute($path); } $this->saveAssets->execute($assets); From 7518f9ffe407c709b5d428e590c05762f3133107 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Sun, 2 Aug 2020 19:03:55 +0100 Subject: [PATCH 8/9] magento/adobe-stock-integration#1645: Corrected absolute path calculation --- .../Model/CreateAssetFromFile.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/MediaGallerySynchronization/Model/CreateAssetFromFile.php b/MediaGallerySynchronization/Model/CreateAssetFromFile.php index 4df1def76b23..67806b97feab 100644 --- a/MediaGallerySynchronization/Model/CreateAssetFromFile.php +++ b/MediaGallerySynchronization/Model/CreateAssetFromFile.php @@ -10,6 +10,7 @@ use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Filesystem; +use Magento\Framework\Filesystem\Directory\ReadInterface; use Magento\Framework\Filesystem\Driver\File; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\MediaGalleryApi\Api\Data\AssetInterface; @@ -96,8 +97,8 @@ public function __construct( */ public function execute(string $path): AssetInterface { - $file = $this->splFileInfoFactory->create($path); - $absolutePath = $file->getPath() . '/' . $file->getFileName(); + $absolutePath = $this->getMediaDirectory()->getAbsolutePath($path); + $file = $this->splFileInfoFactory->create($absolutePath); [$width, $height] = getimagesize($absolutePath); $metadata = $this->extractMetadata->execute($absolutePath); @@ -129,8 +130,16 @@ public function execute(string $path): AssetInterface */ private function getHash(string $path): string { - return $this->getContentHash->execute( - $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->readFile($path) - ); + return $this->getContentHash->execute($this->getMediaDirectory()->readFile($path)); + } + + /** + * Retrieve media directory instance with read access + * + * @return ReadInterface + */ + private function getMediaDirectory(): ReadInterface + { + return $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); } } From b3a1cf50e1c2646e86e74c50ce5402e213167fa9 Mon Sep 17 00:00:00 2001 From: Sergii Ivashchenko Date: Sun, 2 Aug 2020 20:51:20 +0100 Subject: [PATCH 9/9] Fixed static tests --- MediaGalleryCatalogIntegration/composer.json | 3 ++- MediaGallerySynchronizationApi/Model/SynchronizerPool.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/MediaGalleryCatalogIntegration/composer.json b/MediaGalleryCatalogIntegration/composer.json index f71abbc93537..efabb70da9f3 100644 --- a/MediaGalleryCatalogIntegration/composer.json +++ b/MediaGalleryCatalogIntegration/composer.json @@ -6,7 +6,8 @@ "magento/framework": "*", "magento/module-cms": "*", "magento/module-media-gallery-api": "*", - "magento/module-media-gallery-synchronization-api": "*" + "magento/module-media-gallery-synchronization-api": "*", + "magento/module-media-gallery-ui-api": "*" }, "suggest": { "magento/module-catalog": "*" diff --git a/MediaGallerySynchronizationApi/Model/SynchronizerPool.php b/MediaGallerySynchronizationApi/Model/SynchronizerPool.php index 3b3d0885e613..1294a4f7679f 100644 --- a/MediaGallerySynchronizationApi/Model/SynchronizerPool.php +++ b/MediaGallerySynchronizationApi/Model/SynchronizerPool.php @@ -30,7 +30,8 @@ public function __construct(array $synchronizers = []) foreach ($synchronizers as $name => $synchronizer) { if (!$synchronizer instanceof SynchronizeFilesInterface) { throw new \InvalidArgumentException(sprintf( - 'Synchronizer must implement %s.', + 'Synchronizer %s must implement %s.', + $name, SynchronizeFilesInterface::class )); }