Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Created April 22, 2015 08:30
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 StanAngeloff/379f9737c2aa45106822 to your computer and use it in GitHub Desktop.
Save StanAngeloff/379f9737c2aa45106822 to your computer and use it in GitHub Desktop.
The MIT License (MIT)
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
<?php
/**
* (c) PSP UK Group Ltd. <hello@psp-group.co.uk>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/
namespace Psp\Fluent;
/**
* A fluent operation interface.
*
* Acts much like a promise by performing an operation and executing configured callables.
*
* @see http://en.wikipedia.org/wiki/Fluent_interface
*/
final class Operation
{
/**
* @var array
*/
private $parts = array();
/**
* Prevent new instances created outside of the class itself.
*
* @param callable $when
*/
private function __construct(callable $when)
{
$this->addPart('when', $when);
}
/**
* Create a new Operation instance.
*
* @param callable $when The callable to invoke as part of the operation.
*
* @return static
*/
public static function when(callable $when)
{
return new static($when);
}
/**
* Add a callable to the internal collection.
*
* @param string $partName
* @param callable $callable
*
* @return void
*/
private function addPart($partName, callable $callable)
{
$this->parts[$partName][] = $callable;
}
/**
* Invoke configured callables.
*
* @param string $partName
*
* @return mixed The result of the last callable.
*/
private function invokePart($partName)
{
if (( ! isset ($this->parts[$partName]))) {
return null;
}
$lastResult = null;
foreach ($this->parts[$partName] as $callable) {
$lastResult = call_user_func($callable);
}
return $lastResult;
}
/**
* Add a callable to be invoked immediately before the operation.
*
* @param callable $before
*
* @return $this
*/
public function before(callable $before)
{
$this->addPart('before', $before);
return $this;
}
/**
* Add a callable to be invoked if the operation succeeds.
*
* @param callable $then
*
* @return $this
*/
public function then(callable $then)
{
$this->addPart('then', $then);
return $this;
}
/**
* Add a callable to be invoked if the operation fails.
*
* @param callable $otherwise
*
* @return $this
*/
public function otherwise(callable $otherwise)
{
$this->addPart('otherwise', $otherwise);
return $this;
}
/**
* Run the operation and any configured callables.
*
* @return mixed The result of the operation.
*/
public function complete()
{
$this->invokePart('before');
try {
$result = $this->invokePart('when');
$this->invokePart('then');
return $result;
} catch (\Exception $exception) {
try {
$this->invokePart('otherwise');
} catch (\Exception $ignore) {
}
throw $exception;
}
}
}
<?php
Fluent\Operation::when(
function () {
// do work...
}
)->before(
function () {
// start a transaction
}
)->then(
function () {
// commit the transaction
}
)->otherwise(
function () {
// rollback the transaction
}
)->complete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment