Skip to content

Instantly share code, notes, and snippets.

@Addvilz
Forked from antonioribeiro/gist:c10958178f7dc9ea8cb8
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Addvilz/f3e3264ade39e88e6069 to your computer and use it in GitHub Desktop.
Save Addvilz/f3e3264ade39e88e6069 to your computer and use it in GitHub Desktop.
<?php
/**
* I would rather solve this problem by having a Entity, a dumb business model that contains
* all the data, is dumb (yep), has no logic and represents a data type.
*
* Populate it once and reuse the type where applicable, as it will give many advantages against
* having to juggle around bazillion of separate variables.
*
* a) your business logic and data will be clearly separated - SOC;
* b) KISS DRY;
* c) you will gain consistency;
* d) this will be a lot easier to test;
* e) codebase will be a lot cleaner and easier to maintain;
*/
// Form Request (Validation)
class Update extends FormRequest
{
public function rules()
{
return [
// whatever
];
}
}
// Your entity, model, business object
// http://en.wikipedia.org/wiki/Business_object
class Event // ?? extends Eloquent
{
private $id;
private $user;
private $client_id;
private $office_room_id;
private $title;
private $description;
private $start;
private $end;
private $updated_start;
private $updated_end;
private $full;
private $color;
// Getters, setters, things...?
}
// Controller
class Controller
{
//...
public function update(UpdateRequest $request)
{
$event = new Event();
// OR
$event = Event::findOrFail($someId);
// populate Event via Eloquent mass populate or something similar
$this->execute(UpdateEventCommand::class, $event);
$this->flash->message('paragraphs.event-updated');
return back();
}
}
// Command
class UpdateEventCommand extends SelfHandlingCommand
{
private $event;
/**
* @param Event $event Ensures only "Event" domain object is received by this command as it is not
* nor should be capable of processing anything else than "Event"
*/
public function __construct(Event $event)
{
$this->event = $event;
}
public function handle(EventRepository $repository)
{
// State is more or less clear - $this->event will always have Event type content
// unless modified within $this
return $repository->updateEvent($this->event);
}
}
// Repository
class EventRepository
{
/**
* @param Event $event Again, only "Event" is accepted, as we can not process anything else.
*/
public function updateEvent(Event $event)
{
//...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment