* zend eventmanager added
* sms store triggers an event on save * event subscriber middleware added. place to set up listeners * koinservice lazylistens on sms save event
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use Zend\EventManager\EventManager;
|
||||
|
||||
/**
|
||||
* The configuration provider for the App module
|
||||
*
|
||||
@@ -35,6 +37,7 @@ class ConfigProvider
|
||||
return [
|
||||
'invokables' => [
|
||||
Action\PingAction::class => Action\PingAction::class,
|
||||
EventManager::class => EventManager::class,
|
||||
],
|
||||
'factories' => [
|
||||
Action\HomePageAction::class => Action\HomePageFactory::class,
|
||||
|
||||
24
src/App/Middleware/EventSubscriberMiddleware.php
Normal file
24
src/App/Middleware/EventSubscriberMiddleware.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Middleware;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class EventSubscriberMiddleware implements ServerMiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* This actually does nothing, just wraps the next middleware in the pipe.
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @param DelegateInterface $delegate
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
|
||||
{
|
||||
return $delegate->process($request);
|
||||
}
|
||||
}
|
||||
27
src/App/Middleware/EventSubscriberMiddlewareFactory.php
Normal file
27
src/App/Middleware/EventSubscriberMiddlewareFactory.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Middleware;
|
||||
|
||||
use App\Service\KoinService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\EventManager\EventManager;
|
||||
use Zend\EventManager\LazyListener;
|
||||
|
||||
class EventSubscriberMiddlewareFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$eventManager = $container->get(EventManager::class);
|
||||
$eventManager->setIdentifiers([
|
||||
EventSubscriberMiddleware::class,
|
||||
]);
|
||||
|
||||
$lazyListener = new LazyListener([
|
||||
'listener' => KoinService::class,
|
||||
'method' => 'onReceiveSmsListener',
|
||||
], $container);
|
||||
$eventManager->attach('store.sms.persisted', $lazyListener);
|
||||
|
||||
return new EventSubscriberMiddleware();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace App\Service;
|
||||
use App\Entity\Sms;
|
||||
use GuzzleHttp\Client;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Zend\EventManager\Event;
|
||||
|
||||
class KoinService
|
||||
{
|
||||
@@ -60,6 +61,11 @@ class KoinService
|
||||
$this->httpClient = $httpClient;
|
||||
}
|
||||
|
||||
public function onReceiveSmsListener(Event $event)
|
||||
{
|
||||
$this->onReceiveSms($event->getParam('sms'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Sms $sms
|
||||
* @return int
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace App\Service;
|
||||
use App\Entity\Sms;
|
||||
use App\Entity\User;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Zend\EventManager\EventManager;
|
||||
|
||||
class SmsStoreService
|
||||
{
|
||||
@@ -15,9 +16,10 @@ class SmsStoreService
|
||||
*/
|
||||
private $em;
|
||||
|
||||
public function __construct(EntityManager $em)
|
||||
public function __construct(EntityManager $em, EventManager $eventManager)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->eventManager = $eventManager;
|
||||
}
|
||||
|
||||
public function storeSms(string $hashKey, int $direction, array $requestData): bool
|
||||
@@ -34,6 +36,10 @@ class SmsStoreService
|
||||
$this->em->persist($sms);
|
||||
$this->em->flush();
|
||||
|
||||
$this->eventManager->trigger('store.sms.persisted', $this, [
|
||||
'sms' => $sms,
|
||||
]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Service;
|
||||
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\EventManager\EventManager;
|
||||
|
||||
|
||||
class SmsStoreServiceFactory
|
||||
@@ -13,7 +14,8 @@ class SmsStoreServiceFactory
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $container->get('doctrine.entity_manager.orm_default');
|
||||
$eventManager = $container->get(EventManager::class);
|
||||
|
||||
return new SmsStoreService($em);
|
||||
return new SmsStoreService($em,$eventManager);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user