Skip to content

Instantly share code, notes, and snippets.

@tonysm
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save tonysm/f26fcc8d2bf3f8f9ad55 to your computer and use it in GitHub Desktop.

Select an option

Save tonysm/f26fcc8d2bf3f8f9ad55 to your computer and use it in GitHub Desktop.
RepositoryPattern
<?php
interface EventGenerator
{
public function raise(DomainEvent $event);
public function releaseEvents();
}
class Entity extends \Eloquent implements EventGenerator
{
protected $domainEvents = [];
public function raise(DomainEvent $event)
{
$this->domainEvents[] = $event;
}
public function releaseEvents()
{
return $this->domainEvents;
}
}
class GroupCreated extends DomainEvent
{
function __construct($groupId, $groupName, $groupType, $creatorId) {}
}
class User extends Entity
{
public function groups()
{
return $this->hasMany("Group");
}
}
class Group extends Entity
{
protected $fillable = ["name", "type"];
public function owner()
{
return $this->belongsTo("User", "user_id");
}
public static function createForUser(User $user, $name, $type)
{
$group = $user->groups()->create([
"name" => $name,
"type" => $type
]);
$group->raise(new GroupCreated(
$group->id,
$group->name,
$group->type,
$this->id
));
return $group;
}
}
class CreateNewGroupCommand
{
public $groupName;
public $groupType;
public $user;
function __construct($data)
{
$this->groupName = $data["name"];
$this->groupType = $data["type"];
$this->user = $data["user"];
}
}
class CreateNewGroupCommandHandler
{
private $dispatcher;
function __construct(DomainEventsDispatcher $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function handle($command)
{
$group = Group::creatForUser(
$command->user,
$command->groupName,
$command->groupType
);
$this->dispatcher->dispatchFor($group);
return $group;
}
}
class GroupsController extends BaseController
{
use Commander;
public function store()
{
$input = array_add(Input::only("name", "type"), "user", Auth::user());
// binds the command to its handler
$group = $this->execute(new CreateNewGroupCommand($input));
return Redirect::route("groups.view", $group->id);
}
}
<?php
interface GroupRepository
{
public function createNewGroup($name, $type, User $user);
}
class EloquentGroupRepository implements GroupRepository
{
public function createNewGroup($name, $type, User $user)
{
$group = $user->group()->create([
"name" => $name,
"type" => $type
]);
$group->raise(new GroupCreated(
$group->id,
$group->name,
$group->type,
$user->id
));
return $group;
}
}
class GroupCreated extends DomainEvent
{
function __construct($groupId, $groupName, $groupType, $creatorId)
{}
}
interface EventGenerator
{
public function raise(DomainEvent $event);
public function releaseEvents();
}
class Entity extends \Eloquent implements EventGenerator
{
public function raise(DomainEvent $event) {}
public function releaseEvents() {}
}
class User extends Entity
{
public function groups()
{
return $this->hasMany("Group");
}
}
class Group extends Entity
{
protected $fillable = ["name", "type"];
public function owner()
{
return $this->belongsTo("User", "user_id");
}
}
class CreateNewGroupCommand
{
public $groupName;
public $groupType;
public $user;
function __construct($data)
{
$this->groupName = $data["name"];
$this->groupType = $data["type"];
$this->user = $data["user"];
}
}
class CreateNewGroupCommandHandler
{
private $groupRepo;
private $dispatcher;
function __construct(GroupRepository $groupRepo, DomainEventsDispatcher $dispatcher)
{
$this->groupRepo = $groupRepo;
$this->dispatcher = $dispatcher;
}
public function handle($command)
{
$group = $this->groupRepo->createNewGroup(
$command->groupName,
$command->groupType,
$command->user
);
$this->dispatcher->dispatchFor($group);
return $group;
}
}
class GroupsController extends BaseController
{
use Commander;
public function store()
{
$input = array_add(Input::only("name", "type"), "user", Auth::user());
// binds the command to its handler
$group = $this->execute(new CreateNewGroupCommand($input));
return Redirect::route("groups.view", $group->id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment