Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.57 KB | None | 0 0
  1. <?php
  2. namespace App\Api\v1\Transactions\Verify;
  3.  
  4. use App\Api\v1\Exceptions\NotFoundException;
  5. use App\Api\v1\Exceptions\ParameterNotFoundException;
  6. use App\Entity\Merchants;
  7. use App\Entity\Transactions;
  8. use App\Middleware\VerifyMiddleware;
  9. use Interop\Http\ServerMiddleware\DelegateInterface;
  10. use Psr\Http\Message\ServerRequestInterface;
  11.  
  12. class ExistsMiddleware extends VerifyMiddleware
  13. {
  14.     const URI_TOKEN = 'transactionid';
  15.  
  16.     /**
  17.      * @param ServerRequestInterface $request
  18.      * @param DelegateInterface $delegate
  19.      * @return \Psr\Http\Message\ResponseInterface
  20.      * @throws NotFoundException
  21.      * @throws ParameterNotFoundException
  22.      */
  23.     public function process(ServerRequestInterface $request, DelegateInterface $delegate)
  24.     {
  25.         $this->isRequestVerified($request, \App\Api\v1\Merchants\Verify\ExistsMiddleware::class);
  26.  
  27.         $merchant = $request->getAttribute(Merchants::class);
  28.  
  29.         $transaction = $request->getAttribute(Transactions::class);
  30.  
  31.         if (!$transaction) {
  32.             $transactionUuid = $this->getRouteParam($request, static::URI_TOKEN);
  33.  
  34.             $this->log('Get an information about one transtaction',
  35.                 ['transactionUuid' => $transactionUuid]
  36.             );
  37.  
  38.             if ($transactionUuid) {
  39.                 $transaction = $this->getEm()->getRepository(Transactions::class)
  40.                     ->findOneBy([
  41.                         'uuid' => $transactionUuid,
  42.                         'merchant' => $merchant
  43.                     ]);
  44.  
  45.                 $this->log('Request an information about one transtaction',
  46.                     $transaction->export()
  47.                 );
  48.  
  49.                 $this->logTags([
  50.                     'transaction' => $transaction->uuid,
  51.                 ]);
  52.  
  53.                 if ($transaction) {
  54.                     $request = $request->withAttribute(Transactions::class, $transaction);
  55.                 }
  56.             }
  57.         }
  58.  
  59.         if (!is_array($transaction)) {
  60.             $transactions = [$transaction];
  61.         }
  62.         else {
  63.             $transactions = $transaction;
  64.         }
  65.  
  66.         foreach ($transactions as $transaction) {
  67.             if (!$transaction || $transaction->merchant->uuid != $merchant->uuid) {
  68.                 throw new ParameterNotFoundException('transaction');
  69.             }
  70.         }
  71.  
  72.         // @codeCoverageIgnoreStart
  73.         // go next if the request had not been processed
  74.         return $delegate->process(
  75.             $this
  76.                 ->verifyRequest($request)
  77.         );
  78.     }// @codeCoverageIgnoreEnd
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement