vendor/payum/core/Payum/Core/Gateway.php line 107

Open in your IDE?
  1. <?php
  2. namespace Payum\Core;
  3. use Payum\Core\Action\ActionInterface;
  4. use Payum\Core\Exception\LogicException;
  5. use Payum\Core\Exception\RequestNotSupportedException;
  6. use Payum\Core\Exception\UnsupportedApiException;
  7. use Payum\Core\Extension\Context;
  8. use Payum\Core\Extension\ExtensionCollection;
  9. use Payum\Core\Extension\ExtensionInterface;
  10. use Payum\Core\Reply\ReplyInterface;
  11. class Gateway implements GatewayInterface
  12. {
  13.     /**
  14.      * @var Action\ActionInterface[]
  15.      */
  16.     protected $actions;
  17.     /**
  18.      * @var mixed[]
  19.      */
  20.     protected $apis;
  21.     /**
  22.      * @var \Payum\Core\Extension\ExtensionCollection
  23.      */
  24.     protected $extensions;
  25.     /**
  26.      * @var Context[]
  27.      */
  28.     protected $stack;
  29.     /**
  30.      */
  31.     public function __construct()
  32.     {
  33.         $this->stack = [];
  34.         $this->actions = [];
  35.         $this->apis = [];
  36.         $this->extensions = new ExtensionCollection();
  37.     }
  38.     /**
  39.      * @param mixed $api
  40.      * @param bool  $forcePrepend
  41.      *
  42.      * @return void
  43.      */
  44.     public function addApi($api$forcePrepend false)
  45.     {
  46.         $forcePrepend ?
  47.             array_unshift($this->apis$api) :
  48.             array_push($this->apis$api)
  49.         ;
  50.     }
  51.     /**
  52.      * @param Action\ActionInterface $action
  53.      * @param bool                   $forcePrepend
  54.      *
  55.      * @return void
  56.      */
  57.     public function addAction(ActionInterface $action$forcePrepend false)
  58.     {
  59.         $forcePrepend ?
  60.             array_unshift($this->actions$action) :
  61.             array_push($this->actions$action)
  62.         ;
  63.     }
  64.     /**
  65.      * @param \Payum\Core\Extension\ExtensionInterface $extension
  66.      * @param bool                                     $forcePrepend
  67.      *
  68.      * @return void
  69.      */
  70.     public function addExtension(ExtensionInterface $extension$forcePrepend false)
  71.     {
  72.         $this->extensions->addExtension($extension$forcePrepend);
  73.     }
  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     public function execute($request$catchReply false)
  78.     {
  79.         $context = new Context($this$request$this->stack);
  80.         array_push($this->stack$context);
  81.         try {
  82.             $this->extensions->onPreExecute($context);
  83.             if (false == $context->getAction()) {
  84.                 if (false == $action $this->findActionSupported($context->getRequest())) {
  85.                     throw RequestNotSupportedException::create($context->getRequest());
  86.                 }
  87.                 $context->setAction($action);
  88.             }
  89.             $this->extensions->onExecute($context);
  90.             $context->getAction()->execute($request);
  91.             $this->extensions->onPostExecute($context);
  92.             array_pop($this->stack);
  93.         } catch (ReplyInterface $reply) {
  94.             $context->setReply($reply);
  95.             $this->extensions->onPostExecute($context);
  96.             array_pop($this->stack);
  97.             if ($catchReply && $context->getReply()) {
  98.                 return $context->getReply();
  99.             }
  100.             if ($context->getReply()) {
  101.                 throw $context->getReply();
  102.             }
  103.         } catch (\Exception $e) {
  104.             $context->setException($e);
  105.             $this->onPostExecuteWithException($context);
  106.         }
  107.         return;
  108.     }
  109.     protected function onPostExecuteWithException(Context $context)
  110.     {
  111.         array_pop($this->stack);
  112.         $exception $context->getException();
  113.         try {
  114.             $this->extensions->onPostExecute($context);
  115.         } catch (\Exception $e) {
  116.             // logic is similar to one in Symfony's ExceptionListener::onKernelException
  117.             $wrapper $e;
  118.             while ($prev $wrapper->getPrevious()) {
  119.                 if ($exception === $wrapper $prev) {
  120.                     throw $e;
  121.                 }
  122.             }
  123.             $prev = new \ReflectionProperty('Exception''previous');
  124.             $prev->setAccessible(true);
  125.             $prev->setValue($wrapper$exception);
  126.             throw $e;
  127.         }
  128.         if ($context->getException()) {
  129.             throw $context->getException();
  130.         }
  131.     }
  132.     /**
  133.      * @param mixed $request
  134.      *
  135.      * @return ActionInterface|false
  136.      */
  137.     protected function findActionSupported($request)
  138.     {
  139.         foreach ($this->actions as $action) {
  140.             if ($action instanceof GatewayAwareInterface) {
  141.                 $action->setGateway($this);
  142.             }
  143.             if ($action instanceof ApiAwareInterface) {
  144.                 $apiSet false;
  145.                 $unsupportedException null;
  146.                 foreach ($this->apis as $api) {
  147.                     try {
  148.                         $action->setApi($api);
  149.                         $apiSet true;
  150.                         break;
  151.                     } catch (UnsupportedApiException $e) {
  152.                         $unsupportedException $e;
  153.                     }
  154.                 }
  155.                 if (false == $apiSet) {
  156.                     throw new LogicException(sprintf('Cannot find right api for the action %s'get_class($action)), 0$unsupportedException);
  157.                 }
  158.             }
  159.             if ($action->supports($request)) {
  160.                 return $action;
  161.             }
  162.         }
  163.         return false;
  164.     }
  165. }