Skip to content

Commit

Permalink
Fix media channel support (#386)
Browse files Browse the repository at this point in the history
* Add channel support for media repository

* Add missing channel to behat tests
  • Loading branch information
em411 authored Aug 4, 2021
1 parent e4de01c commit 830a4d4
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/Repository/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/MediaRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 11 additions & 1 deletion src/Resolver/MediaResourceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/routing/shop/media.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/resolver.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion tests/Behat/Context/Setup/MediaContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
19 changes: 13 additions & 6 deletions tests/Behat/Context/Transform/MediaContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@
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
{
/** @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;
}

/**
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/Behat/Resources/services/contexts/transform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 830a4d4

Please sign in to comment.