Last active
August 27, 2020 08:18
-
-
Save syntheticsh/d918f490245cbc1f0ee6bd10c6603e7f to your computer and use it in GitHub Desktop.
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 | |
| $withTrans = function($trans, $work): bool { | |
| if ($work()) { | |
| $trans->commit(); | |
| return true; | |
| } else { | |
| $trans->rollback(); | |
| return false; | |
| } | |
| }; | |
| $work = function($myWork1, $myWork2) { | |
| return function() use ($mywork1, $myWork2): bbol { | |
| if ($data = $myWork1()) { | |
| return $myWork2($data); | |
| } | |
| return false; | |
| }; | |
| }; | |
| $myWork1 = function($data1) { | |
| return function() use ($data1): array { | |
| //do work... | |
| return []; | |
| }; | |
| }; | |
| $myWork2 = function($data2) { | |
| //do work | |
| return function($moreData) use ($data2): bool { | |
| //more work | |
| return true; | |
| }; | |
| }; | |
| $withRepo1 = function($repo1, $myWork1) { | |
| return function() use ($repo1, $myWork1): array { | |
| $data = $myWork1($repo1->get()); | |
| $repo1->save($data); | |
| return $data; | |
| }; | |
| }; | |
| $withRepo2 = function($repo2, $myWork2) { | |
| return function($moreData) use ($repo2, $myWork2): bool { | |
| $res = $myWork2($repo2->get())($moreData); | |
| $repo2->save($res) | |
| return $res; | |
| } | |
| }; | |
| $withTrans( | |
| Trans::begin(), | |
| $work( | |
| $withRepo1(new Repo1(), $myWork1), | |
| $withRepo2(new Repo2(), $myWork2) | |
| ) | |
| ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment