Skip to content
This repository was archived by the owner on Jun 16, 2024. It is now read-only.
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
50 changes: 50 additions & 0 deletions src/BaconUser/Service/RegistrationEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* BaconUser
*
* @link http://github.com/Bacon/BaconUser For the canonical source repository
* @copyright 2013 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/

namespace BaconUser\Service;

use BaconUser\Entity\UserInterface;
use Zend\EventManager\Event;

/**
* Registration event.
*/
class RegistrationEvent extends Event
{
const EVENT_USER_REGISTERED = 'userRegistered';

/**
* $name: defined by Event.
*
* @see Event::$name
* @var string
*/
protected $name = self::EVENT_USER_REGISTERED;

/**
* @var UserInterface
*/
protected $user;

/**
* @param UserInterface $user
*/
public function __construct(UserInterface $user)
{
$this->user = $user;
}

/**
* @return UserInterface
*/
public function getUser()
{
return $this->user;
}
}
42 changes: 41 additions & 1 deletion src/BaconUser/Service/RegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
}
28 changes: 28 additions & 0 deletions tests/BaconUserTest/Service/RegistrationEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* BaconUser
*
* @link http://github.com/Bacon/BaconUser For the canonical source repository
* @copyright 2013 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/

namespace BaconUserTest\Service;

use BaconUser\Service\RegistrationEvent;
use PHPUnit_Framework_TestCase as TestCase;

/**
* @covers BaconUser\Service\RegistrationEvent
*/
class RegistrationEventTest extends TestCase
{
public function testCanCreateEvent()
{
$user = $this->getMock('BaconUser\Entity\UserInterface');
$event = new RegistrationEvent($user);

$this->assertSame($user, $event->getUser());
$this->assertEquals(RegistrationEvent::EVENT_USER_REGISTERED, $event->getName());
}
}
17 changes: 17 additions & 0 deletions tests/BaconUserTest/Service/RegistrationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down