diff --git a/src/BaconUser/Service/RegistrationEvent.php b/src/BaconUser/Service/RegistrationEvent.php new file mode 100644 index 0000000..cdbbc2a --- /dev/null +++ b/src/BaconUser/Service/RegistrationEvent.php @@ -0,0 +1,50 @@ +user = $user; + } + + /** + * @return UserInterface + */ + public function getUser() + { + return $this->user; + } +} diff --git a/src/BaconUser/Service/RegistrationService.php b/src/BaconUser/Service/RegistrationService.php index 6c10480..583db20 100644 --- a/src/BaconUser/Service/RegistrationService.php +++ b/src/BaconUser/Service/RegistrationService.php @@ -15,13 +15,21 @@ use BaconUser\Form\RegistrationForm; use BaconUser\Options\UserOptionsInterface; use Doctrine\Common\Persistence\ObjectManager; +use Zend\EventManager\EventManager; +use Zend\EventManager\EventManagerAwareInterface; +use Zend\EventManager\EventManagerInterface; use Zend\Form\FormInterface; /** * Service managing the registration of users. */ -class RegistrationService +class RegistrationService implements EventManagerAwareInterface { + /** + * @var EventManagerInterface + */ + protected $eventManager; + /** * @var RegistrationForm */ @@ -86,6 +94,9 @@ public function register(array $data) $this->objectManager->persist($user); $this->objectManager->flush(); + // Trigger an event so that we can send an email, for instance + $this->getEventManager()->trigger(new RegistrationEvent($user)); + return $user; } @@ -110,4 +121,33 @@ public function setUserPrototype(UserInterface $userPrototype) $this->userPrototype = $userPrototype; return $this; } + + /** + * setEventManager(): defined by EventManagerAwareInterface. + * + * @see EventManagerAwareInterface::setEventManager() + * @param EventManagerInterface $eventManager + * @return void + */ + public function setEventManager(EventManagerInterface $eventManager) + { + $eventManager->setIdentifiers(array(__CLASS__, get_class($this))); + + $this->eventManager = $eventManager; + } + + /** + * getEventManager(): defined by EventManagerAwareInterface. + * + * @see EventManagerAwareInterface::getEventManager() + * @return EventManagerInterface + */ + public function getEventManager() + { + if (null === $this->eventManager) { + $this->setEventManager(new EventManager()); + } + + return $this->eventManager; + } } diff --git a/tests/BaconUserTest/Service/RegistrationEventTest.php b/tests/BaconUserTest/Service/RegistrationEventTest.php new file mode 100644 index 0000000..8c63b3c --- /dev/null +++ b/tests/BaconUserTest/Service/RegistrationEventTest.php @@ -0,0 +1,28 @@ +getMock('BaconUser\Entity\UserInterface'); + $event = new RegistrationEvent($user); + + $this->assertSame($user, $event->getUser()); + $this->assertEquals(RegistrationEvent::EVENT_USER_REGISTERED, $event->getName()); + } +} diff --git a/tests/BaconUserTest/Service/RegistrationServiceTest.php b/tests/BaconUserTest/Service/RegistrationServiceTest.php index 2dba251..5ed7a9a 100644 --- a/tests/BaconUserTest/Service/RegistrationServiceTest.php +++ b/tests/BaconUserTest/Service/RegistrationServiceTest.php @@ -11,6 +11,7 @@ use BaconUser\Entity\User; use BaconUser\Options\UserOptions; +use BaconUser\Service\RegistrationEvent; use BaconUser\Service\RegistrationService; use PHPUnit_Framework_TestCase as TestCase; @@ -70,6 +71,14 @@ public function testValidRegistration() ->method('flush'); $service = new RegistrationService($form, $objectManager, new UserOptions()); + + // Event should be triggered + $callback = $this->getMock('stdLib', array('__invoke')); + $eventManager = $service->getEventManager(); + + $callback->expects($this->once())->method('__invoke'); + $eventManager->attach(RegistrationEvent::EVENT_USER_REGISTERED, $callback); + $result = $service->register(array()); $this->assertSame($user, $result); } @@ -88,6 +97,14 @@ public function testInvalidRegistration() ->method('flush'); $service = new RegistrationService($form, $objectManager, new UserOptions()); + + // Event should not be triggered if registration is invalid + $callback = $this->getMock('stdLib', array('__invoke')); + $eventManager = $service->getEventManager(); + + $callback->expects($this->never())->method('__invoke'); + $eventManager->attach(RegistrationEvent::EVENT_USER_REGISTERED, $callback); + $result = $service->register(array()); $this->assertNull($result); }