-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
852 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\EventListener\Doctrine; | ||
|
||
use Doctrine\Persistence\Event\LifecycleEventArgs; | ||
use Setono\SyliusMeilisearchPlugin\Message\Command\UpdateSynonyms; | ||
use Setono\SyliusMeilisearchPlugin\Model\SynonymInterface; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
final class SynonymListener | ||
{ | ||
public function __construct(private readonly MessageBusInterface $commandBus) | ||
{ | ||
} | ||
|
||
public function postPersist(LifecycleEventArgs $eventArgs): void | ||
{ | ||
$this->handle($eventArgs); | ||
} | ||
|
||
public function postUpdate(LifecycleEventArgs $eventArgs): void | ||
{ | ||
$this->handle($eventArgs); | ||
} | ||
|
||
public function postRemove(LifecycleEventArgs $eventArgs): void | ||
{ | ||
$this->handle($eventArgs); | ||
} | ||
|
||
public function handle(LifecycleEventArgs $eventArgs): void | ||
{ | ||
if (!$eventArgs->getObject() instanceof SynonymInterface) { | ||
return; | ||
} | ||
|
||
$this->commandBus->dispatch(new UpdateSynonyms()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\EventSubscriber; | ||
|
||
use Knp\Menu\ItemInterface; | ||
use Sylius\Bundle\UiBundle\Menu\Event\MenuBuilderEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class AddMenuSubscriber implements EventSubscriberInterface | ||
{ | ||
public const MENU_ITEM_KEY = 'setono_sylius_meilisearch'; | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
'sylius.menu.admin.main' => 'add', | ||
]; | ||
} | ||
|
||
public function add(MenuBuilderEvent $event): void | ||
{ | ||
$menu = $event->getMenu(); | ||
|
||
$header = $this->getHeader($menu); | ||
|
||
$header | ||
->addChild('synonyms', [ | ||
'route' => 'setono_sylius_meilisearch_admin_synonym_index', | ||
]) | ||
->setLabel('setono_sylius_meilisearch.menu.admin.main.meilisearch.synonyms') | ||
->setLabelAttribute('icon', 'exchange') | ||
; | ||
|
||
$order = ['catalog', 'sales', 'customers', 'marketing', self::MENU_ITEM_KEY]; | ||
$rest = array_diff(array_keys($menu->getChildren()), $order); | ||
|
||
try { | ||
$event->getMenu()->reorderChildren(array_merge($order, $rest)); | ||
} catch (\InvalidArgumentException) { | ||
} | ||
} | ||
|
||
private function getHeader(ItemInterface $menu): ItemInterface | ||
{ | ||
$header = $menu->getChild(self::MENU_ITEM_KEY); | ||
if (null !== $header) { | ||
return $header; | ||
} | ||
|
||
return $menu->addChild(self::MENU_ITEM_KEY) | ||
->setLabel('setono_sylius_meilisearch.menu.admin.main.meilisearch.header'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\EventSubscriber; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Setono\Doctrine\ORMTrait; | ||
use Setono\SyliusMeilisearchPlugin\Factory\SynonymFactoryInterface; | ||
use Setono\SyliusMeilisearchPlugin\Model\SynonymInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\Exception\OutOfBoundsException; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class NewSynonymSubscriber implements EventSubscriberInterface | ||
{ | ||
use ORMTrait; | ||
|
||
public function __construct(ManagerRegistry $managerRegistry, private readonly SynonymFactoryInterface $synonymFactory) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
FormEvents::POST_SUBMIT => 'handleFormEvent', | ||
]; | ||
} | ||
|
||
public function handleFormEvent(FormEvent $event): void | ||
{ | ||
try { | ||
$direction = $event->getForm()->get('direction')->getData(); | ||
} catch (OutOfBoundsException) { | ||
return; | ||
} | ||
|
||
// todo replace with constant | ||
if ('two_way' !== $direction) { | ||
return; | ||
} | ||
|
||
/** @var mixed $synonym */ | ||
$synonym = $event->getData(); | ||
Assert::isInstanceOf($synonym, SynonymInterface::class); | ||
|
||
$newSynonym = $this->synonymFactory->createInverseFromExisting($synonym); | ||
|
||
$this->getManager($newSynonym)->persist($newSynonym); | ||
} | ||
} |
Oops, something went wrong.