Skip to content

Commit

Permalink
OP-496: Fix for finding media when translation is empty + remove dead…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
jkindly committed Aug 23, 2024
1 parent 19ef10c commit b26d99c
Show file tree
Hide file tree
Showing 13 changed files with 11 additions and 346 deletions.
13 changes: 3 additions & 10 deletions spec/Resolver/MediaResourceResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ final class MediaResourceResolverSpec extends ObjectBehavior
{
public function let(
MediaRepositoryInterface $mediaRepository,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
LoggerInterface $logger,
) {
$this->beConstructedWith($mediaRepository, $localeContext, $channelContext, $logger);
$this->beConstructedWith($mediaRepository, $channelContext, $logger);
}

public function it_is_initializable(): void
Expand All @@ -43,40 +42,34 @@ public function it_implements_media_resource_resolver_interface(): void

public function it_returns_media_when_found(
MediaRepositoryInterface $mediaRepository,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
MediaInterface $media,
ChannelInterface $channel,
) {
$code = 'media_code';
$localeCode = 'en_US';
$channelCode = 'ecommerce';

$channelContext->getChannel()->willReturn($channel);
$channel->getCode()->willReturn($channelCode);
$localeContext->getLocaleCode()->willReturn($localeCode);

$mediaRepository->findOneEnabledByCode($code, $localeCode, $channelCode)->willReturn($media);
$mediaRepository->findOneEnabledByCode($code, $channelCode)->willReturn($media);

$this->findOrLog($code)->shouldReturn($media);
}

public function it_logs_warning_and_returns_null_when_media_not_found(
MediaRepositoryInterface $mediaRepository,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
LoggerInterface $logger,
ChannelInterface $channel,
) {
$code = 'non_existing_code';
$localeCode = 'en_US';
$channelCode = 'ecommerce';

$channelContext->getChannel()->willReturn($channel);
$channel->getCode()->willReturn($channelCode);
$localeContext->getLocaleCode()->willReturn($localeCode);

$mediaRepository->findOneEnabledByCode($code, $localeCode, $channelCode)->willReturn(null);
$mediaRepository->findOneEnabledByCode($code, $channelCode)->willReturn(null);

$logger->warning(sprintf('Media with "%s" code was not found in the database.', $code))->shouldBeCalled();

Expand Down
6 changes: 3 additions & 3 deletions src/Entity/MediaTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class MediaTranslation extends AbstractTranslation implements MediaTranslationIn
{
protected ?int $id;

protected ?string $content;
protected ?string $content = null;

protected ?string $alt;
protected ?string $alt = null;

protected ?string $link;
protected ?string $link = null;

public function getId(): ?int
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
return '';
}

/** @var CollectionInterface $collection */
/** @var CollectionInterface|null $collection */
$collection = $this->collectionRepository->findOneBy(['code' => $code]);

return $this->twig->render('@BitBagSyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@BitBagSyliusCmsPlugin/Shop/ContentElement/_pages_collection.html.twig',
'collection' => $collection->getPages(),
'collection' => $collection?->getPages(),
]);
}
}
46 changes: 0 additions & 46 deletions src/Repository/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,20 @@ public function createListQueryBuilder(string $locale): QueryBuilder

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()
;
}

public function findByCollectionCode(
string $collectionCode,
string $localeCode,
string $channelCode,
): array {
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->innerJoin('o.collections', 'collection')
->innerJoin('o.channels', 'channels')
->andWhere('translation.locale = :localeCode')
->andWhere('collection.code = :collectionCode')
->andWhere('o.enabled = true')
->andWhere('channels.code = :channelCode')
->setParameter('localeCode', $localeCode)
->setParameter('collectionCode', $collectionCode)
->setParameter('channelCode', $channelCode)
->getQuery()
->getResult()
;
}

public function findByProductCode(
string $productCode,
string $localeCode,
string $channelCode,
): array {
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->innerJoin('o.products', 'product')
->innerJoin('o.channels', 'channels')
->andWhere('translation.locale = :localeCode')
->andWhere('product.code = :productCode')
->andWhere('o.enabled = true')
->andWhere('channels.code = :channelCode')
->setParameter('localeCode', $localeCode)
->setParameter('productCode', $productCode)
->setParameter('channelCode', $channelCode)
->getQuery()
->getResult()
;
}

public function findByNamePart(string $phrase, array $mediaType): array
{
return $this->createQueryBuilder('o')
Expand Down
13 changes: 0 additions & 13 deletions src/Repository/MediaRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,8 @@ public function createListQueryBuilder(string $locale): QueryBuilder;

public function findOneEnabledByCode(
string $code,
string $localeCode,
string $channelCode,
): ?MediaInterface;

public function findByCollectionCode(
string $collectionCode,
string $localeCode,
string $channelCode,
): array;

public function findByProductCode(
string $productCode,
string $localeCode,
string $channelCode,
): array;

public function findByNamePart(string $phrase, array $mediaType): array;
}
3 changes: 0 additions & 3 deletions src/Resolver/MediaResourceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
use Psr\Log\LoggerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Webmozart\Assert\Assert;

final class MediaResourceResolver implements MediaResourceResolverInterface
{
public function __construct(
private MediaRepositoryInterface $mediaRepository,
private LocaleContextInterface $localeContext,
private ChannelContextInterface $channelContext,
private LoggerInterface $logger,
) {
Expand All @@ -32,7 +30,6 @@ public function findOrLog(string $code): ?MediaInterface
Assert::notNull($this->channelContext->getChannel()->getCode());
$media = $this->mediaRepository->findOneEnabledByCode(
$code,
$this->localeContext->getLocaleCode(),
$this->channelContext->getChannel()->getCode(),
);

Expand Down
42 changes: 0 additions & 42 deletions src/Resources/config/routing/shop/media.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@ bitbag_sylius_cms_plugin_shop_media_render:
defaults:
_controller: bitbag_sylius_cms_plugin.controller.media.overriden::renderMediaAction

bitbag_sylius_cms_plugin_shop_media_render_template:
path: /media-template/{code}
methods: [GET]
defaults:
_controller: bitbag_sylius_cms_plugin.controller.media.overriden::showAction
_sylius:
template: $template
repository:
method: findOneEnabledByCode
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}
methods: [GET]
Expand All @@ -31,31 +17,3 @@ bitbag_sylius_cms_plugin_shop_media_inline:
defaults:
_controller: bitbag_sylius_cms_plugin.controller.media.overriden::downloadMediaAction
disposition: !php/const Symfony\Component\HttpFoundation\ResponseHeaderBag::DISPOSITION_INLINE

bitbag_sylius_cms_plugin_shop_media_index_by_collection_code:
path: /media/collection/{collectionCode}
methods: [GET]
defaults:
_controller: bitbag_sylius_cms_plugin.controller.media.overriden::indexAction
_sylius:
template: $template
repository:
method: findByCollectionCode
arguments:
- $collectionCode
- "expr:service('sylius.context.locale').getLocaleCode()"
- "expr:service('sylius.context.channel').getChannel().getCode()"

bitbag_sylius_cms_plugin_shop_media_index_by_product_code:
path: /media/product/{productCode}
methods: [GET]
defaults:
_controller: bitbag_sylius_cms_plugin.controller.media.overriden::indexAction
_sylius:
template: $template
repository:
method: findByProductCode
arguments:
- $productCode
- "expr:service('sylius.context.locale').getLocaleCode()"
- "expr:service('sylius.context.channel').getChannel().getCode()"
1 change: 0 additions & 1 deletion src/Resources/config/services/resolver.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@

<service id="bitbag_sylius_cms_plugin.resolver.media_resource" class="BitBag\SyliusCmsPlugin\Resolver\MediaResourceResolver" public="true">
<argument type="service" id="bitbag_sylius_cms_plugin.repository.media" />
<argument type="service" id="sylius.context.locale" />
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="logger" />
</service>
Expand Down
1 change: 0 additions & 1 deletion tests/Behat/Context/Transform/MediaContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function getMediaByCode(string $mediaCode): MediaInterface
{
$media = $this->mediaRepositoryInterface->findOneEnabledByCode(
$mediaCode,
$this->sharedStorage->get('locale')->getCode(),
$this->sharedStorage->get('channel')->getCode(),
);

Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Api/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function setUp(): void
public function test_media_response(): void
{
/** @var MediaInterface $media */
$media = $this->getRepository()->findOneEnabledByCode('media1-code', 'en_US', 'code');
$media = $this->getRepository()->findOneEnabledByCode('media1-code', 'code');

$this->client->request('GET', '/api/v2/shop/cms-plugin/media/' . $media->getId(), [], [], self::CONTENT_TYPE_HEADER);
$response = $this->client->getResponse();
Expand Down

This file was deleted.

Loading

0 comments on commit b26d99c

Please sign in to comment.