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

[General] Present admin data in the store #83

Merged
merged 3 commits into from
Nov 13, 2017
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
8 changes: 4 additions & 4 deletions spec/Twig/Extension/RenderBlockExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function it_adds_warning_for_not_found_block(
\Twig_Environment $twigEnvironment
): void
{
$blockRepository->findEnabledByCode('bitbag')->willReturn(null);
$blockRepository->findOneEnabledByCode('bitbag')->willReturn(null);
$logger->warning('Block with "bitbag" code was not found in the database.')->shouldBeCalled();

$this->renderBlock($twigEnvironment, 'bitbag')->shouldReturn(null);
Expand All @@ -69,7 +69,7 @@ function it_renders_text_template_for_text_type(
\Twig_Environment $twigEnvironment
): void
{
$blockRepository->findEnabledByCode('bitbag')->willReturn($block);
$blockRepository->findOneEnabledByCode('bitbag')->willReturn($block);
$block->getType()->willReturn('text');
$twigEnvironment->render('@BitBagCmsPlugin/Shop/Block/textBlock.html.twig', ['block' => $block])->shouldBeCalled();

Expand All @@ -82,7 +82,7 @@ function it_renders_html_template_for_html_type(
\Twig_Environment $twigEnvironment
): void
{
$blockRepository->findEnabledByCode('bitbag')->willReturn($block);
$blockRepository->findOneEnabledByCode('bitbag')->willReturn($block);
$block->getType()->willReturn('html');
$twigEnvironment->render('@BitBagCmsPlugin/Shop/Block/htmlBlock.html.twig', ['block' => $block])->shouldBeCalled();

Expand All @@ -95,7 +95,7 @@ function it_renders_image_template_for_image_type(
\Twig_Environment $twigEnvironment
): void
{
$blockRepository->findEnabledByCode('bitbag')->willReturn($block);
$blockRepository->findOneEnabledByCode('bitbag')->willReturn($block);
$block->getType()->willReturn('image');
$twigEnvironment->render('@BitBagCmsPlugin/Shop/Block/imageBlock.html.twig', ['block' => $block])->shouldBeCalled();

Expand Down
4 changes: 2 additions & 2 deletions spec/Twig/Extension/RenderPageLinkByCodeExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function it_renders_page_link(
\Twig_Environment $twigEnvironment
): void
{
$pageRepository->findEnabledByCode('bitbag')->willReturn($page);
$pageRepository->findOneEnabledByCode('bitbag')->willReturn($page);
$twigEnvironment->render('BitBagCmsPlugin:Shop:Page:_link.html.twig', ['page' => $page])->shouldBeCalled();

$this->renderPageLinkByCode($twigEnvironment, 'bitbag');
Expand All @@ -69,7 +69,7 @@ function it_adds_warning_for_not_found_page(
\Twig_Environment $twigEnvironment
): void
{
$pageRepository->findEnabledByCode('bitbag')->willReturn(null);
$pageRepository->findOneEnabledByCode('bitbag')->willReturn(null);
$logger->warning('Page with "bitbag" code was not found in the database.')->shouldBeCalled();

$this->renderPageLinkByCode($twigEnvironment, 'bitbag')->shouldReturn(null);
Expand Down
5 changes: 5 additions & 0 deletions src/Fixture/BlockFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->prototype('array')
->children()
->booleanNode('remove_existing')->defaultTrue()->end()
->integerNode('number')->defaultNull()->end()
->booleanNode('last_four_products')->defaultFalse()->end()
->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
->booleanNode('enabled')->defaultTrue()->end()
->integerNode('products')->defaultNull()->end()
->arrayNode('sections')
->prototype('scalar')->end()
->end()
->arrayNode('translations')
->prototype('array')
->children()
Expand Down
111 changes: 88 additions & 23 deletions src/Fixture/Factory/BlockFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@

namespace BitBag\CmsPlugin\Fixture\Factory;

use BitBag\CmsPlugin\Entity\BlockImage;
use BitBag\CmsPlugin\Entity\BlockInterface;
use BitBag\CmsPlugin\Entity\BlockTranslationInterface;
use BitBag\CmsPlugin\Entity\BlockImage;
use BitBag\CmsPlugin\Entity\SectionInterface;
use BitBag\CmsPlugin\Factory\BlockFactoryInterface;
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
use BitBag\CmsPlugin\Repository\SectionRepositoryInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand Down Expand Up @@ -46,23 +49,39 @@ final class BlockFixtureFactory implements FixtureFactoryInterface
*/
private $imageUploader;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @var SectionRepositoryInterface
*/
private $sectionRepository;

/**
* @param BlockFactoryInterface $blockFactory
* @param FactoryInterface $blockTranslationFactory
* @param BlockRepositoryInterface $blockRepository
* @param ImageUploaderInterface $imageUploader
* @param ProductRepositoryInterface $productRepository
* @param SectionRepositoryInterface $sectionRepository
*/
public function __construct(
BlockFactoryInterface $blockFactory,
FactoryInterface $blockTranslationFactory,
BlockRepositoryInterface $blockRepository,
ImageUploaderInterface $imageUploader
ImageUploaderInterface $imageUploader,
ProductRepositoryInterface $productRepository,
SectionRepositoryInterface $sectionRepository
)
{
$this->blockFactory = $blockFactory;
$this->blockTranslationFactory = $blockTranslationFactory;
$this->blockRepository = $blockRepository;
$this->imageUploader = $imageUploader;
$this->productRepository = $productRepository;
$this->sectionRepository = $sectionRepository;
}

/**
Expand All @@ -78,36 +97,82 @@ public function load(array $data): void
$this->blockRepository->remove($block);
}

$type = $fields['type'];
$block = $this->blockFactory->createWithType($type);
if (null !== $fields['number']) {
for ($i = 0; $i < $fields['number']; $i++) {
$this->createBlock(md5(uniqid()), $fields);
}
} else {
$this->createBlock($code, $fields);
}
}
}

private function createBlock(string $code, array $blockData): void
{
$type = $blockData['type'];
$block = $this->blockFactory->createWithType($type);
$products = $blockData['products'];

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

$block->setCode($code);
$block->setEnabled($fields['enabled']);
$this->resolveSections($block, $blockData['sections']);

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

$blockTranslation->setLocale($localeCode);
$blockTranslation->setName($translation['name']);
$blockTranslation->setContent($translation['content']);
$blockTranslation->setLink($translation['link']);
foreach ($blockData['translations'] as $localeCode => $translation) {
/** @var BlockTranslationInterface $blockTranslation */
$blockTranslation = $this->blockTranslationFactory->createNew();

if (BlockInterface::IMAGE_BLOCK_TYPE === $type) {
$image = new BlockImage();
$path = $translation['image_path'];
$uploadedImage = new UploadedFile($path, md5($path) . '.jpg');
$blockTranslation->setLocale($localeCode);
$blockTranslation->setName($translation['name']);
$blockTranslation->setContent($translation['content']);
$blockTranslation->setLink($translation['link']);

$image->setFile($uploadedImage);
$blockTranslation->setImage($image);
if (BlockInterface::IMAGE_BLOCK_TYPE === $type) {
$image = new BlockImage();
$path = $translation['image_path'];
$uploadedImage = new UploadedFile($path, md5($path) . '.jpg');

$this->imageUploader->upload($image);
}
$image->setFile($uploadedImage);
$blockTranslation->setImage($image);

$block->addTranslation($blockTranslation);
$this->imageUploader->upload($image);
}

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

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


/**
* @param BlockInterface $block
* @param int $limit
*/
private function resolveProducts(BlockInterface $block, int $limit): void
{
$products = $this->productRepository->findBy([], null, $limit);

foreach ($products as $product) {
$block->addProduct($product);
}
}

/**
* @param BlockInterface $block
* @param array $sections
*/
private function resolveSections(BlockInterface $block, array $sections): void
{
foreach ($sections as $sectionCode) {
/** @var SectionInterface $section */
$section = $this->sectionRepository->findOneBy(['code' => $sectionCode]);

$block->addSection($section);
}
}
}
10 changes: 5 additions & 5 deletions src/Fixture/Factory/FrequentlyAskedQuestionFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public function load(array $data): void

if (null !== $fields['number']) {
for ($i = 0; $i < $fields['number']; $i++) {
$this->createFrequentlyAskedQuestion(md5(uniqid()), $fields);
$this->createFrequentlyAskedQuestion(md5(uniqid()), $fields, ++$i);
}
} else {
$this->createFrequentlyAskedQuestion($code, $fields);
$this->createFrequentlyAskedQuestion($code, $fields, $fields['position']);
}
}
}
Expand All @@ -80,18 +80,18 @@ public function load(array $data): void
* @param string $code
* @param array $frequentlyAskedQuestionData
*/
private function createFrequentlyAskedQuestion(string $code, array $frequentlyAskedQuestionData): void
private function createFrequentlyAskedQuestion(string $code, array $frequentlyAskedQuestionData, int $position): void
{
/** @var FrequentlyAskedQuestionInterface $frequentlyAskedQuestion */
$frequentlyAskedQuestion = $this->frequentlyAskedQuestionFactory->createNew();

$frequentlyAskedQuestion->setCode($code);
$frequentlyAskedQuestion->setEnabled($frequentlyAskedQuestionData['enabled']);
$frequentlyAskedQuestion->setPosition($frequentlyAskedQuestionData['position']);
$frequentlyAskedQuestion->setPosition($position);

foreach ($frequentlyAskedQuestionData['translations'] as $localeCode => $translation) {
/** @var FrequentlyAskedQuestionTranslationInterface $frequentlyAskedQuestionTranslation */
$frequentlyAskedQuestionTranslation = $this->frequentlyAskedQuestionFactory->createNew();
$frequentlyAskedQuestionTranslation = $this->frequentlyAskedQuestionTranslationFactory->createNew();

$frequentlyAskedQuestionTranslation->setLocale($localeCode);
$frequentlyAskedQuestionTranslation->setQuestion($translation['question']);
Expand Down
47 changes: 18 additions & 29 deletions src/Repository/BlockRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ class BlockRepository extends EntityRepository implements BlockRepositoryInterfa
/**
* {@inheritdoc}
*/
public function createListQueryBuilder(string $locale): QueryBuilder
public function createListQueryBuilder(string $localeCode): QueryBuilder
{
return $this->createQueryBuilder('o')
->innerJoin('o.translations', 'translation')
->where('translation.locale = :locale')
->setParameter('locale', $locale)
->where('translation.locale = :localeCode')
->setParameter('localeCode', $localeCode)
;
}

/**
* {@inheritdoc}
*/
public function findEnabledByCode(string $code): ?BlockInterface
public function findOneEnabledByCode(string $code): ?BlockInterface
{
return $this->createQueryBuilder('o')
->where('o.code = :code')
Expand All @@ -51,47 +51,36 @@ public function findEnabledByCode(string $code): ?BlockInterface
/**
* {@inheritdoc}
*/
public function findEnabledByCodeAndContent(string $code, string $content): ?BlockInterface
public function findBySectionCode(string $sectionCode, string $localeCode): ?array
{
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->where('o.code = :code')
->innerJoin('o.sections', 'section')
->andWhere('translation.locale = :localeCode')
->andWhere('section.code = :sectionCode')
->andWhere('o.enabled = true')
->andWhere('translation.content = :content')
->setParameter('code', $code)
->setParameter('content', $content)
->setParameter('localeCode', $localeCode)
->setParameter('sectionCode', $sectionCode)
->getQuery()
->getOneOrNullResult()
->getResult()
;
}

/**
* {@inheritdoc}
*/
public function findOneByTypeAndContent(string $type, string $content): ?BlockInterface
public function findByProductCode(string $productCode, string $localeCode): ?array
{
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->where('o.type = :type')
->innerJoin('o.products', 'products')
->andWhere('translation.locale = :localeCode')
->andWhere('product.code = :productCode')
->andWhere('o.enabled = true')
->andWhere('translation.content = :content')
->setParameter('type', $type)
->setParameter('content', $content)
->setParameter('localeCode', $localeCode)
->setParameter('productCode', $productCode)
->getQuery()
->getOneOrNullResult()
;
}

/**
* {@inheritdoc}
*/
public function createShopListQueryBuilder(string $sectionCode): QueryBuilder
{
return $this->createQueryBuilder('o')
->innerJoin('o.sections', 'section')
->andWhere('section.code = :sectionCode')
->andWhere('o.enabled = true')
->setParameter('sectionCode', $sectionCode)
->getResult()
;
}
}
Loading