Skip to content

Instantly share code, notes, and snippets.

@liuggio
Last active August 29, 2015 14:07
Show Gist options
  • Save liuggio/5882c1b259ca6ff87734 to your computer and use it in GitHub Desktop.
Save liuggio/5882c1b259ca6ff87734 to your computer and use it in GitHub Desktop.
Alternative approach using the invoke magic call.
<?php
/**
* We are used to create Commands/UseCases with a single method,
* is very common to create the name of the method with `execute`
* `$doSomething->execute($thisDay)`
* or to create the method name repeating the expressive class name:
* `$doSomething->doSomething($thisDay)`.
*
* This gist uses the magic call invoke
* in order to be more expressive:
* `$doSomething($thisDay)`
*/
class DoSomethingThisDay
{
public function __construct($collaborator)
{
// ... store collaborators into vars
}
/**
* @param DayCommand $thisDay
*
* @return ActionView
*/
public function __invoke(DayCommand $thisDay)
{
$you = You::createWithLazyBeing();
if ($thisDay->isRainyDay()) {
$you->doSleep();
return new ActionView("sleep");
}
$you->watchTelevision();
return new ActionView("tv");
}
}
$thisDay = Day::createARainyDay();
$collaborator = // something like a factory, service repository...
$doSomething = new DoSomethingThisDay($collaborator);
echo $doSomething($thisDay);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment