Skip to content

Commit

Permalink
To modify the query and response to Makaira API, with DI.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmloberon authored and TumTum committed Apr 20, 2023
1 parent 001a3bc commit 055aa1f
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 2 deletions.
1 change: 1 addition & 0 deletions services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ services:
$oxLang: '@makaira.connect.oxid.language'
$operationalIntelligence: '@Makaira\Connect\Utils\OperationalIntelligence'
$searchHandler: '@Makaira\Connect\SearchHandler'
$dispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface'

# makaira.connect.searchhandler
Makaira\Connect\SearchHandler:
Expand Down
20 changes: 19 additions & 1 deletion src/Makaira/Connect/Core/Autosuggester.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

namespace Makaira\Connect\Core;

use Makaira\Connect\Event\ModifierQueryRequestEvent;
use Makaira\Connect\Event\AutoSuggesterResponseEvent;
use oxLang as Language;
use Makaira\Connect\SearchHandler;
use Makaira\Connect\Utils\OperationalIntelligence;
use Makaira\Constraints;
use Makaira\Query;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class makaira_connect_autosuggester
Expand All @@ -36,14 +39,21 @@ class Autosuggester
*/
private $searchHandler;

/**
* @var EventDispatcherInterface
*/
private $dispatcher;

public function __construct(
Language $oxLang,
OperationalIntelligence $operationalIntelligence,
SearchHandler $searchHandler
SearchHandler $searchHandler,
EventDispatcherInterface $dispatcher
) {
$this->oxLang = $oxLang;
$this->operationalIntelligence = $operationalIntelligence;
$this->searchHandler = $searchHandler;
$this->dispatcher = $dispatcher;
}

/**
Expand Down Expand Up @@ -373,12 +383,20 @@ protected function prepareProductItem(&$doc, &$product)
*/
public function modifyRequest(Query &$query)
{
$this->dispatcher->dispatch(
ModifierQueryRequestEvent::NAME_AUTOSUGGESTER,
new ModifierQueryRequestEvent($query)
);
}

/**
* @param $result
*/
public function afterSearchRequest(&$result)
{
$this->dispatcher->dispatch(
AutoSuggesterResponseEvent::NAME,
new AutoSuggesterResponseEvent($result)
);
}
}
31 changes: 31 additions & 0 deletions src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Makaira\Connect\Event;

use Makaira\Result;
use Symfony\Component\EventDispatcher\Event;

/**
* After receiving the Auggester, you can still adjust it for the display.
*/
class AutoSuggesterResponseEvent extends Event
{
public const NAME = 'makaira.response.autosuggester';

private $result;

public function __construct(Result &$result)
{
$this->result = $result;
}

/**
* @return Result
*/
public function getResult()
{
return $this->result;
}
}
33 changes: 33 additions & 0 deletions src/Makaira/Connect/Event/ModifierQueryRequestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Makaira\Connect\Event;

use Makaira\Query;
use Symfony\Component\EventDispatcher\Event;

/**
* Possibility to change the query before sending it to Makaira.
*/
class ModifierQueryRequestEvent extends Event
{
public const NAME_SEARCH = 'makaira.request.modifier.query.search';

public const NAME_AUTOSUGGESTER = 'makaira.request.modifier.query.autosuggester';

private $query;

public function __construct(Query $query)
{
$this->query = $query;
}

/**
* @return Query
*/
public function getQuery()
{
return $this->query;
}
}
30 changes: 30 additions & 0 deletions src/Makaira/Connect/Event/SearchResponseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Makaira\Connect\Event;

use Symfony\Component\EventDispatcher\Event;

/**
* After receiving the $productIds you can still adjust them
*/
class SearchResponseEvent extends Event
{
public const NAME = 'makaira.response.search';

private array $productIds;

public function __construct(array &$productIds)
{
$this->productIds = $productIds;
}

/**
* @return array
*/
public function getProductIds()
{
return $this->productIds;
}
}
16 changes: 15 additions & 1 deletion src/oxid/core/makaira_connect_request_handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Makaira\Query;
use Makaira\Result;
use Makaira\Connect\Connect;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Makaira\Connect\Event\ModifierQueryRequestEvent;
use Makaira\Connect\Event\SearchResponseEvent;

/**
* Class makaira_connect_request_handler
Expand Down Expand Up @@ -273,14 +276,25 @@ public function deletePageNumberCookie()

protected function modifyRequest(Query $query)
{
$dispatcher = Connect::getContainerFactory()->getContainer()->get(EventDispatcherInterface::class);
$dispatcher->dispatch(
ModifierQueryRequestEvent::NAME_SEARCH,
new ModifierQueryRequestEvent($query),
);

return $query;
}

/**
* @param array $productIds
*/
public function afterSearchRequest(array $productIds = [])
public function afterSearchRequest(array &$productIds = [])
{
$dispatcher = Connect::getContainerFactory()->getContainer()->get(EventDispatcherInterface::class);
$dispatcher->dispatch(
SearchResponseEvent::NAME,
new SearchResponseEvent($productIds),
);
}

/**
Expand Down

0 comments on commit 055aa1f

Please sign in to comment.