/vendor/phpspec/prophecy/src/Prophecy/Promise/ThrowPromise.php

https://bitbucket.org/alan_cordova/api-sb-map · PHP · 99 lines · 49 code · 13 blank · 37 comment · 10 complexity · cb16ee4e1d47678f92b6588dcded4f03 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Prophecy.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. * Marcello Duarte <marcello.duarte@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Prophecy\Promise;
  11. use Doctrine\Instantiator\Instantiator;
  12. use Prophecy\Prophecy\ObjectProphecy;
  13. use Prophecy\Prophecy\MethodProphecy;
  14. use Prophecy\Exception\InvalidArgumentException;
  15. use ReflectionClass;
  16. /**
  17. * Throw promise.
  18. *
  19. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  20. */
  21. class ThrowPromise implements PromiseInterface
  22. {
  23. private $exception;
  24. /**
  25. * @var \Doctrine\Instantiator\Instantiator
  26. */
  27. private $instantiator;
  28. /**
  29. * Initializes promise.
  30. *
  31. * @param string|\Exception|\Throwable $exception Exception class name or instance
  32. *
  33. * @throws \Prophecy\Exception\InvalidArgumentException
  34. */
  35. public function __construct($exception)
  36. {
  37. if (is_string($exception)) {
  38. if (!class_exists($exception) || !$this->isAValidThrowable($exception)) {
  39. throw new InvalidArgumentException(sprintf(
  40. 'Exception / Throwable class or instance expected as argument to ThrowPromise, but got %s.',
  41. $exception
  42. ));
  43. }
  44. } elseif (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
  45. throw new InvalidArgumentException(sprintf(
  46. 'Exception / Throwable class or instance expected as argument to ThrowPromise, but got %s.',
  47. is_object($exception) ? get_class($exception) : gettype($exception)
  48. ));
  49. }
  50. $this->exception = $exception;
  51. }
  52. /**
  53. * Throws predefined exception.
  54. *
  55. * @param array $args
  56. * @param ObjectProphecy $object
  57. * @param MethodProphecy $method
  58. *
  59. * @throws object
  60. */
  61. public function execute(array $args, ObjectProphecy $object, MethodProphecy $method)
  62. {
  63. if (is_string($this->exception)) {
  64. $classname = $this->exception;
  65. $reflection = new ReflectionClass($classname);
  66. $constructor = $reflection->getConstructor();
  67. if ($constructor->isPublic() && 0 == $constructor->getNumberOfRequiredParameters()) {
  68. throw $reflection->newInstance();
  69. }
  70. if (!$this->instantiator) {
  71. $this->instantiator = new Instantiator();
  72. }
  73. throw $this->instantiator->instantiate($classname);
  74. }
  75. throw $this->exception;
  76. }
  77. /**
  78. * @param string $exception
  79. *
  80. * @return bool
  81. */
  82. private function isAValidThrowable($exception)
  83. {
  84. return is_a($exception, 'Exception', true) || is_subclass_of($exception, 'Throwable', true);
  85. }
  86. }