Skip to content

Commit

Permalink
Move duplicate model methods to base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikola committed Jan 19, 2012
1 parent 03e311c commit 871d8bc
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 475 deletions.
76 changes: 2 additions & 74 deletions Document/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@

namespace Ornicar\MessageBundle\Document;

use Doctrine\Common\Collections\ArrayCollection;
use Ornicar\MessageBundle\Model\Message as AbstractMessage;
use Ornicar\MessageBundle\Model\ParticipantInterface;
use Ornicar\MessageBundle\Model\Message as BaseMessage;

abstract class Message extends AbstractMessage
abstract class Message extends BaseMessage
{
/**
* Message metadata
*
* @var Collection of MessageMetadata
*/
protected $metadata;

/**
* Tells if the message is spam or flood
* This denormalizes Thread.isSpam
Expand All @@ -31,46 +22,6 @@ abstract class Message extends AbstractMessage
*/
protected $unreadForParticipants = array();

/**
* Initializes the collections
*/
public function __construct()
{
parent::__construct();
$this->metadata = new ArrayCollection();
}

/**
* Tells if this participant has read this message
*
* @param ParticipantInterface $participant
* @return boolean
*/
public function isReadByParticipant(ParticipantInterface $participant)
{
if ($meta = $this->getMetadataForParticipant($participant)) {
return $meta->getIsRead();
}

return false;
}

/**
* Sets whether or not this participant has read this message
*
* @param ParticipantInterface $participant
* @param boolean $isRead
* @throws InvalidArgumentException if no metadata exists for the participant
*/
public function setIsReadByParticipant(ParticipantInterface $participant, $isRead)
{
if (!$meta = $this->getMetadataForParticipant($participant)) {
throw new \InvalidArgumentException(sprintf('No metadata exists for participant with id "%s"', $participant->getId()));
}

$meta->setIsRead($isRead);
}

/**
* @param boolean
* @return null
Expand All @@ -80,29 +31,6 @@ public function setIsSpam($isSpam)
$this->isSpam = (boolean) $isSpam;
}

/**
* @param ParticipantInterface $participant
* @return MessageMetadata
*/
public function getMetadataForParticipant(ParticipantInterface $participant)
{
foreach ($this->metadata as $meta) {
if ($meta->getParticipant()->getId() == $participant->getId()) {
return $meta;
}
}

return null;
}

/**
* @param MessageMetadata $meta
*/
public function addMetadata(MessageMetadata $meta)
{
$this->metadata->add($meta);
}

/**
* DENORMALIZATION
*
Expand Down
166 changes: 1 addition & 165 deletions Document/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,13 @@

abstract class Thread extends AbstractThread
{
/**
* Messages contained in this thread
*
* @var Collection of MessageInterface
*/
protected $messages;

/**
* Thread metadata
*
* @var Collection of ThreadMetadata
*/
protected $metadata;

/**
* Users participating in this conversation
*
* @var Collection of ParticipantInterface
*/
protected $participants;

/**
* Participant that created the thread
*
* @var ParticipantInterface
*/
protected $createdBy;

/**
* Date this thread was created at
*
* @var \DateTime
*/
protected $createdAt;

/**
* Date that the last message in this thread was created at
*
* This denormalization field is used for sorting threads in the inbox and
* sent list.
*
* @var \DateTime
* @var DateTime
*/
protected $lastMessageDate;

Expand Down Expand Up @@ -89,75 +54,6 @@ abstract class Thread extends AbstractThread
*/
protected $activeSenders = array();

/**
* Initializes the collections
*/
public function __construct()
{
$this->messages = new ArrayCollection();
$this->metadata = new ArrayCollection();
$this->participants = new ArrayCollection();
}

/**
* Gets the messages contained in the thread
*
* @return array of MessageInterface
*/
public function getMessages()
{
return $this->messages->toArray();
}

/**
* Adds a new message to the thread
*
* @param MessageInterface $message
*/
public function addMessage(MessageInterface $message)
{
$this->messages->add($message);
}

/**
* Gets the participant that created the thread
* Generally the sender of the first message
*
* @return ParticipantInterface
*/
public function getCreatedBy()
{
return $this->createdBy;
}

/**
* Sets the participant that created the thread
* Generally the sender of the first message
*
* @param ParticipantInterface
*/
public function setCreatedBy(ParticipantInterface $participant)
{
$this->createdBy = $participant;
}

/**
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
* @param \DateTime
* @return null
*/
public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
}

/**
* Gets the users participating in this conversation
*
Expand Down Expand Up @@ -193,66 +89,6 @@ public function isParticipant(ParticipantInterface $participant)
return $this->participants->contains($participant);
}

/**
* Tells if this thread is deleted by this participant
*
* @return bool
*/
public function isDeletedByParticipant(ParticipantInterface $participant)
{
if ($meta = $this->getMetadataForParticipant($participant)) {
return $meta->getIsDeleted();
}

return false;
}

/**
* Sets whether or not this participant has deleted this thread
*
* @param ParticipantInterface $participant
* @param boolean $isDeleted
* @throws InvalidArgumentException if no metadata exists for the participant
*/
public function setIsDeletedByParticipant(ParticipantInterface $participant, $isDeleted)
{
if (!$meta = $this->getMetadataForParticipant($participant)) {
throw new \InvalidArgumentException(sprintf('No metadata exists for participant with id "%s"', $participant->getId()));
}

$meta->setIsDeleted($isDeleted);

if ($isDeleted) {
// also mark all thread messages as read
foreach ($this->getMessages() as $message) {
$message->setIsReadByParticipant($participant, true);
}
}
}

/**
* @param ParticipantInterface $participant
* @return ThreadMetadata
*/
public function getMetadataForParticipant(ParticipantInterface $participant)
{
foreach ($this->metadata as $meta) {
if ($meta->getParticipant()->getId() == $participant->getId()) {
return $meta;
}
}

return null;
}

/**
* @param ThreadMetadata $meta
*/
public function addMetadata(ThreadMetadata $meta)
{
$this->metadata->add($meta);
}

/**
* DENORMALIZATION
*
Expand Down
65 changes: 7 additions & 58 deletions Entity/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,27 @@

namespace Ornicar\MessageBundle\Entity;

use Doctrine\Common\Collections\Collection;
use Ornicar\MessageBundle\Model\Message as BaseMessage;
use Ornicar\MessageBundle\Model\ParticipantInterface;
use Doctrine\Common\Collections\ArrayCollection;

abstract class Message extends BaseMessage
{
/**
* Message metadata
* Get the collection of MessageMetadata.
*
* @var Collection of MessageMetadata
* @return Collection
*/
protected $metadata;

/**
* Initializes the collections
*/
public function __construct()
{
parent::__construct();
$this->metadata = new ArrayCollection();
}

/**
* Tells if this participant has read this message
*
* @param ParticipantInterface $participant
* @return boolean
*/
public function isReadByParticipant(ParticipantInterface $participant)
{
if ($meta = $this->getMetadataForParticipant($participant)) {
return $meta->getIsRead();
}

return false;
}

/**
* Sets whether or not this participant has read this message
*
* @param ParticipantInterface $participant
* @param boolean $isRead
* @throws InvalidArgumentException if no metadata exists for the participant
*/
public function setIsReadByParticipant(ParticipantInterface $participant, $isRead)
{
if (!$meta = $this->getMetadataForParticipant($participant)) {
throw new \InvalidArgumentException(sprintf('No metadata exists for participant with id "%s"', $participant->getId()));
}

$meta->setIsRead($isRead);
}

public function getAllMetadata()
{
return $this->metadata;
}

public function getMetadataForParticipant($participant)
{
foreach ($this->metadata as $meta) {
if ($meta->getParticipant()->getId() == $participant->getId()) {
return $meta;
}
}

return null;
}

/**
* @see Ornicar\MessageBundle\Model\Message::addMetadata()
*/
public function addMetadata(MessageMetadata $meta)
{
$meta->setMessage($this);
$this->metadata->add($meta);
parent::addMetadata($meta);
}
}
Loading

0 comments on commit 871d8bc

Please sign in to comment.