I want to create a voting system, where multiple domain objects can be voted on:
- a calendar event
- a comment
- a user
So I figured I would create a Voteable
interface for these items:
interface Voteable
{
public function vote( User $user, $value );
}
I thought this vote
method would proxy a repository method, something like:
class VotingRepository
{
public function castVote( Voteable $item, User $user, $value )
{
// save the these values, along with the value
$itemId = $item->getId();
$userId = $user->getId();
}
}
For now, the repository will be a database. This database will have linking tables for each type of vote:
- eventVote
- commentVote
- userVote
So, this essentially means that each domain object needs another table to cast the votes to. Would this be a good candidate for a factory? A VotingRepositoryFactory
in this case? In other words something like:
class VotingRepositoryFactory
{
createVotingRepository( $type )
{
switch( $type )
{
case 'event':
// create a voting repository with EventVote table
return new VotingRepository( new EventVoteTable() );
case 'comment':
// create a voting repository with CommentVote table
return new VotingRepository( new CommentVoteTable() );
case 'user':
// create a voting repository with UserVote table
return new VotingRepository( new UserVoteTable() );
}
}
}
Then, tying it all together, from within the domain objects (comment in this case for example), I would look something like this:
class Comment implements Voteable
{
public function construct()
{
$this->_repository = VotingRepositoryFactory::createVotingRepository( 'comment' );
}
public function vote( User $user, $value )
{
$this->_repository->castVote( $this, $user, $value );
}
}
Does this make sense?