Skip to content

Commit 830a4d4

Browse files
authored
Fix media channel support (#386)
* Add channel support for media repository * Add missing channel to behat tests
1 parent e4de01c commit 830a4d4

File tree

8 files changed

+40
-11
lines changed

8 files changed

+40
-11
lines changed

src/Repository/MediaRepository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@ public function createListQueryBuilder(string $locale): QueryBuilder
2626
;
2727
}
2828

29-
public function findOneEnabledByCode(string $code, string $localeCode): ?MediaInterface
29+
public function findOneEnabledByCode(string $code, string $localeCode, string $channelCode): ?MediaInterface
3030
{
3131
return $this->createQueryBuilder('o')
3232
->leftJoin('o.translations', 'translation')
33+
->innerJoin('o.channels', 'channels')
3334
->where('translation.locale = :localeCode')
3435
->andWhere('o.code = :code')
3536
->andWhere('o.enabled = true')
37+
->andWhere('channels.code = :channelCode')
3638
->setParameter('code', $code)
3739
->setParameter('localeCode', $localeCode)
40+
->setParameter('channelCode', $channelCode)
3841
->getQuery()
3942
->getOneOrNullResult()
4043
;

src/Repository/MediaRepositoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ interface MediaRepositoryInterface extends RepositoryInterface
2020
{
2121
public function createListQueryBuilder(string $locale): QueryBuilder;
2222

23-
public function findOneEnabledByCode(string $code, string $localeCode): ?MediaInterface;
23+
public function findOneEnabledByCode(string $code, string $localeCode, string $channelCode): ?MediaInterface;
2424

2525
public function findBySectionCode(string $sectionCode, string $localeCode, string $channelCode): array;
2626

src/Resolver/MediaResourceResolver.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
1616
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
1717
use Psr\Log\LoggerInterface;
18+
use Sylius\Component\Channel\Context\ChannelContextInterface;
1819
use Sylius\Component\Locale\Context\LocaleContextInterface;
1920

2021
final class MediaResourceResolver implements MediaResourceResolverInterface
@@ -25,22 +26,31 @@ final class MediaResourceResolver implements MediaResourceResolverInterface
2526
/** @var LocaleContextInterface */
2627
private $localeContext;
2728

29+
/** @var ChannelContextInterface */
30+
private $channelContext;
31+
2832
/** @var LoggerInterface */
2933
private $logger;
3034

3135
public function __construct(
3236
MediaRepositoryInterface $mediaRepository,
3337
LocaleContextInterface $localeContext,
38+
ChannelContextInterface $channelContext,
3439
LoggerInterface $logger
3540
) {
3641
$this->mediaRepository = $mediaRepository;
3742
$this->localeContext = $localeContext;
43+
$this->channelContext = $channelContext;
3844
$this->logger = $logger;
3945
}
4046

4147
public function findOrLog(string $code): ?MediaInterface
4248
{
43-
$media = $this->mediaRepository->findOneEnabledByCode($code, $this->localeContext->getLocaleCode());
49+
$media = $this->mediaRepository->findOneEnabledByCode(
50+
$code,
51+
$this->localeContext->getLocaleCode(),
52+
$this->channelContext->getChannel()->getCode()
53+
);
4454

4555
if (false === $media instanceof MediaInterface) {
4656
$this->logger->warning(sprintf(

src/Resources/config/routing/shop/media.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bitbag_sylius_cms_plugin_shop_media_render_template:
1616
arguments:
1717
- $code
1818
- "expr:service('sylius.context.locale').getLocaleCode()"
19+
- "expr:service('sylius.context.channel').getChannel().getCode()"
1920

2021
bitbag_sylius_cms_plugin_shop_media_download:
2122
path: /media/download/{code}

src/Resources/config/services/resolver.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ services:
5252
arguments:
5353
- "@bitbag_sylius_cms_plugin.repository.media"
5454
- "@sylius.context.locale"
55+
- "@sylius.context.channel"
5556
- "@logger"
5657

5758
bitbag_sylius_cms_plugin.resolver.media_provider:

tests/Behat/Context/Setup/MediaContext.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use BitBag\SyliusCmsPlugin\Resolver\MediaProviderResolverInterface;
2020
use Doctrine\ORM\EntityManagerInterface;
2121
use Sylius\Behat\Service\SharedStorageInterface;
22+
use Sylius\Component\Core\Model\ChannelInterface;
2223
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
2324
use Sylius\Component\Resource\Factory\FactoryInterface;
2425
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -98,7 +99,8 @@ private function createMedia(
9899
?string $code = null,
99100
?string $name = null,
100101
?string $content = null,
101-
?string $fileType = null
102+
?string $fileType = null,
103+
ChannelInterface $channel = null
102104
): MediaInterface {
103105
/** @var MediaInterface $media */
104106
$media = $this->mediaFactory->createNew();
@@ -119,11 +121,16 @@ private function createMedia(
119121
$fileType = MediaInterface::FILE_TYPE;
120122
}
121123

124+
if (null === $channel && $this->sharedStorage->has('channel')) {
125+
$channel = $this->sharedStorage->get('channel');
126+
}
127+
122128
$media->setCode($code);
123129
$media->setCurrentLocale('en_US');
124130
$media->setName($name);
125131
$media->setContent($content);
126132
$media->setType($fileType);
133+
$media->addChannel($channel);
127134

128135
return $media;
129136
}

tests/Behat/Context/Transform/MediaContext.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@
1515
use Behat\Behat\Context\Context;
1616
use BitBag\SyliusCmsPlugin\Entity\MediaInterface;
1717
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
18+
use Sylius\Behat\Service\SharedStorageInterface;
1819
use Webmozart\Assert\Assert;
1920

2021
final class MediaContext implements Context
2122
{
2223
/** @var MediaRepositoryInterface */
2324
private $mediaRepositoryInterface;
2425

25-
/** @var string */
26-
private $locale;
26+
/** @var SharedStorageInterface */
27+
private $sharedStorage;
2728

28-
public function __construct(MediaRepositoryInterface $mediaRepositoryInterface, string $locale = 'en_US')
29-
{
29+
public function __construct(
30+
MediaRepositoryInterface $mediaRepositoryInterface,
31+
SharedStorageInterface $sharedStorage
32+
) {
3033
$this->mediaRepositoryInterface = $mediaRepositoryInterface;
31-
$this->locale = $locale;
34+
$this->sharedStorage = $sharedStorage;
3235
}
3336

3437
/**
@@ -39,7 +42,11 @@ public function __construct(MediaRepositoryInterface $mediaRepositoryInterface,
3942
*/
4043
public function getMediaByCode(string $mediaCode): MediaInterface
4144
{
42-
$media = $this->mediaRepositoryInterface->findOneEnabledByCode($mediaCode, $this->locale);
45+
$media = $this->mediaRepositoryInterface->findOneEnabledByCode(
46+
$mediaCode,
47+
$this->sharedStorage->get('locale')->getCode(),
48+
$this->sharedStorage->get('channel')->getCode()
49+
);
4350

4451
Assert::notNull(
4552
$media,

tests/Behat/Resources/services/contexts/transform.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
class: Tests\BitBag\SyliusCmsPlugin\Behat\Context\Transform\MediaContext
1717
arguments:
1818
- "@bitbag_sylius_cms_plugin.repository.media"
19-
- "%locale%"
19+
- '@sylius.behat.shared_storage'
2020

2121
bitbag_sylius_cms_plugin.behat.context.transform.page:
2222
class: Tests\BitBag\SyliusCmsPlugin\Behat\Context\Transform\PageContext

0 commit comments

Comments
 (0)