diff --git a/services.yaml b/services.yaml index 1235d8a8..f89c34e1 100644 --- a/services.yaml +++ b/services.yaml @@ -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: diff --git a/src/Makaira/Connect/Core/Autosuggester.php b/src/Makaira/Connect/Core/Autosuggester.php index 66491fc8..15fa8c9c 100644 --- a/src/Makaira/Connect/Core/Autosuggester.php +++ b/src/Makaira/Connect/Core/Autosuggester.php @@ -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 @@ -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; } /** @@ -373,6 +383,10 @@ protected function prepareProductItem(&$doc, &$product) */ public function modifyRequest(Query &$query) { + $this->dispatcher->dispatch( + ModifierQueryRequestEvent::NAME_AUTOSUGGESTER, + new ModifierQueryRequestEvent($query) + ); } /** @@ -380,5 +394,11 @@ public function modifyRequest(Query &$query) */ public function afterSearchRequest(&$result) { + $event = new AutoSuggesterResponseEvent($result); + $this->dispatcher->dispatch( + AutoSuggesterResponseEvent::NAME, + $event + ); + $result = (array)$event->getResult(); } } diff --git a/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php b/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php new file mode 100644 index 00000000..7acd084f --- /dev/null +++ b/src/Makaira/Connect/Event/AutoSuggesterResponseEvent.php @@ -0,0 +1,31 @@ +result = new \ArrayObject($result); + } + + /** + * @return \ArrayObject + */ + public function getResult() + { + return $this->result; + } +} diff --git a/src/Makaira/Connect/Event/ModifierQueryRequestEvent.php b/src/Makaira/Connect/Event/ModifierQueryRequestEvent.php new file mode 100644 index 00000000..4d9e8afc --- /dev/null +++ b/src/Makaira/Connect/Event/ModifierQueryRequestEvent.php @@ -0,0 +1,33 @@ +query = $query; + } + + /** + * @return Query + */ + public function getQuery() + { + return $this->query; + } +} diff --git a/src/Makaira/Connect/Event/SearchResponseEvent.php b/src/Makaira/Connect/Event/SearchResponseEvent.php new file mode 100644 index 00000000..15e8b9fa --- /dev/null +++ b/src/Makaira/Connect/Event/SearchResponseEvent.php @@ -0,0 +1,33 @@ +productIds = new \ArrayObject($productIds); + } + + /** + * @return array + */ + public function getProductIds() + { + return $this->productIds; + } +} diff --git a/src/oxid/core/makaira_connect_request_handler.php b/src/oxid/core/makaira_connect_request_handler.php index 636f43c4..34274963 100644 --- a/src/oxid/core/makaira_connect_request_handler.php +++ b/src/oxid/core/makaira_connect_request_handler.php @@ -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 @@ -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(); } /** diff --git a/tests/Makaira/Connect/ApiEventTest.php b/tests/Makaira/Connect/ApiEventTest.php new file mode 100644 index 00000000..227cc880 --- /dev/null +++ b/tests/Makaira/Connect/ApiEventTest.php @@ -0,0 +1,119 @@ +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']); + } +} diff --git a/tests/Makaira/Connect/OxidMocks/oxRegistry.php b/tests/Makaira/Connect/OxidMocks/oxRegistry.php index ab955628..5a9b01bf 100644 --- a/tests/Makaira/Connect/OxidMocks/oxRegistry.php +++ b/tests/Makaira/Connect/OxidMocks/oxRegistry.php @@ -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;