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

OP-464: Block and page import adjustments #517

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions doc/block.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
code,type,name_en_US,content_en_US,sections,channels,products,image_en_US,slug_en_US
test4,image,Test,test,"blog, general",US_WEB,"010ba66b-adee-3d6e-9d63-67c44d686db1, 01d35db9-247d-3834-b300-20483d5e34e8",https://bitbag.shop/assets/web/images/header-logo.png,https://bitbag.shop/assets/web/images/header-logo.png
code,name,enabled,collections,locales,channels,products,products_in_taxons,taxons
test4,Block name,1,"blog,general",,FASHION_WEB,"000F_office_grey_jeans, 007M_black_elegance_jeans","caps, t_shirts",t_shirts
4 changes: 2 additions & 2 deletions doc/page.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
code,sections,channels,products,slug_en_US,name_en_US,image_en_US,meta_keywords_en_US,meta_description_en_US,content_en_US,breadcrumb_en_US,name_when_linked_en_US,description_when_linked_en_US
aboutUS,,US_WEB,,about_us,About US,,About US,About US,"",,,
code,name,enabled,collections,channels,slug_en_US,meta_title_en_US,meta_keywords_en_US,meta_description_en_US,slug_de_DE,meta_title_de_DE,meta_keywords_de_DE,meta_description_de_DE
aboutUS,About US,1,,FASHION_WEB,about_us,Meta title en US,About US,About US,about_us,Meta title en DE,About US,About US
53 changes: 39 additions & 14 deletions spec/Importer/BlockImporterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
namespace spec\BitBag\SyliusCmsPlugin\Importer;

use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
use BitBag\SyliusCmsPlugin\Importer\BlockImporter;
use BitBag\SyliusCmsPlugin\Importer\BlockImporterInterface;
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterChannelsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterLocalesResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsInTaxonsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterCollectionsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterTaxonsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ResourceResolverInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\Validator\ConstraintViolationList;
Expand All @@ -23,43 +28,63 @@
final class BlockImporterSpec extends ObjectBehavior
{
public function let(
ResourceResolverInterface $blockResourceResolver,
ResourceResolverInterface $blockResourceResolver,
ImporterCollectionsResolverInterface $importerCollectionsResolver,
ImporterChannelsResolverInterface $importerChannelsResolver,
ValidatorInterface $validator,
BlockRepositoryInterface $blockRepository
) {
ImporterChannelsResolverInterface $importerChannelsResolver,
ImporterLocalesResolverInterface $importerLocalesResolver,
ImporterProductsResolverInterface $importerProductsResolver,
ImporterTaxonsResolverInterface $importerTaxonsResolver,
ImporterProductsInTaxonsResolverInterface $importerProductsInTaxonsResolver,
ValidatorInterface $validator,
BlockRepositoryInterface $blockRepository
)
{
$this->beConstructedWith(
$blockResourceResolver,
$importerCollectionsResolver,
$importerChannelsResolver,
$importerLocalesResolver,
$importerProductsResolver,
$importerTaxonsResolver,
$importerProductsInTaxonsResolver,
$validator,
$blockRepository
);
}

public function it_is_initializable()
{
$this->shouldHaveType(\BitBag\SyliusCmsPlugin\Importer\BlockImporter::class);
$this->shouldImplement(\BitBag\SyliusCmsPlugin\Importer\BlockImporterInterface::class);
$this->shouldHaveType(BlockImporter::class);
$this->shouldImplement(BlockImporterInterface::class);
}

public function it_imports_block(
ResourceResolverInterface $blockResourceResolver,
ResourceResolverInterface $blockResourceResolver,
ImporterCollectionsResolverInterface $importerCollectionsResolver,
ImporterChannelsResolverInterface $importerChannelsResolver,
ValidatorInterface $validator,
BlockRepositoryInterface $blockRepository,
BlockInterface $block
) {
$row = ['name_pl' => 'name', 'code' => 'block_code'];
ImporterChannelsResolverInterface $importerChannelsResolver,
ImporterLocalesResolverInterface $importerLocalesResolver,
ImporterProductsResolverInterface $importerProductsResolver,
ImporterTaxonsResolverInterface $importerTaxonsResolver,
ImporterProductsInTaxonsResolverInterface $importerProductsInTaxonsResolver,
ValidatorInterface $validator,
BlockRepositoryInterface $blockRepository,
BlockInterface $block
)
{
$row = ['name' => 'block_name', 'code' => 'block_code', 'enabled' => '1'];

$blockResourceResolver->getResource('block_code')->willReturn($block);

$block->setCode('block_code')->shouldBeCalled();
$block->setName('block_name')->shouldBeCalled();
$block->setEnabled(true)->shouldBeCalled();

$importerCollectionsResolver->resolve($block, null)->shouldBeCalled();
$importerChannelsResolver->resolve($block, null)->shouldBeCalled();
$importerLocalesResolver->resolve($block, null)->shouldBeCalled();
$importerProductsResolver->resolve($block, null)->shouldBeCalled();
$importerTaxonsResolver->resolve($block, null)->shouldBeCalled();
$importerProductsInTaxonsResolver->resolve($block, null)->shouldBeCalled();

$validator->validate($block, null, ['bitbag'])->willReturn(new ConstraintViolationList());

Expand Down
19 changes: 8 additions & 11 deletions spec/Importer/PageImporterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,29 @@ public function it_imports_page_no_url(
{
$row = [
'code' => 'page_code',
'name' => 'page_name',
'enabled' => '1',
'slug_pl' => 'slug',
'name_pl' => 'name',
'image_pl' => null,
'imagecode_pl' => 'imagecode',
'metakeywords_pl' => 'metakeywords',
'metadescription_pl' => 'metadescription',
'content_pl' => 'content',
'breadcrumb_pl' => 'breadcrumb',
'namewhenlinked_pl' => 'namewhenlinked',
'descriptionwhenlinked_pl' => 'descriptionwhenlinked',
'meta_title_pl' => 'metatitle',
'meta_keywords_pl' => 'metakeywords',
'meta_description_pl' => 'metadescription',
'collections' => 'collections',
'channels' => 'channels',
'products' => 'products',
];

$pageResourceResolver->getResource('page_code')->willReturn($page);

$localeContext->getLocaleCode()->willReturn('en_US');

$page->setCode('page_code')->shouldBeCalled();
$page->setName('page_name')->shouldBeCalled();
$page->setEnabled(true)->shouldBeCalled();
$page->setFallbackLocale('en_US')->shouldBeCalled();

$page->setCurrentLocale('pl')->shouldBeCalled();

$page->setSlug('slug')->shouldBeCalled();
$page->setName('name')->shouldBeCalled();
$page->setTitle('metatitle')->shouldBeCalled();
$page->setMetaKeywords('metakeywords')->shouldBeCalled();
$page->setMetaDescription('metadescription')->shouldBeCalled();

Expand Down
34 changes: 34 additions & 0 deletions src/Assigner/LocalesAssigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Assigner;

use BitBag\SyliusCmsPlugin\Entity\LocaleAwareInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class LocalesAssigner implements LocalesAssignerInterface
{
public function __construct(private RepositoryInterface $localeRepository)
{
}

public function assign(LocaleAwareInterface $localesAware, array $localesCodes): void
jkindly marked this conversation as resolved.
Show resolved Hide resolved
{
$locales = $this->localeRepository->findAll();

/** @var LocaleInterface $locale */
foreach ($locales as $locale) {
if (in_array($locale->getCode(), $localesCodes, true)) {
$localesAware->addLocale($locale);
}
}
}
}
18 changes: 18 additions & 0 deletions src/Assigner/LocalesAssignerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\Assigner;

use BitBag\SyliusCmsPlugin\Entity\LocaleAwareInterface;

interface LocalesAssignerInterface
{
public function assign(LocaleAwareInterface $localesAware, array $localesCodes): void;
}
34 changes: 34 additions & 0 deletions src/Assigner/ProductsInTaxonsAssigner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Assigner;

use BitBag\SyliusCmsPlugin\Entity\ProductsInTaxonsAwareInterface;
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
use Webmozart\Assert\Assert;

final class ProductsInTaxonsAssigner implements ProductsInTaxonsAssignerInterface
{
public function __construct(private TaxonRepositoryInterface $taxonRepository)
{
}

public function assign(ProductsInTaxonsAwareInterface $productsInTaxonsAware, array $taxonCodes): void
jkindly marked this conversation as resolved.
Show resolved Hide resolved
{
foreach ($taxonCodes as $taxonCode) {
/** @var TaxonInterface|null $taxon */
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]);

Assert::notNull($taxon, sprintf('Taxon with %s code not found.', $taxonCode));
$productsInTaxonsAware->addProductsInTaxon($taxon);
}
}
}
18 changes: 18 additions & 0 deletions src/Assigner/ProductsInTaxonsAssignerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\Assigner;

use BitBag\SyliusCmsPlugin\Entity\ProductsInTaxonsAwareInterface;

interface ProductsInTaxonsAssignerInterface
{
public function assign(ProductsInTaxonsAwareInterface $productsInTaxonsAware, array $taxonCodes): void;
}
8 changes: 7 additions & 1 deletion src/Command/ImportFromCsvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class ImportFromCsvCommand extends Command
{
Expand All @@ -36,11 +37,16 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$resourceName = $input->getArgument('resource');
$file = $input->getArgument('file');

$io->title('Importing resources...');

$this->importProcessor->process($resourceName, $file);

return 0;
$io->success('Resources imported successfully.');

return Command::SUCCESS;
}
}
14 changes: 14 additions & 0 deletions src/Importer/BlockImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterChannelsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterCollectionsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterLocalesResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsInTaxonsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterProductsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ImporterTaxonsResolverInterface;
use BitBag\SyliusCmsPlugin\Resolver\ResourceResolverInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Webmozart\Assert\Assert;
Expand All @@ -24,6 +28,10 @@ public function __construct(
private ResourceResolverInterface $blockResourceResolver,
private ImporterCollectionsResolverInterface $importerCollectionsResolver,
private ImporterChannelsResolverInterface $importerChannelsResolver,
private ImporterLocalesResolverInterface $importerLocalesResolver,
private ImporterProductsResolverInterface $importerProductsResolver,
private ImporterTaxonsResolverInterface $importerTaxonsResolver,
private ImporterProductsInTaxonsResolverInterface $importerProductsInTaxonsResolver,
ValidatorInterface $validator,
private BlockRepositoryInterface $blockRepository,
) {
Expand All @@ -38,9 +46,15 @@ public function import(array $row): void
/** @var BlockInterface $block */
$block = $this->blockResourceResolver->getResource($code);
$block->setCode($code);
$block->setName($this->getColumnValue(self::NAME_COLUMN, $row));
$block->setEnabled((bool) $this->getColumnValue(self::ENABLED_COLUMN, $row));

$this->importerCollectionsResolver->resolve($block, $this->getColumnValue(self::COLLECTIONS_COLUMN, $row));
$this->importerChannelsResolver->resolve($block, $this->getColumnValue(self::CHANNELS_COLUMN, $row));
$this->importerLocalesResolver->resolve($block, $this->getColumnValue(self::LOCALES_COLUMN, $row));
$this->importerProductsResolver->resolve($block, $this->getColumnValue(self::PRODUCTS_COLUMN, $row));
$this->importerTaxonsResolver->resolve($block, $this->getColumnValue(self::TAXONS_COLUMN, $row));
$this->importerProductsInTaxonsResolver->resolve($block, $this->getColumnValue(self::PRODUCTS_IN_TAXONS_COLUMN, $row));

$this->validateResource($block, ['bitbag']);
$this->blockRepository->add($block);
Expand Down
12 changes: 8 additions & 4 deletions src/Importer/BlockImporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ interface BlockImporterInterface extends ImporterInterface
{
public const CODE_COLUMN = 'code';

public const NAME_COLUMN = 'name';

public const ENABLED_COLUMN = 'enabled';

public const COLLECTIONS_COLUMN = 'collections';

public const LOCALES_COLUMN = 'locales';

public const CHANNELS_COLUMN = 'channels';

public const PRODUCTS_COLUMN = 'products';

public const NAME_COLUMN = 'name__locale__';

public const CONTENT_COLUMN = 'content__locale__';
public const PRODUCTS_IN_TAXONS_COLUMN = 'products_in_taxons';

public const LINK_COLUMN = 'link__locale__';
public const TAXONS_COLUMN = 'taxons';
}
12 changes: 4 additions & 8 deletions src/Importer/PageImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ public function import(array $row): void

$page->setCode($code);
$page->setFallbackLocale($this->localeContext->getLocaleCode());
$page->setName($this->getColumnValue(self::NAME_COLUMN, $row));
$page->setEnabled((bool) $this->getColumnValue(self::ENABLED_COLUMN, $row));

foreach ($this->getAvailableLocales($this->getTranslatableColumns(), array_keys($row)) as $locale) {
$page->setCurrentLocale($locale);
$page->setSlug($this->getTranslatableColumnValue(self::SLUG_COLUMN, $locale, $row));
$page->setName($this->getTranslatableColumnValue(self::NAME_COLUMN, $locale, $row));
$page->setTitle($this->getTranslatableColumnValue(self::META_TITLE_COLUMN, $locale, $row));
$page->setMetaKeywords($this->getTranslatableColumnValue(self::META_KEYWORDS_COLUMN, $locale, $row));
$page->setMetaDescription($this->getTranslatableColumnValue(self::META_DESCRIPTION_COLUMN, $locale, $row));
}
Expand All @@ -70,15 +72,9 @@ private function getTranslatableColumns(): array
{
return [
self::SLUG_COLUMN,
self::NAME_COLUMN,
self::IMAGE_COLUMN,
self::IMAGE_CODE_COLUMN,
self::META_TITLE_COLUMN,
self::META_KEYWORDS_COLUMN,
self::META_DESCRIPTION_COLUMN,
self::CONTENT_COLUMN,
self::BREADCRUMB_COLUMN,
self::NAME_WHEN_LINKED_COLUMN,
self::DESCRIPTION_WHEN_LINKED_COLUMN,
];
}
}
Loading
Loading