Skip to content

Commit

Permalink
OP-344: init
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly committed Aug 7, 2024
1 parent a360d8c commit 7a05e3e
Show file tree
Hide file tree
Showing 18 changed files with 437 additions and 180 deletions.
35 changes: 33 additions & 2 deletions src/Fixture/BlockFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,42 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->arrayPrototype()
->children()
->booleanNode('remove_existing')->defaultTrue()->end()
->integerNode('number')->defaultNull()->end()
->booleanNode('last_four_products')->defaultFalse()->end()
->scalarNode('name')->end()
->booleanNode('enabled')->defaultTrue()->end()
->arrayNode('collections')->scalarPrototype()->end()->end()
->arrayNode('channels')->scalarPrototype()->end()->end()
->arrayNode('locales')->scalarPrototype()->end()->end()
->arrayNode('products')->scalarPrototype()->end()->end()
->arrayNode('taxons')->scalarPrototype()->end()->end()
->arrayNode('products_in_taxons')->scalarPrototype()->end()->end()
->arrayNode('content_elements')
->prototype('array')
->children()
->scalarNode('heading_type')->end()
->scalarNode('heading')->end()
->scalarNode('textarea')->end()
->scalarNode('single_media')->end()
->scalarNode('products_carousel_by_taxon')->end()
->scalarNode('products_grid_by_taxon')->end()
->arrayNode('multiple_media')->scalarPrototype()->end()->end()
->arrayNode('products_grid')
->children()
->arrayNode('products')->scalarPrototype()->end()->end()
->end()
->end()
->arrayNode('products_carousel')
->children()
->arrayNode('products')->scalarPrototype()->end()->end()
->end()
->end()
->arrayNode('taxons_list')
->children()
->arrayNode('taxons')->scalarPrototype()->end()->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
10 changes: 3 additions & 7 deletions src/Fixture/CollectionFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,9 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->arrayPrototype()
->children()
->booleanNode('remove_existing')->defaultTrue()->end()
->arrayNode('translations')
->prototype('array')
->children()
->scalarNode('name')->defaultNull()->end()
->end()
->end()
->end()
->node('name', 'scalar')->end()
->node('type', 'scalar')->end()
->arrayNode('page_codes')->scalarPrototype()->end()
->end()
->end()
->end()
Expand Down
34 changes: 25 additions & 9 deletions src/Fixture/Factory/BlockFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

use BitBag\SyliusCmsPlugin\Assigner\ChannelsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\CollectionsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\LocalesAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\ProductsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\ProductsInTaxonsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\TaxonsAssignerInterface;
use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
use BitBag\SyliusCmsPlugin\Entity\ContentConfiguration;
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

Expand All @@ -23,6 +28,10 @@ public function __construct(
private BlockRepositoryInterface $blockRepository,
private CollectionsAssignerInterface $collectionsAssigner,
private ChannelsAssignerInterface $channelAssigner,
private LocalesAssignerInterface $localesAssigner,
private ProductsAssignerInterface $productsAssigner,
private TaxonsAssignerInterface $taxonsAssigner,
private ProductsInTaxonsAssignerInterface $productsInTaxonsAssigner,
) {
}

Expand All @@ -38,13 +47,7 @@ public function load(array $data): void
$this->blockRepository->remove($block);
}

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

Expand All @@ -53,11 +56,24 @@ private function createBlock(string $code, array $blockData): void
/** @var BlockInterface $block */
$block = $this->blockFactory->createNew();

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

$this->collectionsAssigner->assign($block, $blockData['collections']);
$this->channelAssigner->assign($block, $blockData['channels']);
$this->localesAssigner->assign($block, $blockData['locales']);
$this->productsAssigner->assign($block, $blockData['products']);
$this->taxonsAssigner->assign($block, $blockData['taxons']);
$this->productsInTaxonsAssigner->assign($block, $blockData['products_in_taxons']);

$block->setCode($code);
$block->setEnabled($blockData['enabled']);
foreach ($blockData['content_elements'] as $type => $data) {
$contentConfiguration = new ContentConfiguration();
$contentConfiguration->setType($type);
$contentConfiguration->setConfiguration($data);
$contentConfiguration->setBlock($block);
$block->addContentElement($contentConfiguration);
}

$this->blockRepository->add($block);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Fixture/Factory/CollectionFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
namespace BitBag\SyliusCmsPlugin\Fixture\Factory;

use BitBag\SyliusCmsPlugin\Entity\CollectionInterface;
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
use BitBag\SyliusCmsPlugin\Repository\CollectionRepositoryInterface;
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;

final class CollectionFixtureFactory implements FixtureFactoryInterface
{
public function __construct(
private FactoryInterface $collectionFactory,
private CollectionRepositoryInterface $collectionRepository,
private PageRepositoryInterface $pageRepository,
) {
}

Expand All @@ -37,6 +40,16 @@ public function load(array $data): void
/** @var CollectionInterface $collection */
$collection = $this->collectionFactory->createNew();
$collection->setCode($code);
$collection->setName($fields['name']);
$collection->setType($fields['type']);

foreach ($fields['page_codes'] as $pageCode) {
/** @var PageInterface $page */
$page = $this->pageRepository->findOneBy(['code' => $pageCode]);
if ($page) {

Check failure on line 49 in src/Fixture/Factory/CollectionFixtureFactory.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.1, Symfony ^6.4

If condition is always true.

Check failure on line 49 in src/Fixture/Factory/CollectionFixtureFactory.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.1, Symfony ^5.4

If condition is always true.
$collection->addPage($page);
}
}

$this->collectionRepository->add($collection);
}
Expand Down
8 changes: 1 addition & 7 deletions src/Fixture/Factory/MediaFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ public function load(array $data): void
$this->mediaRepository->remove($media);
}

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

Expand Down
28 changes: 16 additions & 12 deletions src/Fixture/Factory/PageFixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use BitBag\SyliusCmsPlugin\Assigner\ChannelsAssignerInterface;
use BitBag\SyliusCmsPlugin\Assigner\CollectionsAssignerInterface;
use BitBag\SyliusCmsPlugin\Entity\ContentConfiguration;
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface;
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface;
Expand Down Expand Up @@ -42,42 +43,45 @@ public function load(array $data): void
$this->pageRepository->remove($page);
}

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

private function createPage(
string $code,
array $pageData,
bool $generateSlug = false,
): void {
/** @var PageInterface $page */
$page = $this->pageFactory->createNew();
$channelsCodes = $pageData['channels'];

$this->collectionsAssigner->assign($page, $pageData['collections']);
$this->channelAssigner->assign($page, $channelsCodes);
$this->channelAssigner->assign($page, $pageData['channels']);

$page->setCode($code);
$page->setName($pageData['name']);
$page->setEnabled($pageData['enabled']);

foreach ($pageData['translations'] as $localeCode => $translation) {
/** @var PageTranslationInterface $pageTranslation */
$pageTranslation = $this->pageTranslationFactory->createNew();
$slug = true === $generateSlug ? md5(uniqid()) : $translation['slug'];
$slug = $translation['slug'] ?? md5(uniqid('', true));

$pageTranslation->setLocale($localeCode);
$pageTranslation->setSlug($slug);
$pageTranslation->setTitle($translation['meta_title']);
$pageTranslation->setMetaKeywords($translation['meta_keywords']);
$pageTranslation->setMetaDescription($translation['meta_description']);

$page->addTranslation($pageTranslation);
$page->setName($translation['name']);
}

foreach ($pageData['content_elements'] as $data) {
dd($data);
$contentConfiguration = new ContentConfiguration();

Check failure on line 80 in src/Fixture/Factory/PageFixtureFactory.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.1, Symfony ^6.4

Unreachable statement - code above always terminates.

Check failure on line 80 in src/Fixture/Factory/PageFixtureFactory.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.13.0, PHP 8.1, Symfony ^5.4

Unreachable statement - code above always terminates.
$contentConfiguration->setType($data['type']);
$contentConfiguration->setConfiguration($data['data']);
$contentConfiguration->setPage($page);
$page->addContentElement($contentConfiguration);
}

$this->pageRepository->add($page);
Expand Down
51 changes: 51 additions & 0 deletions src/Fixture/Factory/TemplateFixtureFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Fixture\Factory;

use BitBag\SyliusCmsPlugin\Entity\TemplateInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class TemplateFixtureFactory implements FixtureFactoryInterface
{
public function __construct(
private FactoryInterface $templateFactory,
private RepositoryInterface $templateRepository,
) {
}

public function load(array $data): void
{
foreach ($data as $fields) {
/** @var ?TemplateInterface $template */
$template = $this->templateRepository->findOneBy(['name' => $fields['name']]);
if (
true === $fields['remove_existing'] &&
null !== $template
) {
$this->templateRepository->remove($template);
}

$this->createPage($fields);
}
}

private function createPage(array $pageData): void {
/** @var TemplateInterface $template */
$template = $this->templateFactory->createNew();

$template->setName($pageData['name']);
$template->setType($pageData['type']);
$template->setContentElements($pageData['content_elements']);

$this->templateRepository->add($template);
}
}
2 changes: 0 additions & 2 deletions src/Fixture/MediaFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->arrayPrototype()
->children()
->booleanNode('remove_existing')->defaultTrue()->end()
->integerNode('number')->defaultNull()->end()
->scalarNode('type')->isRequired()->cannotBeEmpty()->end()
->scalarNode('path')->isRequired()->cannotBeEmpty()->end()
->scalarNode('original_name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('name')->defaultNull()->end()
->booleanNode('enabled')->defaultTrue()->end()
->arrayNode('productCodes')->scalarPrototype()->end()->end()
->arrayNode('collections')->scalarPrototype()->end()->end()
->arrayNode('channels')->scalarPrototype()->end()->end()
->arrayNode('translations')
Expand Down
44 changes: 36 additions & 8 deletions src/Fixture/PageFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,54 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
->arrayPrototype()
->children()
->booleanNode('remove_existing')->defaultTrue()->end()
->integerNode('number')->defaultNull()->end()
->booleanNode('enabled')->defaultTrue()->end()
->integerNode('products')->defaultNull()->end()
->arrayNode('productCodes')->scalarPrototype()->end()->end()
->scalarNode('name')->end()
->arrayNode('collections')->scalarPrototype()->end()->end()
->arrayNode('channels')->scalarPrototype()->end()->end()
->arrayNode('translations')
->prototype('array')
->children()
->scalarNode('slug')->defaultNull()->end()
->scalarNode('name')->defaultNull()->end()
->scalarNode('name_when_linked')->defaultNull()->end()
->scalarNode('description_when_linked')->defaultNull()->end()
->scalarNode('image_path')->defaultNull()->end()
->scalarNode('meta_title')->defaultNull()->end()
->scalarNode('meta_keywords')->defaultNull()->end()
->scalarNode('meta_description')->defaultNull()->end()
->scalarNode('content')->defaultNull()->end()
->end()
->end()
->end()
->arrayNode('content_elements')
->useAttributeAsKey('key') // Use keys from YAML as node names
->arrayPrototype()
->children()
->scalarNode('type')->end() // "type" field at the top level of each content element
->arrayNode('data') // "data" field containing the actual data
->children()
->scalarNode('heading_type')->end()
->scalarNode('heading')->end()
->scalarNode('textarea')->end()
->scalarNode('single_media')->end()
->scalarNode('products_carousel_by_taxon')->end()
->scalarNode('products_grid_by_taxon')->end()
->arrayNode('multiple_media')->scalarPrototype()->end()->end()
->arrayNode('products_grid')
->children()
->arrayNode('products')->scalarPrototype()->end()->end()
->end()
->end()
->arrayNode('products_carousel')
->children()
->arrayNode('products')->scalarPrototype()->end()->end()
->end()
->end()
->arrayNode('taxons_list')
->children()
->arrayNode('taxons')->scalarPrototype()->end()->end()
->end()
->end()
->end()
->end() // End of data
->end()
->end() // End of each content element
->end() // End of content_elements array
->end()
->end()
->end()
Expand Down
Loading

0 comments on commit 7a05e3e

Please sign in to comment.