Skip to content

Commit

Permalink
OP-321: Update block functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly committed Jun 25, 2024
1 parent ee7a3e4 commit ae0f031
Show file tree
Hide file tree
Showing 34 changed files with 190 additions and 408 deletions.
4 changes: 0 additions & 4 deletions src/Controller/BlockController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ public function previewAction(Request $request): Response

/** @var BlockInterface $block */
$block = $form->getData();
$defaultLocale = $this->getParameter('locale');

$block->setFallbackLocale($request->get('_locale', $defaultLocale));
$block->setCurrentLocale($request->get('_locale', $defaultLocale));

if (!$configuration->isHtmlRequest()) {
Assert::true(null !== $this->viewHandler);
Expand Down
57 changes: 3 additions & 54 deletions src/Entity/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,26 @@
namespace BitBag\SyliusCmsPlugin\Entity;

use Sylius\Component\Resource\Model\ToggleableTrait;
use Sylius\Component\Resource\Model\TranslatableTrait;
use Sylius\Component\Resource\Model\TranslationInterface;

class Block implements BlockInterface
{
use ToggleableTrait;
use CollectionableTrait;
use ProductsAwareTrait;
use TaxonAwareTrait;
use ChannelsAwareTrait;
use BlockContentAwareTrait;
use TranslatableTrait {
__construct as protected initializeTranslationsCollection;
}
use LocaleAwareTrait;

public function __construct()
{
$this->initializeTranslationsCollection();
$this->initializeCollectionsCollection();
$this->initializeProductsCollection();
$this->initializeTaxonCollection();
$this->initializeChannelsCollection();
$this->initializeContentsCollection();
$this->initializeLocalesCollection();
}

protected ?int $id;

protected ?string $code;
protected ?string $code = null;

protected ?string $name;

Expand Down Expand Up @@ -66,47 +58,4 @@ public function setName(?string $name): void
{
$this->name = $name;
}

public function getContent(): ?string
{
/** @var BlockTranslationInterface $blockTranslationInterface */
$blockTranslationInterface = $this->getBlockTranslation();

return $blockTranslationInterface->getContent();
}

public function setContent(?string $content): void
{
/** @var BlockTranslationInterface $blockTranslationInterface */
$blockTranslationInterface = $this->getBlockTranslation();
$blockTranslationInterface->setContent($content);
}

public function getLink(): ?string
{
/** @var BlockTranslationInterface $blockTranslationInterface */
$blockTranslationInterface = $this->getBlockTranslation();

return $blockTranslationInterface->getLink();
}

public function setLink(?string $link): void
{
/** @var BlockTranslationInterface $blockTranslationInterface */
$blockTranslationInterface = $this->getBlockTranslation();
$blockTranslationInterface->setLink($link);
}

/**
* @return BlockTranslationInterface|TranslationInterface
*/
protected function getBlockTranslation(): TranslationInterface
{
return $this->getTranslation();
}

protected function createTranslation(): BlockTranslationInterface
{
return new BlockTranslation();
}
}
2 changes: 1 addition & 1 deletion src/Entity/BlockContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class BlockContent implements BlockContentInterface

protected array $configuration = [];

protected ?BlockInterface $block;
protected ?BlockInterface $block = null;

public function getId(): ?int
{
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/BlockContentAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function hasContent(BlockContentInterface $contentItem): bool
public function addContent(BlockContentInterface $contentItem): void
{
if (!$this->hasContent($contentItem)) {
$contentItem->setBlock($this);
$this->contents->add($contentItem);
}
}
Expand All @@ -43,6 +44,7 @@ public function removeContent(BlockContentInterface $contentItem): void
{
if ($this->hasContent($contentItem)) {
$this->contents->removeElement($contentItem);
$contentItem->setBlock(null);
}
}
}
16 changes: 2 additions & 14 deletions src/Entity/BlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
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 BlockInterface extends
ResourceInterface,
TranslatableInterface,
ToggleableInterface,
ProductsAwareInterface,
TaxonAwareInterface,
CollectionableInterface,
ChannelsAwareInterface,
ContentableInterface,
BlockContentAwareInterface
BlockContentAwareInterface,
LocaleAwareInterface
{
public function getCode(): ?string;

Expand All @@ -33,12 +29,4 @@ public function setCode(?string $code): void;
public function getName(): ?string;

public function setName(?string $name): void;

public function getContent(): ?string;

public function setContent(?string $content): void;

public function getLink(): ?string;

public function setLink(?string $link): void;
}
37 changes: 0 additions & 37 deletions src/Entity/BlockTranslation.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Entity/LocaleAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/


declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Entity;

use Doctrine\Common\Collections\Collection;
use Sylius\Component\Locale\Model\LocaleInterface;

interface LocaleAwareInterface
{
public function initializeLocalesCollection(): void;

public function getLocales(): Collection;

public function hasLocale(LocaleInterface $locale): bool;

public function addLocale(LocaleInterface $locale): void;

public function removeLocale(LocaleInterface $locale): void;
}
50 changes: 50 additions & 0 deletions src/Entity/LocaleAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/


declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Locale\Model\LocaleInterface;

trait LocaleAwareTrait
{
protected Collection $locales;

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

public function getLocales(): Collection
{
return $this->locales;
}

public function hasLocale(LocaleInterface $locale): bool
{
return $this->locales->contains($locale);
}

public function addLocale(LocaleInterface $locale): void
{
if (!$this->hasLocale($locale)) {
$this->locales->add($locale);
}
}

public function removeLocale(LocaleInterface $locale): void
{
if ($this->hasLocale($locale)) {
$this->locales->removeElement($locale);
}
}
}
13 changes: 0 additions & 13 deletions src/Fixture/BlockFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,8 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->integerNode('number')->defaultNull()->end()
->booleanNode('last_four_products')->defaultFalse()->end()
->booleanNode('enabled')->defaultTrue()->end()
->integerNode('products')->defaultNull()->end()
->arrayNode('productCodes')->scalarPrototype()->end()->end()
->arrayNode('taxons')->scalarPrototype()->end()->end()
->arrayNode('collections')->scalarPrototype()->end()->end()
->arrayNode('channels')->scalarPrototype()->end()->end()
->arrayNode('translations')
->arrayPrototype()
->children()
->scalarNode('name')->defaultNull()->end()
->scalarNode('content')->defaultNull()->end()
->scalarNode('link')->defaultNull()->end()
->scalarNode('image_path')->defaultNull()->end()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
45 changes: 0 additions & 45 deletions src/Fixture/Factory/BlockFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,15 @@

use BitBag\SyliusCmsPlugin\Assigner\ChannelsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\CollectionsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\ProductsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\TaxonsAssignerInterface;
use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
use BitBag\SyliusCmsPlugin\Entity\BlockTranslationInterface;
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class BlockFixtureFactory implements FixtureFactoryInterface
{
public function __construct(
private FactoryInterface $blockFactory,
private FactoryInterface $blockTranslationFactory,
private BlockRepositoryInterface $blockRepository,
private ProductRepositoryInterface $productRepository,
private ChannelContextInterface $channelContext,
private LocaleContextInterface $localeContext,
private ProductsAssignerInterface $productsAssigner,
private TaxonsAssignerInterface $taxonsAssigner,
private CollectionsAssignerInterface $collectionsAssigner,
private ChannelsAssignerInterface $channelAssigner,
) {
Expand Down Expand Up @@ -66,44 +53,12 @@ private function createBlock(string $code, array $blockData): void
/** @var BlockInterface $block */
$block = $this->blockFactory->createNew();

$products = $blockData['products'];
if (null !== $products) {
$this->resolveProducts($block, $products);
}

$this->collectionsAssigner->assign($block, $blockData['collections']);
$this->productsAssigner->assign($block, $blockData['productCodes']);
$this->taxonsAssigner->assign($block, $blockData['taxons']);
$this->channelAssigner->assign($block, $blockData['channels']);

$block->setCode($code);
$block->setEnabled($blockData['enabled']);

foreach ($blockData['translations'] as $localeCode => $translation) {
/** @var BlockTranslationInterface $blockTranslation */
$blockTranslation = $this->blockTranslationFactory->createNew();

$blockTranslation->setLocale($localeCode);
$blockTranslation->setName($translation['name']);
$blockTranslation->setContent($translation['content']);
$blockTranslation->setLink($translation['link']);
$block->addTranslation($blockTranslation);
}

$this->blockRepository->add($block);
}

private function resolveProducts(BlockInterface $block, int $limit): void
{
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
$products = $this->productRepository->findLatestByChannel(
$channel,
$this->localeContext->getLocaleCode(),
$limit,
);
foreach ($products as $product) {
$block->addProduct($product);
}
}
}
Loading

0 comments on commit ae0f031

Please sign in to comment.