diff --git a/src/Repository/MediaRepository.php b/src/Repository/MediaRepository.php index 16071f856..f7e231151 100755 --- a/src/Repository/MediaRepository.php +++ b/src/Repository/MediaRepository.php @@ -26,15 +26,18 @@ public function createListQueryBuilder(string $locale): QueryBuilder ; } - public function findOneEnabledByCode(string $code, string $localeCode): ?MediaInterface + public function findOneEnabledByCode(string $code, string $localeCode, string $channelCode): ?MediaInterface { return $this->createQueryBuilder('o') ->leftJoin('o.translations', 'translation') + ->innerJoin('o.channels', 'channels') ->where('translation.locale = :localeCode') ->andWhere('o.code = :code') ->andWhere('o.enabled = true') + ->andWhere('channels.code = :channelCode') ->setParameter('code', $code) ->setParameter('localeCode', $localeCode) + ->setParameter('channelCode', $channelCode) ->getQuery() ->getOneOrNullResult() ; diff --git a/src/Repository/MediaRepositoryInterface.php b/src/Repository/MediaRepositoryInterface.php index 97c322fd0..2f4ba1845 100755 --- a/src/Repository/MediaRepositoryInterface.php +++ b/src/Repository/MediaRepositoryInterface.php @@ -20,7 +20,7 @@ interface MediaRepositoryInterface extends RepositoryInterface { public function createListQueryBuilder(string $locale): QueryBuilder; - public function findOneEnabledByCode(string $code, string $localeCode): ?MediaInterface; + public function findOneEnabledByCode(string $code, string $localeCode, string $channelCode): ?MediaInterface; public function findBySectionCode(string $sectionCode, string $localeCode, string $channelCode): array; diff --git a/src/Resolver/MediaResourceResolver.php b/src/Resolver/MediaResourceResolver.php index 76a084146..71bdbc735 100755 --- a/src/Resolver/MediaResourceResolver.php +++ b/src/Resolver/MediaResourceResolver.php @@ -15,6 +15,7 @@ use BitBag\SyliusCmsPlugin\Entity\MediaInterface; use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface; use Psr\Log\LoggerInterface; +use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Locale\Context\LocaleContextInterface; final class MediaResourceResolver implements MediaResourceResolverInterface @@ -25,22 +26,31 @@ final class MediaResourceResolver implements MediaResourceResolverInterface /** @var LocaleContextInterface */ private $localeContext; + /** @var ChannelContextInterface */ + private $channelContext; + /** @var LoggerInterface */ private $logger; public function __construct( MediaRepositoryInterface $mediaRepository, LocaleContextInterface $localeContext, + ChannelContextInterface $channelContext, LoggerInterface $logger ) { $this->mediaRepository = $mediaRepository; $this->localeContext = $localeContext; + $this->channelContext = $channelContext; $this->logger = $logger; } public function findOrLog(string $code): ?MediaInterface { - $media = $this->mediaRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode()); + $media = $this->mediaRepository->findOneEnabledByCode( + $code, + $this->localeContext->getLocaleCode(), + $this->channelContext->getChannel()->getCode() + ); if (false === $media instanceof MediaInterface) { $this->logger->warning(sprintf( diff --git a/src/Resources/config/routing/shop/media.yml b/src/Resources/config/routing/shop/media.yml index ee17da9a1..281732452 100755 --- a/src/Resources/config/routing/shop/media.yml +++ b/src/Resources/config/routing/shop/media.yml @@ -16,6 +16,7 @@ bitbag_sylius_cms_plugin_shop_media_render_template: arguments: - $code - "expr:service('sylius.context.locale').getLocaleCode()" + - "expr:service('sylius.context.channel').getChannel().getCode()" bitbag_sylius_cms_plugin_shop_media_download: path: /media/download/{code} diff --git a/src/Resources/config/services/resolver.yml b/src/Resources/config/services/resolver.yml index 43d0c4da2..60a0af6a7 100644 --- a/src/Resources/config/services/resolver.yml +++ b/src/Resources/config/services/resolver.yml @@ -52,6 +52,7 @@ services: arguments: - "@bitbag_sylius_cms_plugin.repository.media" - "@sylius.context.locale" + - "@sylius.context.channel" - "@logger" bitbag_sylius_cms_plugin.resolver.media_provider: diff --git a/tests/Behat/Context/Setup/MediaContext.php b/tests/Behat/Context/Setup/MediaContext.php index 883354556..fe4fe5979 100755 --- a/tests/Behat/Context/Setup/MediaContext.php +++ b/tests/Behat/Context/Setup/MediaContext.php @@ -19,6 +19,7 @@ use BitBag\SyliusCmsPlugin\Resolver\MediaProviderResolverInterface; use Doctrine\ORM\EntityManagerInterface; use Sylius\Behat\Service\SharedStorageInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Repository\ProductRepositoryInterface; use Sylius\Component\Resource\Factory\FactoryInterface; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -98,7 +99,8 @@ private function createMedia( ?string $code = null, ?string $name = null, ?string $content = null, - ?string $fileType = null + ?string $fileType = null, + ChannelInterface $channel = null ): MediaInterface { /** @var MediaInterface $media */ $media = $this->mediaFactory->createNew(); @@ -119,11 +121,16 @@ private function createMedia( $fileType = MediaInterface::FILE_TYPE; } + if (null === $channel && $this->sharedStorage->has('channel')) { + $channel = $this->sharedStorage->get('channel'); + } + $media->setCode($code); $media->setCurrentLocale('en_US'); $media->setName($name); $media->setContent($content); $media->setType($fileType); + $media->addChannel($channel); return $media; } diff --git a/tests/Behat/Context/Transform/MediaContext.php b/tests/Behat/Context/Transform/MediaContext.php index f0d89e07e..20bfe162b 100644 --- a/tests/Behat/Context/Transform/MediaContext.php +++ b/tests/Behat/Context/Transform/MediaContext.php @@ -15,6 +15,7 @@ use Behat\Behat\Context\Context; use BitBag\SyliusCmsPlugin\Entity\MediaInterface; use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface; +use Sylius\Behat\Service\SharedStorageInterface; use Webmozart\Assert\Assert; final class MediaContext implements Context @@ -22,13 +23,15 @@ final class MediaContext implements Context /** @var MediaRepositoryInterface */ private $mediaRepositoryInterface; - /** @var string */ - private $locale; + /** @var SharedStorageInterface */ + private $sharedStorage; - public function __construct(MediaRepositoryInterface $mediaRepositoryInterface, string $locale = 'en_US') - { + public function __construct( + MediaRepositoryInterface $mediaRepositoryInterface, + SharedStorageInterface $sharedStorage + ) { $this->mediaRepositoryInterface = $mediaRepositoryInterface; - $this->locale = $locale; + $this->sharedStorage = $sharedStorage; } /** @@ -39,7 +42,11 @@ public function __construct(MediaRepositoryInterface $mediaRepositoryInterface, */ public function getMediaByCode(string $mediaCode): MediaInterface { - $media = $this->mediaRepositoryInterface->findOneEnabledByCode($mediaCode, $this->locale); + $media = $this->mediaRepositoryInterface->findOneEnabledByCode( + $mediaCode, + $this->sharedStorage->get('locale')->getCode(), + $this->sharedStorage->get('channel')->getCode() + ); Assert::notNull( $media, diff --git a/tests/Behat/Resources/services/contexts/transform.yml b/tests/Behat/Resources/services/contexts/transform.yml index cc60c367d..848d7c05c 100644 --- a/tests/Behat/Resources/services/contexts/transform.yml +++ b/tests/Behat/Resources/services/contexts/transform.yml @@ -16,7 +16,7 @@ services: class: Tests\BitBag\SyliusCmsPlugin\Behat\Context\Transform\MediaContext arguments: - "@bitbag_sylius_cms_plugin.repository.media" - - "%locale%" + - '@sylius.behat.shared_storage' bitbag_sylius_cms_plugin.behat.context.transform.page: class: Tests\BitBag\SyliusCmsPlugin\Behat\Context\Transform\PageContext