Skip to content

Commit

Permalink
Merge pull request #132 from michaelzangerle/fix-media-adapter
Browse files Browse the repository at this point in the history
Fix media creation in ImageMediaAdapter
  • Loading branch information
Prokyonn authored Dec 11, 2023
2 parents 30c5582 + f6f5cf1 commit 1875720
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ Tests/Application/var
# npm
Resources/js/package-lock.json
Resources/js/node_modules

# IDE
./.idea
20 changes: 18 additions & 2 deletions Adapter/ImageMediaAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Sulu\Bundle\MediaBundle\Entity\FileVersionMeta;
use Sulu\Bundle\MediaBundle\Entity\Media;
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
use Sulu\Bundle\MediaBundle\Entity\MediaRepositoryInterface;
use Sulu\Bundle\MediaBundle\Entity\MediaType;
use Sulu\Bundle\MediaBundle\Media\Storage\StorageInterface;
use Sulu\Bundle\SyliusConsumerBundle\Payload\ImagePayload;
Expand Down Expand Up @@ -63,20 +64,35 @@ class ImageMediaAdapter implements ImageAdapterInterface
*/
private $syliusImageDownloader;

/**
* @var MediaRepositoryInterface|null
*/
private $mediaRepository;

public function __construct(
ImageMediaBridgeRepositoryInterface $mediaBridgeRepository,
EntityManagerInterface $entityManager,
StorageInterface $storage,
SystemCollectionManagerInterface $systemCollectionManager,
SyliusImageDownloaderInterface $syliusImageDownloader,
string $collectionKey
string $collectionKey,
?MediaRepositoryInterface $mediaRepository = null
) {
$this->mediaBridgeRepository = $mediaBridgeRepository;
$this->entityManager = $entityManager;
$this->storage = $storage;
$this->systemCollectionManager = $systemCollectionManager;
$this->collectionKey = $collectionKey;
$this->syliusImageDownloader = $syliusImageDownloader;
$this->mediaRepository = $mediaRepository;

if (null === $mediaRepository) {
@trigger_deprecation(
'sulu/sylius-consumer-bundle',
'0.5',
'Omitting the "mediaRepository" argument is deprecated and will not longer work in SuluSyliusConsumerBundle 1.0.'
);
}
}

public function synchronize(ImagePayload $payload): void
Expand All @@ -103,7 +119,7 @@ private function handlePayload(ImagePayload $payload): void
{
$bridge = $this->mediaBridgeRepository->findById($payload->getId());
if (!$bridge) {
$media = new Media();
$media = $this->mediaRepository ? $this->mediaRepository->createNew() : new Media();
$bridge = $this->mediaBridgeRepository->create($payload->getId(), $media);
}

Expand Down
3 changes: 2 additions & 1 deletion Resources/config/image_media_adapter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<argument type="service" id="sulu_media.system_collections.manager"/>
<argument type="service" id="Sulu\Bundle\SyliusConsumerBundle\Service\SyliusImageDownloaderInterface"/>
<argument>%sulu_sylius_consumer.media_collection.key%</argument>
<argument type="service" id="sulu.repository.media" />

<tag name="sulu_sylius_consumer.adapter.image" priority="1024"/>
</service>
Expand All @@ -23,4 +24,4 @@
<argument>%sulu_sylius_consumer.sylius_base_url%</argument>
</service>
</services>
</container>
</container>

0 comments on commit 1875720

Please sign in to comment.