Skip to content

Commit

Permalink
Add events when messages are sent, read or deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jul 16, 2011
1 parent 1e2badf commit 7e533f0
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 4 deletions.
19 changes: 17 additions & 2 deletions Deleter/Deleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use Ornicar\MessageBundle\Security\AuthorizerInterface;
use Ornicar\MessageBundle\Model\ThreadInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Ornicar\MessageBundle\Security\ParticipantProviderInterface;
use Ornicar\MessageBundle\Event\OrnicarMessageEvents;
use Ornicar\MessageBundle\Event\ThreadEvent;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Marks threads as deleted
Expand All @@ -28,10 +31,18 @@ class Deleter implements DeleterInterface
*/
protected $participantProvider;

public function __construct(AuthorizerInterface $authorizer, ParticipantProviderInterface $participantProvider)
/**
* The event dispatcher
*
* @var EventDispatcherInterface
*/
protected $dispatcher;

public function __construct(AuthorizerInterface $authorizer, ParticipantProviderInterface $participantProvider, EventDispatcherInterface $dispatcher)
{
$this->authorizer = $authorizer;
$this->participantProvider = $participantProvider;
$this->dispatcher = $dispatcher;
}

/**
Expand All @@ -45,6 +56,8 @@ public function markAsDeleted(ThreadInterface $thread)
throw new AccessDeniedException('You are not allowed to delete this thread');
}
$thread->setIsDeletedByParticipant($this->getAuthenticatedParticipant(), true);

$this->dispatcher->dispatch(OrnicarMessageEvents::POST_DELETE, new ThreadEvent($thread));
}

/**
Expand All @@ -58,6 +71,8 @@ public function markAsUndeleted(ThreadInterface $thread)
throw new AccessDeniedException('You are not allowed to delete this thread');
}
$thread->setIsDeletedByParticipant($this->getAuthenticatedParticipant(), false);

$this->dispatcher->dispatch(OrnicarMessageEvents::POST_UNDELETE, new ThreadEvent($thread));
}

/**
Expand Down
34 changes: 34 additions & 0 deletions Event/MessageEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Ornicar\MessageBundle\Event;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\Event;
use Ornicar\MessageBundle\Model\MessageInterface;
use Ornicar\MessageBundle\Model\ThreadInterface;

class MessageEvent extends ThreadEvent
{
/**
* The message
* @var MessageInterface
*/
private $message;

public function __construct(MessageInterface $message)
{
parent::__construct($message->getThread());

$this->message = $message;
}

/**
* Returns the message
*
* @return MessageInterface
*/
public function getMessage()
{
return $this->message;
}
}
49 changes: 49 additions & 0 deletions Event/OrnicarMessageEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Ornicar\MessageBundle\Event;

/**
* Declares all events thrown in the MessageBundle
*/
final class OrnicarMessageEvents
{
/**
* The POST_SEND event occurs after a message has been sent
* The event is an instance of Ornicar\MessageBundle\Event\MessageEvent
*
* @var string
*/
const POST_SEND = 'ornicar_message.post_send';

/**
* The POST_DELETE event occurs after a thread has been marked as deleted
* The event is an instance of Ornicar\MessageBundle\Event\ThreadEvent
*
* @var string
*/
const POST_DELETE = 'ornicar_message.post_delete';

/**
* The POST_UNDELETE event occurs after a thread has been marked as undeleted
* The event is an instance of Ornicar\MessageBundle\Event\ThreadEvent
*
* @var string
*/
const POST_UNDELETE = 'ornicar_message.post_undelete';

/**
* The POST_READ event occurs after a thread has been marked as read
* The event is an instance of Ornicar\MessageBundle\Event\ReadableEvent
*
* @var string
*/
const POST_READ = 'ornicar_message.post_read';

/**
* The POST_UNREAD event occurs after a thread has been unread
* The event is an instance of Ornicar\MessageBundle\Event\ReadableEvent
*
* @var string
*/
const POST_UNREAD = 'ornicar_message.post_unread';
}
31 changes: 31 additions & 0 deletions Event/ReadableEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Ornicar\MessageBundle\Event;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\Event;
use Ornicar\MessageBundle\Model\ReadableInterface;

class ReadableEvent extends Event
{
/**
* The readable
* @var ReadableInterface
*/
private $readable;

public function __construct(ReadableInterface $readable)
{
$this->readable = $readable;
}

/**
* Returns the readable
*
* @return ReadableInterface
*/
public function getReadable()
{
return $this->readable;
}
}
31 changes: 31 additions & 0 deletions Event/ThreadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Ornicar\MessageBundle\Event;

use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\EventDispatcher\Event;
use Ornicar\MessageBundle\Model\ThreadInterface;

class ThreadEvent extends Event
{
/**
* The thread
* @var ThreadInterface
*/
private $thread;

public function __construct(ThreadInterface $thread)
{
$this->thread = $thread;
}

/**
* Returns the thread
*
* @return ThreadInterface
*/
public function getThread()
{
return $this->thread;
}
}
17 changes: 16 additions & 1 deletion Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Ornicar\MessageBundle\Security\ParticipantProviderInterface;
use Ornicar\MessageBundle\Model\ReadableInterface;
use Ornicar\MessageBundle\ModelManager\ReadableManagerInterface;
use Ornicar\MessageBundle\Event\OrnicarMessageEvents;
use Ornicar\MessageBundle\Event\ReadableEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Marks messages and threads as read or unread
Expand All @@ -27,10 +30,18 @@ class Reader implements ReaderInterface
*/
protected $readableManager;

public function __construct(ParticipantProviderInterface $participantProvider, ReadableManagerInterface $readableManager)
/**
* The event dispatcher
*
* @var EventDispatcherInterface
*/
protected $dispatcher;

public function __construct(ParticipantProviderInterface $participantProvider, ReadableManagerInterface $readableManager, EventDispatcherInterface $dispatcher)
{
$this->participantProvider = $participantProvider;
$this->readableManager = $readableManager;
$this->dispatcher = $dispatcher;
}

/**
Expand All @@ -41,6 +52,8 @@ public function __construct(ParticipantProviderInterface $participantProvider, R
public function markAsRead(ReadableInterface $readable)
{
$this->readableManager->markAsReadByParticipant($readable, $this->getAuthenticatedParticipant());

$this->dispatcher->dispatch(OrnicarMessageEvents::POST_READ, new ReadableEvent($readable));
}

/**
Expand All @@ -51,6 +64,8 @@ public function markAsRead(ReadableInterface $readable)
public function markAsUnread(ReadableInterface $readable)
{
$this->readableManager->markAsReadByParticipant($readable, $this->getAuthenticatedParticipant());

$this->dispatcher->dispatch(OrnicarMessageEvents::POST_UNREAD, new ReadableEvent($readable));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<service id="ornicar_message.sender.default" class="Ornicar\MessageBundle\Sender\Sender" public="false">
<argument type="service" id="ornicar_message.message_manager" />
<argument type="service" id="ornicar_message.thread_manager" />
<argument type="service" id="event_dispatcher" />
</service>

<service id="ornicar_message.composer.default" class="Ornicar\MessageBundle\Composer\Composer" public="false">
Expand All @@ -35,16 +36,19 @@
<service id="ornicar_message.message_reader.default" class="Ornicar\MessageBundle\Reader\Reader" public="false">
<argument type="service" id="ornicar_message.participant_provider" />
<argument type="service" id="ornicar_message.message_manager" />
<argument type="service" id="event_dispatcher" />
</service>

<service id="ornicar_message.thread_reader.default" class="Ornicar\MessageBundle\Reader\Reader" public="false">
<argument type="service" id="ornicar_message.participant_provider" />
<argument type="service" id="ornicar_message.thread_manager" />
<argument type="service" id="event_dispatcher" />
</service>

<service id="ornicar_message.deleter.default" class="Ornicar\MessageBundle\Deleter\Deleter" public="false">
<argument type="service" id="ornicar_message.authorizer" />
<argument type="service" id="ornicar_message.participant_provider" />
<argument type="service" id="event_dispatcher" />
</service>

<service id="ornicar_message.twig_extension" class="Ornicar\MessageBundle\Twig\Extension\MessageExtension" public="false">
Expand Down
9 changes: 9 additions & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,15 @@ Your class must implement ``Ornicar\MessageBundle\Security\AuthorizerInterface``
You can tell whether the user can see or delete a thread, and if he can send a new message to another user.
See the default implementation in ``Ornicar\MessageBundle\Security\Authorizer``.


Listening to events
===================

This bundles dispatches event when notable actions are performed.

See ``Ornicar\MessageBundle\Event\OrnicarMessageEvents`` for a documented
list of the available events.

Configuration
=============

Expand Down
15 changes: 14 additions & 1 deletion Sender/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Ornicar\MessageBundle\ModelManager\MessageManagerInterface;
use Ornicar\MessageBundle\ModelManager\ThreadManagerInterface;
use Ornicar\MessageBundle\Model\MessageInterface;
use Ornicar\MessageBundle\Event\MessageEvent;
use Ornicar\MessageBundle\Event\OrnicarMessageEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Sends messages
Expand All @@ -27,10 +30,18 @@ class Sender implements SenderInterface
*/
protected $threadManager;

public function __construct(MessageManagerInterface $messageManager, ThreadManagerInterface $threadManager)
/**
* The event dispatcher
*
* @var EventDispatcherInterface
*/
protected $dispatcher;

public function __construct(MessageManagerInterface $messageManager, ThreadManagerInterface $threadManager, EventDispatcherInterface $dispatcher)
{
$this->messageManager = $messageManager;
$this->threadManager = $threadManager;
$this->dispatcher = $dispatcher;
}

/**
Expand All @@ -42,5 +53,7 @@ public function send(MessageInterface $message)
{
$this->threadManager->saveThread($message->getThread(), false);
$this->messageManager->saveMessage($message);

$this->dispatcher->dispatch(OrnicarMessageEvents::POST_SEND, new MessageEvent($message));
}
}

0 comments on commit 7e533f0

Please sign in to comment.