You are here

class ThrowPromise in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/phpspec/prophecy/src/Prophecy/Promise/ThrowPromise.php \Prophecy\Promise\ThrowPromise

Throw promise.

@author Konstantin Kudryashov <ever.zet@gmail.com>

Hierarchy

Expanded class hierarchy of ThrowPromise

File

vendor/phpspec/prophecy/src/Prophecy/Promise/ThrowPromise.php, line 25

Namespace

Prophecy\Promise
View source
class ThrowPromise implements PromiseInterface {
  private $exception;

  /**
   * @var \Doctrine\Instantiator\Instantiator
   */
  private $instantiator;

  /**
   * Initializes promise.
   *
   * @param string|\Exception $exception Exception class name or instance
   *
   * @throws \Prophecy\Exception\InvalidArgumentException
   */
  public function __construct($exception) {
    if (is_string($exception)) {
      if (!class_exists($exception) && 'Exception' !== $exception && !is_subclass_of($exception, 'Exception')) {
        throw new InvalidArgumentException(sprintf('Exception class or instance expected as argument to ThrowPromise, but got %s.', gettype($exception)));
      }
    }
    elseif (!$exception instanceof \Exception) {
      throw new InvalidArgumentException(sprintf('Exception class or instance expected as argument to ThrowPromise, but got %s.', gettype($exception)));
    }
    $this->exception = $exception;
  }

  /**
   * Throws predefined exception.
   *
   * @param array          $args
   * @param ObjectProphecy $object
   * @param MethodProphecy $method
   *
   * @throws object
   */
  public function execute(array $args, ObjectProphecy $object, MethodProphecy $method) {
    if (is_string($this->exception)) {
      $classname = $this->exception;
      $reflection = new ReflectionClass($classname);
      $constructor = $reflection
        ->getConstructor();
      if ($constructor
        ->isPublic() && 0 == $constructor
        ->getNumberOfRequiredParameters()) {
        throw $reflection
          ->newInstance();
      }
      if (!$this->instantiator) {
        $this->instantiator = new Instantiator();
      }
      throw $this->instantiator
        ->instantiate($classname);
    }
    throw $this->exception;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ThrowPromise::$exception private property
ThrowPromise::$instantiator private property
ThrowPromise::execute public function Throws predefined exception. Overrides PromiseInterface::execute
ThrowPromise::__construct public function Initializes promise.