Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add channel awareness #142

Merged
merged 5 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions spec/Resolver/BlockResourceResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
use BitBag\SyliusCmsPlugin\Resolver\BlockResourceResolverInterface;
use PhpSpec\ObjectBehavior;
use Psr\Log\LoggerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;

final class BlockResourceResolverSpec extends ObjectBehavior
{
function let(BlockRepositoryInterface $blockRepository, LoggerInterface $logger)
function let(BlockRepositoryInterface $blockRepository, LoggerInterface $logger, ChannelContextInterface $channelContext)
{
$this->beConstructedWith($blockRepository, $logger);
$this->beConstructedWith($blockRepository, $logger, $channelContext);
}

function it_is_initializable(): void
Expand All @@ -36,9 +38,15 @@ function it_implements_block_resource_resolver_interface(): void
$this->shouldHaveType(BlockResourceResolverInterface::class);
}

function it_logs_warning_if_block_was_not_found(BlockRepositoryInterface $blockRepository, LoggerInterface $logger)
{
$blockRepository->findOneEnabledByCode('homepage_banner')->willReturn(null);
function it_logs_warning_if_block_was_not_found(
BlockRepositoryInterface $blockRepository,
LoggerInterface $logger,
ChannelContextInterface $channelContext,
ChannelInterface $channel
) {
$channel->getCode()->willReturn('WEB');
$channelContext->getChannel()->willReturn($channel);
$blockRepository->findOneEnabledByCodeAndChannelCode('homepage_banner', 'WEB')->willReturn(null);

$logger
->warning(sprintf(
Expand All @@ -51,9 +59,15 @@ function it_logs_warning_if_block_was_not_found(BlockRepositoryInterface $blockR
$this->findOrLog('homepage_banner');
}

function it_returns_block_if_found_in_database(BlockRepositoryInterface $blockRepository, BlockInterface $block)
{
$blockRepository->findOneEnabledByCode('homepage_banner')->willReturn($block);
function it_returns_block_if_found_in_database(
BlockRepositoryInterface $blockRepository,
BlockInterface $block,
ChannelContextInterface $channelContext,
ChannelInterface $channel
) {
$channel->getCode()->willReturn('WEB');
$channelContext->getChannel()->willReturn($channel);
$blockRepository->findOneEnabledByCodeAndChannelCode('homepage_banner', 'WEB')->willReturn($block);

$this->findOrLog('homepage_banner')->shouldReturn($block);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Block implements BlockInterface
use ToggleableTrait;
use SectionableTrait;
use ProductsAwareTrait;
use ChannelsAwareTrait;
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
}
Expand All @@ -31,6 +32,7 @@ public function __construct()
$this->initializeTranslationsCollection();
$this->initializeSectionsCollection();
$this->initializeProductsCollection();
$this->initializeChannelsCollection();
}

/** @var int */
Expand Down
4 changes: 3 additions & 1 deletion src/Entity/BlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace BitBag\SyliusCmsPlugin\Entity;

use Sylius\Component\Channel\Model\ChannelsAwareInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\ToggleableInterface;
Expand All @@ -22,7 +23,8 @@ interface BlockInterface extends
TranslatableInterface,
ToggleableInterface,
ProductsAwareInterface,
SectionableInterface
SectionableInterface,
ChannelsAwareInterface
{
const TEXT_BLOCK_TYPE = 'text';
const IMAGE_BLOCK_TYPE = 'image';
Expand Down
52 changes: 52 additions & 0 deletions src/Entity/ChannelsAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* another great project.
* You can find more information about us on https://bitbag.shop and write us
* an email on [email protected].
*/

namespace BitBag\SyliusCmsPlugin\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Channel\Model\ChannelInterface;

trait ChannelsAwareTrait
{
/** @var Collection|ChannelInterface[] */
protected $channels;

public function initializeChannelsCollection(): void
{
$this->channels = new ArrayCollection();
}

public function getChannels(): Collection
{
return $this->channels;
}

public function addChannel(ChannelInterface $channel): void
{
if (!$this->hasChannel($channel)) {
$this->channels->add($channel);
}
}

public function removeChannel(ChannelInterface $channel): void
{
if ($this->hasChannel($channel)) {
$this->channels->removeElement($channel);
}
}

public function hasChannel(ChannelInterface $channel): bool
{
return $this->channels->contains($channel);
}
}
2 changes: 2 additions & 0 deletions src/Entity/FrequentlyAskedQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

class FrequentlyAskedQuestion implements FrequentlyAskedQuestionInterface
{
use ChannelsAwareTrait;
use ToggleableTrait,
TranslatableTrait {
__construct as private initializeTranslationsCollection;
Expand All @@ -35,6 +36,7 @@ class FrequentlyAskedQuestion implements FrequentlyAskedQuestionInterface
public function __construct()
{
$this->initializeTranslationsCollection();
$this->initializeChannelsCollection();
}

public function getId(): ?int
Expand Down
7 changes: 6 additions & 1 deletion src/Entity/FrequentlyAskedQuestionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@

namespace BitBag\SyliusCmsPlugin\Entity;

use Sylius\Component\Channel\Model\ChannelsAwareInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\ToggleableInterface;
use Sylius\Component\Resource\Model\TranslatableInterface;

interface FrequentlyAskedQuestionInterface extends ResourceInterface, TranslatableInterface, ToggleableInterface
interface FrequentlyAskedQuestionInterface extends
ResourceInterface,
TranslatableInterface,
ToggleableInterface,
ChannelsAwareInterface
{
public function getCode(): ?string;

Expand Down
3 changes: 3 additions & 0 deletions src/Entity/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Page implements PageInterface
use ProductsAwareTrait;
use SectionableTrait;
use TimestampableTrait;
use ChannelsAwareTrait;
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
}
Expand All @@ -39,6 +40,8 @@ public function __construct()
$this->initializeProductsCollection();
$this->initializeSectionsCollection();
$this->initializeTranslationsCollection();
$this->initializeChannelsCollection();
$this->initializeChannelsCollection();

$this->createdAt = new \DateTime();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Entity/PageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace BitBag\SyliusCmsPlugin\Entity;

use Sylius\Component\Channel\Model\ChannelsAwareInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\TimestampableInterface;
use Sylius\Component\Resource\Model\ToggleableInterface;
Expand All @@ -23,7 +24,8 @@ interface PageInterface extends
ToggleableInterface,
ProductsAwareInterface,
SectionableInterface,
TimestampableInterface
TimestampableInterface,
ChannelsAwareInterface
{
public function getSlug(): ?string;

Expand Down
2 changes: 2 additions & 0 deletions src/Entity/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class Section implements SectionInterface
{
use ChannelsAwareTrait;
use TranslatableTrait {
__construct as private initializeTranslationsCollection;
}
Expand All @@ -30,6 +31,7 @@ class Section implements SectionInterface
public function __construct()
{
$this->initializeTranslationsCollection();
$this->initializeChannelsCollection();
}

public function getId(): ?int
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/SectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

namespace BitBag\SyliusCmsPlugin\Entity;

use Sylius\Component\Channel\Model\ChannelsAwareInterface;
use Sylius\Component\Resource\Model\ResourceInterface;
use Sylius\Component\Resource\Model\TranslatableInterface;

interface SectionInterface extends ResourceInterface, TranslatableInterface
interface SectionInterface extends ResourceInterface, TranslatableInterface, ChannelsAwareInterface
{
public function getCode(): ?string;

Expand Down
6 changes: 6 additions & 0 deletions src/Form/Type/BlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use BitBag\SyliusCmsPlugin\Form\Type\Translation\HtmlBlockTranslationType;
use BitBag\SyliusCmsPlugin\Form\Type\Translation\ImageBlockTranslationType;
use BitBag\SyliusCmsPlugin\Form\Type\Translation\TextBlockTranslationType;
use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType;
use Sylius\Bundle\ProductBundle\Form\Type\ProductAutocompleteChoiceType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
Expand Down Expand Up @@ -47,6 +48,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'bitbag_sylius_cms_plugin.ui.products',
'multiple' => true,
])
->add('channels', ChannelChoiceType::class, [
'multiple' => true,
'expanded' => true,
'label' => 'bitbag_sylius_cms_plugin.ui.channels',
])
;

$this->resolveBlockType($block, $builder);
Expand Down
6 changes: 6 additions & 0 deletions src/Form/Type/FrequentlyAskedQuestionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace BitBag\SyliusCmsPlugin\Form\Type;

use BitBag\SyliusCmsPlugin\Form\Type\Translation\FrequentlyAskedQuestionTranslationType;
use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Expand All @@ -39,6 +40,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label' => false,
'entry_type' => FrequentlyAskedQuestionTranslationType::class,
])
->add('channels', ChannelChoiceType::class, [
'multiple' => true,
'expanded' => true,
'label' => 'bitbag_sylius_cms_plugin.ui.channels',
])
;
}
}
6 changes: 6 additions & 0 deletions src/Form/Type/PageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace BitBag\SyliusCmsPlugin\Form\Type;

use BitBag\SyliusCmsPlugin\Form\Type\Translation\PageTranslationType;
use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType;
use Sylius\Bundle\ProductBundle\Form\Type\ProductAutocompleteChoiceType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
Expand Down Expand Up @@ -44,6 +45,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'bitbag_sylius_cms_plugin.ui.sections',
'multiple' => true,
])
->add('channels', ChannelChoiceType::class, [
'multiple' => true,
'expanded' => true,
'label' => 'bitbag_sylius_cms_plugin.ui.channels',
])
;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Form/Type/SectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace BitBag\SyliusCmsPlugin\Form\Type;

use BitBag\SyliusCmsPlugin\Form\Type\Translation\SectionTranslationType;
use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
Expand All @@ -30,6 +31,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('translations', ResourceTranslationsType::class, [
'entry_type' => SectionTranslationType::class,
])
->add('channels', ChannelChoiceType::class, [
'multiple' => true,
'expanded' => true,
'label' => 'bitbag_sylius_cms_plugin.ui.channels',
])
;
}

Expand Down
25 changes: 20 additions & 5 deletions src/Repository/BlockRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,57 @@ public function createListQueryBuilder(string $localeCode): QueryBuilder
;
}

public function findOneEnabledByCode(string $code): ?BlockInterface
public function findOneEnabledByCodeAndChannelCode(string $code, string $channelCode): ?BlockInterface
{
return $this->createQueryBuilder('o')
->innerJoin('o.channels', 'channels')
->where('o.code = :code')
->andWhere('o.enabled = true')
->andWhere('channels.code = :channelCode')
->setParameter('code', $code)
->setParameter('channelCode', $channelCode)
->getQuery()
->getOneOrNullResult()
;
}

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

public function findByProductCode(string $productCode, string $localeCode): array
{
public function findByProductCodeAndChannelCode(
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()
;
Expand Down
Loading