Вход Регистрация
Файл: gapps/vendor/phpspec/prophecy/src/Prophecy/Promise/ThrowPromise.php
Строк: 127
<?php

/*
 * This file is part of the Prophecy.
 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
 *     Marcello Duarte <marcello.duarte@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ProphecyPromise;

use 
DoctrineInstantiatorInstantiator;
use 
ProphecyProphecyObjectProphecy;
use 
ProphecyProphecyMethodProphecy;
use 
ProphecyExceptionInvalidArgumentException;
use 
ReflectionClass;

/**
 * Throw promise.
 *
 * @author Konstantin Kudryashov <ever.zet@gmail.com>
 */
class ThrowPromise implements PromiseInterface
{
    private 
$exception;

    
/**
     * @var DoctrineInstantiatorInstantiator
     */
    
private $instantiator;

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

        
$this->exception $exception;
    }

    
/**
     * Throws predefined exception.
     *
     * @param array          $args
     * @param ObjectProphecy $object
     * @param MethodProphecy $method
     *
     * @throws object
     */
    
public function execute(array $argsObjectProphecy $objectMethodProphecy $method)
    {
        if (
is_string($this->exception)) {
            
$classname   $this->exception;
            
$reflection  = new ReflectionClass($classname);
            
$constructor $reflection->getConstructor();

            if (
$constructor->isPublic() && == $constructor->getNumberOfRequiredParameters()) {
                throw 
$reflection->newInstance();
            }

            if (!
$this->instantiator) {
                
$this->instantiator = new Instantiator();
            }

            throw 
$this->instantiator->instantiate($classname);
        }

        throw 
$this->exception;
    }

    
/**
     * @param string $exception
     *
     * @return bool
     */
    
private function isAValidThrowable($exception)
    {
        return 
is_a($exception'Exception'true) || is_subclass_of($exception'Throwable'true);
    }
}
Онлайн: 0
Реклама