Last active
August 29, 2015 14:07
-
-
Save liuggio/5882c1b259ca6ff87734 to your computer and use it in GitHub Desktop.
Alternative approach using the invoke magic call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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