Skip to content

Commit 1e0ecda

Browse files
authored
Merge pull request #274 from Setono/blocktaxons
Related taxons to blocks
2 parents 1e5c1dd + 9295b07 commit 1e0ecda

21 files changed

Lines changed: 321 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace spec\BitBag\SyliusCmsPlugin\Assigner;
4+
5+
use BitBag\SyliusCmsPlugin\Assigner\TaxonsAssigner;
6+
use BitBag\SyliusCmsPlugin\Assigner\TaxonsAssignerInterface;
7+
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
8+
use BitBag\SyliusCmsPlugin\Entity\TaxonAwareInterface;
9+
use PhpSpec\ObjectBehavior;
10+
use Sylius\Component\Core\Model\TaxonInterface;
11+
12+
final class TaxonsAssignerSpec extends ObjectBehavior
13+
{
14+
function let(TaxonRepositoryInterface $taxonRepository): void
15+
{
16+
$this->beConstructedWith($taxonRepository);
17+
}
18+
19+
function it_is_initializable(): void
20+
{
21+
$this->shouldHaveType(TaxonsAssigner::class);
22+
}
23+
24+
function it_implements_taxons_assigner_interface(): void
25+
{
26+
$this->shouldHaveType(TaxonsAssignerInterface::class);
27+
}
28+
29+
function it_assigns_taxons(
30+
TaxonRepositoryInterface $taxonRepository,
31+
TaxonInterface $mugsTaxon,
32+
TaxonInterface $stickersTaxon,
33+
TaxonAwareInterface $taxonsAware
34+
): void
35+
{
36+
$taxonRepository->findOneBy(['code' => 'mugs'])->willReturn($mugsTaxon);
37+
$taxonRepository->findOneBy(['code' => 'stickers'])->willReturn($stickersTaxon);
38+
39+
$taxonsAware->addTaxon($mugsTaxon)->shouldBeCalled();
40+
$taxonsAware->addTaxon($stickersTaxon)->shouldBeCalled();
41+
42+
$this->assign($taxonsAware, ['mugs', 'stickers']);
43+
}
44+
}

spec/Entity/BlockSpec.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Sylius\Component\Core\Model\ChannelInterface;
2020
use Sylius\Component\Core\Model\ProductInterface;
2121
use Sylius\Component\Resource\Model\ResourceInterface;
22+
use Sylius\Component\Core\Model\TaxonInterface;
2223

2324
final class BlockSpec extends ObjectBehavior
2425
{
@@ -81,4 +82,16 @@ function it_associates_channels(ChannelInterface $firstChannel, ChannelInterface
8182

8283
$this->hasChannel($firstChannel)->shouldReturn(false);
8384
}
85+
86+
function it_associates_taxons(TaxonInterface $firstTaxon, TaxonInterface $secondTaxon): void
87+
{
88+
$this->addTaxon($firstTaxon);
89+
$this->hasTaxon($firstTaxon)->shouldReturn(true);
90+
91+
$this->hasTaxon($secondTaxon)->shouldReturn(false);
92+
93+
$this->removeTaxon($firstTaxon);
94+
95+
$this->hasTaxon($secondTaxon)->shouldReturn(false);
96+
}
8497
}

src/Assigner/TaxonsAssigner.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace BitBag\SyliusCmsPlugin\Assigner;
14+
15+
use BitBag\SyliusCmsPlugin\Entity\TaxonAwareInterface;
16+
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
17+
use Sylius\Component\Core\Model\TaxonInterface;
18+
19+
final class TaxonsAssigner implements TaxonsAssignerInterface
20+
{
21+
/** @var TaxonRepositoryInterface */
22+
private $taxonRepository;
23+
24+
public function __construct(TaxonRepositoryInterface $taxonRepository)
25+
{
26+
$this->taxonRepository = $taxonRepository;
27+
}
28+
29+
public function assign(TaxonAwareInterface $taxonAware, array $taxonCodes): void
30+
{
31+
foreach ($taxonCodes as $taxonCode) {
32+
/** @var TaxonInterface $taxon */
33+
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]);
34+
35+
if (null !== $taxon) {
36+
$taxonAware->addTaxon($taxon);
37+
}
38+
}
39+
}
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace BitBag\SyliusCmsPlugin\Assigner;
14+
15+
use BitBag\SyliusCmsPlugin\Entity\TaxonAwareInterface;
16+
17+
interface TaxonsAssignerInterface
18+
{
19+
public function assign(TaxonAwareInterface $taxonAware, array $taxonCodes): void;
20+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace BitBag\SyliusCmsPlugin\Controller\Action\Admin;
14+
15+
use FOS\RestBundle\View\View;
16+
use FOS\RestBundle\View\ViewHandler;
17+
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\Response;
20+
use Sylius\Component\Locale\Context\LocaleContextInterface;
21+
22+
final class TaxonSearchAction
23+
{
24+
/** @var TaxonRepositoryInterface */
25+
private $taxonRepository;
26+
27+
/** @var LocaleContextInterface */
28+
private $localeContext;
29+
30+
/** @var ViewHandler */
31+
private $viewHandler;
32+
33+
public function __construct(
34+
TaxonRepositoryInterface $taxonRepository,
35+
LocaleContextInterface $localeContext,
36+
ViewHandler $viewHandler
37+
)
38+
{
39+
$this->taxonRepository = $taxonRepository;
40+
$this->localeContext = $localeContext;
41+
$this->viewHandler = $viewHandler;
42+
}
43+
44+
public function __invoke(Request $request): Response
45+
{
46+
$taxon = $this->taxonRepository->findByNamePart($request->get('phrase', ''), $this->localeContext->getLocaleCode());
47+
$view = View::create($taxon);
48+
49+
$this->viewHandler->setExclusionStrategyGroups(['Autocomplete']);
50+
$view->getContext()->enableMaxDepth();
51+
52+
return $this->viewHandler->handle($view);
53+
}
54+
}

src/Entity/Block.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Block implements BlockInterface
2121
use ToggleableTrait;
2222
use SectionableTrait;
2323
use ProductsAwareTrait;
24+
use TaxonAwareTrait;
2425
use ChannelsAwareTrait;
2526
use TranslatableTrait {
2627
__construct as protected initializeTranslationsCollection;
@@ -31,6 +32,7 @@ public function __construct()
3132
$this->initializeTranslationsCollection();
3233
$this->initializeSectionsCollection();
3334
$this->initializeProductsCollection();
35+
$this->initializeTaxonCollection();
3436
$this->initializeChannelsCollection();
3537
}
3638

src/Entity/BlockInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface BlockInterface extends
2222
TranslatableInterface,
2323
ToggleableInterface,
2424
ProductsAwareInterface,
25+
TaxonAwareInterface,
2526
SectionableInterface,
2627
ChannelsAwareInterface,
2728
ContentableInterface

src/Entity/TaxonAwareInterface.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace BitBag\SyliusCmsPlugin\Entity;
14+
15+
use Doctrine\Common\Collections\Collection;
16+
use Sylius\Component\Core\Model\TaxonInterface;
17+
18+
interface TaxonAwareInterface
19+
{
20+
public function initializeTaxonCollection(): void;
21+
22+
/**
23+
* @return Collection|TaxonInterface[]
24+
*/
25+
public function getTaxons(): Collection;
26+
27+
public function hasTaxon(TaxonInterface $taxon): bool;
28+
29+
public function addTaxon(TaxonInterface $taxon): void;
30+
31+
public function removeTaxon(TaxonInterface $taxon): void;
32+
}

src/Entity/TaxonAwareTrait.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* another great project.
7+
* You can find more information about us on https://bitbag.shop and write us
8+
* an email on mikolaj.krol@bitbag.pl.
9+
*/
10+
11+
declare(strict_types=1);
12+
/*
13+
* This file has been created by developers from BitBag.
14+
* Feel free to contact us once you face any issues or want to start
15+
* another great project.
16+
* You can find more information about us on https://bitbag.shop and write us
17+
* an email on mikolaj.krol@bitbag.pl.
18+
*/
19+
20+
namespace BitBag\SyliusCmsPlugin\Entity;
21+
22+
use Doctrine\Common\Collections\ArrayCollection;
23+
use Doctrine\Common\Collections\Collection;
24+
use Sylius\Component\Core\Model\TaxonInterface;
25+
26+
trait TaxonAwareTrait
27+
{
28+
/** @var Collection|TaxonInterface[] */
29+
protected $taxonomies;
30+
31+
public function initializeTaxonCollection(): void
32+
{
33+
$this->taxonomies = new ArrayCollection();
34+
}
35+
36+
public function getTaxons(): Collection
37+
{
38+
return $this->taxonomies;
39+
}
40+
41+
public function hasTaxon(TaxonInterface $taxon): bool
42+
{
43+
return $this->taxonomies->contains($taxon);
44+
}
45+
46+
public function addTaxon(TaxonInterface $taxon): void
47+
{
48+
if (false === $this->hasTaxon($taxon)) {
49+
$this->taxonomies->add($taxon);
50+
}
51+
}
52+
53+
public function removeTaxon(TaxonInterface $taxon): void
54+
{
55+
if (true === $this->hasTaxon($taxon)) {
56+
$this->taxonomies->removeElement($taxon);
57+
}
58+
}
59+
}

src/Fixture/BlockFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
4949
->booleanNode('enabled')->defaultTrue()->end()
5050
->integerNode('products')->defaultNull()->end()
5151
->arrayNode('productCodes')->scalarPrototype()->end()->end()
52+
->arrayNode('taxons')->scalarPrototype()->end()->end()
5253
->arrayNode('sections')->scalarPrototype()->end()->end()
5354
->arrayNode('channels')->scalarPrototype()->end()->end()
5455
->arrayNode('translations')

0 commit comments

Comments
 (0)