Skip to content

Commit

Permalink
Add taxon position sorting and make it default
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbager committed Sep 21, 2019
1 parent bf9a0c4 commit 154e988
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@

namespace spec\BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler;

use BitBag\SyliusElasticsearchPlugin\Context\TaxonContextInterface;
use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\ShopProductsSortDataHandler;
use BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\SortDataHandlerInterface;
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\TaxonInterface;

final class ShopProductsSortDataHandlerSpec extends ObjectBehavior
{
function let(
ConcatedNameResolverInterface $channelPricingNameResolver,
ChannelContextInterface $channelContext
ChannelContextInterface $channelContext,
TaxonContextInterface $taxonContext,
ConcatedNameResolverInterface $taxonPositionNameResolver
): void {
$this->beConstructedWith(
$channelPricingNameResolver,
$channelContext,
$taxonContext,
$taxonPositionNameResolver,
'sold_units',
'created_at',
'price'
Expand All @@ -43,11 +49,19 @@ function it_implements_sort_data_handler_interface(): void
$this->shouldHaveType(SortDataHandlerInterface::class);
}

function it_retrieves_data(): void
function it_retrieves_data(
TaxonContextInterface $taxonContext,
TaxonInterface $taxon,
ConcatedNameResolverInterface $taxonPositionNameResolver
): void
{
$taxonContext->getTaxon()->willReturn($taxon);
$taxon->getCode()->willReturn('t_shirt');
$taxonPositionNameResolver->resolvePropertyName('t_shirt')->willReturn('t_shirt_position');

$this->retrieveData([])->shouldBeEqualTo([
'sort' => [
'sold_units' => [
't_shirt_position' => [
'order' => SortDataHandlerInterface::SORT_DESC_INDEX,
],
],
Expand Down
23 changes: 21 additions & 2 deletions src/Controller/RequestDataHandler/ShopProductsSortDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler;

use BitBag\SyliusElasticsearchPlugin\Context\TaxonContextInterface;
use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;

Expand All @@ -23,6 +24,12 @@ final class ShopProductsSortDataHandler implements SortDataHandlerInterface
/** @var ChannelContextInterface */
private $channelContext;

/** @var TaxonContextInterface */
private $taxonContext;

/** @var ConcatedNameResolverInterface */
private $taxonPositionNameResolver;

/** @var string */
private $soldUnitsProperty;

Expand All @@ -35,12 +42,16 @@ final class ShopProductsSortDataHandler implements SortDataHandlerInterface
public function __construct(
ConcatedNameResolverInterface $channelPricingNameResolver,
ChannelContextInterface $channelContext,
TaxonContextInterface $taxonContext,
ConcatedNameResolverInterface $taxonPositionNameResolver,
string $soldUnitsProperty,
string $createdAtProperty,
string $pricePropertyPrefix
) {
$this->channelPricingNameResolver = $channelPricingNameResolver;
$this->channelContext = $channelContext;
$this->taxonContext = $taxonContext;
$this->taxonPositionNameResolver = $taxonPositionNameResolver;
$this->soldUnitsProperty = $soldUnitsProperty;
$this->createdAtProperty = $createdAtProperty;
$this->pricePropertyPrefix = $pricePropertyPrefix;
Expand All @@ -49,11 +60,12 @@ public function __construct(
public function retrieveData(array $requestData): array
{
$data = [];
$positionSortingProperty = $this->getPositionSortingProperty();

$orderBy = isset($requestData[self::ORDER_BY_INDEX]) ? $requestData[self::ORDER_BY_INDEX] : $this->soldUnitsProperty;
$orderBy = isset($requestData[self::ORDER_BY_INDEX]) ? $requestData[self::ORDER_BY_INDEX] : $positionSortingProperty;
$sort = isset($requestData[self::SORT_INDEX]) ? $requestData[self::SORT_INDEX] : self::SORT_DESC_INDEX;

$availableSorters = [$this->soldUnitsProperty, $this->createdAtProperty, $this->pricePropertyPrefix];
$availableSorters = [$positionSortingProperty, $this->soldUnitsProperty, $this->createdAtProperty, $this->pricePropertyPrefix];
$availableSorting = [self::SORT_ASC_INDEX, self::SORT_DESC_INDEX];

if (!in_array($orderBy, $availableSorters) || !in_array($sort, $availableSorting)) {
Expand All @@ -69,4 +81,11 @@ public function retrieveData(array $requestData): array

return $data;
}

private function getPositionSortingProperty(): string
{
$taxonCode = $this->taxonContext->getTaxon()->getCode();

return $this->taxonPositionNameResolver->resolvePropertyName($taxonCode);
}
}
37 changes: 37 additions & 0 deletions src/PropertyBuilder/ProductMainTaxonPositionPropertyBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace BitBag\SyliusElasticsearchPlugin\PropertyBuilder;

use BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolverInterface;
use Elastica\Document;
use FOS\ElasticaBundle\Event\TransformEvent;
use Sylius\Component\Core\Model\ProductInterface;

final class ProductMainTaxonPositionPropertyBuilder extends AbstractBuilder
{
/** @var ConcatedNameResolverInterface */
private $taxonPositionNameResolver;

public function __construct(ConcatedNameResolverInterface $taxonPositionNameResolver)
{
$this->taxonPositionNameResolver = $taxonPositionNameResolver;
}

public function consumeEvent(TransformEvent $event): void
{
$this->buildProperty($event, ProductInterface::class,
function (ProductInterface $product, Document $document): void {
$mainTaxon = $product->getMainTaxon();

if (null === $mainTaxon) {
return;
}

$document->set(
$this->taxonPositionNameResolver->resolvePropertyName($mainTaxon->getCode()),
$mainTaxon->getPosition()
);
}
);
}
}
4 changes: 2 additions & 2 deletions src/Refresher/ResourceRefresherInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
Expand All @@ -10,6 +8,8 @@
* an email on [email protected].
*/

declare(strict_types=1);

namespace BitBag\SyliusElasticsearchPlugin\Refresher;

use Sylius\Component\Resource\Model\ResourceInterface;
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/indexes/bitbag_shop_products.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ parameters:
bitbag_es_shop_product_price_property_prefix: price
bitbag_es_shop_option_property_prefix: option
bitbag_es_shop_attribute_property_prefix: attribute
bitbag_es_shop_taxon_position_property_prefix: taxon_position

fos_elastica:
indexes:
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<service id="bitbag_sylius_elasticsearch_plugin.controller.request_data_handler.shop_products_sort" class="BitBag\SyliusElasticsearchPlugin\Controller\RequestDataHandler\ShopProductsSortDataHandler">
<argument type="service" id="bitbag_sylius_elasticsearch_plugin.property_name_resolver.channel_pricing" />
<argument type="service" id="sylius.context.channel" />
<argument type="service" id="bitbag.sylius_elasticsearch_plugin.context.taxon" />
<argument type="service" id="bitbag_sylius_elasticsearch_plugin.property_name_resolver.taxon_position" />
<argument>%bitbag_es_shop_product_sold_units%</argument>
<argument>%bitbag_es_shop_product_created_at%</argument>
<argument>%bitbag_es_shop_product_price_property_prefix%</argument>
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/services/property_builder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<tag name="kernel.event_subscriber" />
</service>

<service id="bitbag_sylius_elasticsearch_plugin.property_builder.main_taxon_position" class="BitBag\SyliusElasticsearchPlugin\PropertyBuilder\ProductMainTaxonPositionPropertyBuilder">
<argument type="service" id="bitbag_sylius_elasticsearch_plugin.property_name_resolver.taxon_position" />
<tag name="kernel.event_subscriber" />
</service>

<service id="bitbag_sylius_elasticsearch_plugin.property_builder.mapper.product_taxons" class="BitBag\SyliusElasticsearchPlugin\PropertyBuilder\Mapper\ProductTaxonsMapper" />
</services>
</container>
4 changes: 4 additions & 0 deletions src/Resources/config/services/property_name_resolver.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
<service id="bitbag_sylius_elasticsearch_plugin.property_name_resolver.price" class="BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\PriceNameResolver">
<argument>%bitbag_es_shop_product_price_property_prefix%</argument>
</service>

<service id="bitbag_sylius_elasticsearch_plugin.property_name_resolver.taxon_position" class="BitBag\SyliusElasticsearchPlugin\PropertyNameResolver\ConcatedNameResolver">
<argument>%bitbag_es_shop_taxon_position_property_prefix%</argument>
</service>
</services>
</container>

0 comments on commit 154e988

Please sign in to comment.