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

To modify the query and response to Makaira API, with DI. #49

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
22 changes: 21 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,22 @@ 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)
{
$event = new AutoSuggesterResponseEvent($result);
$this->dispatcher->dispatch(
AutoSuggesterResponseEvent::NAME,
$event
);
$result = (array)$event->getResult();
}
}
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)
{
$this->result = new \ArrayObject($result);
}

/**
* @return \ArrayObject
*/
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;
}
}
33 changes: 33 additions & 0 deletions src/Makaira/Connect/Event/SearchResponseEvent.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 Symfony\Component\EventDispatcher\Event;

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

/**
* @var array
*/
private $productIds;

public function __construct(array $productIds)
{
$this->productIds = new \ArrayObject($productIds);
}

/**
* @return array
*/
public function getProductIds()
{
return $this->productIds;
}
}
20 changes: 19 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,29 @@ 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 = [])
{
$searchResponseEvent = new SearchResponseEvent($productIds);

$dispatcher = Connect::getContainerFactory()->getContainer()->get(EventDispatcherInterface::class);
$dispatcher->dispatch(
SearchResponseEvent::NAME,
$searchResponseEvent
);

$productIds = (array)$searchResponseEvent->getProductIds();
}

/**
Expand Down
119 changes: 119 additions & 0 deletions tests/Makaira/Connect/ApiEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

namespace oxidprojects\DI\Tests\Makaira\Connect;

use Makaira\Connect\Core\Autosuggester;
use Makaira\Connect\Event\AutoSuggesterResponseEvent;
use Makaira\Connect\Event\ModifierQueryRequestEvent;
use Makaira\Connect\Event\SearchResponseEvent;
use Makaira\Connect\IntegrationTest;
use Makaira\Connect\Utils\OperationalIntelligence;
use Makaira\Query;
use Makaira\Result;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class ApiEventTest extends IntegrationTest
{
public function testQueryAutosuggesterEvent()
{
//Arrange
$container = $this->getContainer();
$event = $container->get(EventDispatcherInterface::class);
$autosuggester = $container->get(Autosuggester::class);
$query = new Query(['searchPhrase' => 'Tisch']);

//Act
$event->addListener(ModifierQueryRequestEvent::NAME_AUTOSUGGESTER, [$this, 'listenerModifierQueryRequest']);

$autosuggester->modifyRequest($query);
$accept = $query->searchPhrase;

//Assert
$this->assertEquals('Bett', $accept);
}

public function testQuerySearchEvent()
{
//Arrange
$container = $this->getContainer();
$event = $container->get(EventDispatcherInterface::class);
$makaira_connect_request_handler = new \makaira_connect_request_handler();
$reflectionClass = new \ReflectionClass(\makaira_connect_request_handler::class);
$methodModifyRequest = $reflectionClass->getMethod('modifyRequest');
$methodModifyRequest->setAccessible(true);

$query = new Query(['searchPhrase' => 'Tische']);

//Act
$event->addListener(ModifierQueryRequestEvent::NAME_SEARCH, [$this, 'listenerModifierQueryRequest']);

$methodModifyRequest->invoke($makaira_connect_request_handler, $query);
$accept = $query->searchPhrase;

//Assert
$this->assertEquals('Bett', $accept);
}

public function testResponseSearchEvent()
{
//Arrange
$container = $this->getContainer();
$event = $container->get(EventDispatcherInterface::class);
$makaira_connect_request_handler = new \makaira_connect_request_handler();

$productIds = ['1234'];
$expect = ['1234', 'Extra ID'];

//Act
$event->addListener(SearchResponseEvent::NAME, [$this, 'listenerSearchResponse']);
$makaira_connect_request_handler->afterSearchRequest($productIds);

//Assert
$this->assertEquals($expect, $productIds);
}

public function testResponseAutoSuggesterResponseEvent()
{
//Arrange
$container = $this->getContainer();
$event = $container->get(EventDispatcherInterface::class);
$autosuggester = $container->get(Autosuggester::class);

$result = ['count' => 33];

//Act
$event->addListener(AutoSuggesterResponseEvent::NAME, [$this, 'listenerResponseAutoSuggester']);
$autosuggester->afterSearchRequest($result);

//Assert
$this->assertEquals(66, $result['count']);
}

public function listenerResponseAutoSuggester(AutoSuggesterResponseEvent $event) {
$result = $event->getResult();
$this->assertInstanceOf(\ArrayObject::class, $result);
$result['count'] = 66;
}

public function listenerModifierQueryRequest(ModifierQueryRequestEvent $event) {
$event->getQuery()->searchPhrase = 'Bett';
}

public function listenerSearchResponse(SearchResponseEvent $event)
{
$productIds = $event->getProductIds();
$productIds[] = 'Extra ID';
}

protected function setUp(): void
{
$container = $this->getContainer();
$event = $container->get(EventDispatcherInterface::class);
$event->removeListener(ModifierQueryRequestEvent::NAME_AUTOSUGGESTER, [$this, 'listenerModifierQueryRequest']);
$event->removeListener(ModifierQueryRequestEvent::NAME_SEARCH, [$this, 'listenerModifierQueryRequest']);
$event->removeListener(SearchResponseEvent::NAME, [$this, 'listenerSearchResponse']);
$event->removeListener(AutoSuggesterResponseEvent::NAME, [$this, 'listenerResponseAutoSuggester']);
}
}
2 changes: 2 additions & 0 deletions tests/Makaira/Connect/OxidMocks/oxRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public static function get($key)
case 'oxseoencodercategory':
case 'oxseoencodermanufacturer':
return new oxSeoEncoder();
case 'makaira_cookie_utils':
return new makaira_cookie_utils();
}

return null;
Expand Down