4 * This file is part of the Prophecy.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6 * Marcello Duarte <marcello.duarte@gmail.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Prophecy\Promise
;
14 use Prophecy\Prophecy\ObjectProphecy
;
15 use Prophecy\Prophecy\MethodProphecy
;
16 use Prophecy\Exception\InvalidArgumentException
;
22 * @author Konstantin Kudryashov <ever.zet@gmail.com>
24 class CallbackPromise
implements PromiseInterface
29 * Initializes callback promise.
31 * @param callable $callback Custom callback
33 * @throws \Prophecy\Exception\InvalidArgumentException
35 public function __construct($callback)
37 if (!is_callable($callback)) {
38 throw new InvalidArgumentException(sprintf(
39 'Callable expected as an argument to CallbackPromise, but got %s.',
44 $this->callback
= $callback;
48 * Evaluates promise callback.
51 * @param ObjectProphecy $object
52 * @param MethodProphecy $method
56 public function execute(array $args, ObjectProphecy
$object, MethodProphecy
$method)
58 $callback = $this->callback
;
60 if ($callback instanceof Closure
&& method_exists('Closure', 'bind')) {
61 $callback = Closure
::bind($callback, $object);
64 return call_user_func($callback, $args, $object, $method);