<?php
declare(strict_types=1);
/**
* TransactionVoter.php File.
* This file is part of the Payment.net Project.
*
* PHP version 5
*
* @category Application
* @package Bdm\BackofficeBundle\Security\Authorization\Voter
* @author Pavel Baraulya <pbaraulya@bdmultimedia.fr>
* @link http://www.payment.net/
*
* FEATURES :
* ==========
*
* TODO-LIST :
* ===========
*
* HISTORY :
* =========
* 20150709 - Pavel Baraulya
*
**/
namespace Bdm\BackofficeBundle\Security\Authorization\Voter;
use Bdm\BackofficeBundle\Entity\Application;
use Bdm\BackofficeBundle\Entity\Merchant;
use Bdm\CheckoutBundle\Entity\Payment;
use Bdm\CheckoutBundle\Entity\Transaction;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
/**
* Voter for transaction entity
*/
class TransactionVoter extends AbstractVoter
{
/**
* @param string $sAttribute attribute
* @param mixed $oSubject subject
* @return bool
*/
#[\Override]
public function supports($sAttribute, $oSubject)
{
return $oSubject instanceof Transaction && parent::supports($sAttribute, $oSubject);
}
/**
* @param string $sAttr attr
* @param mixed $mEntity entity
* @param TokenInterface $oToken token
*
* @return bool
*/
protected function voteOnAttribute($sAttr, $mEntity, TokenInterface $oToken)
{
$oUser = $oToken->getUser();
if (!$oUser instanceof Merchant) {
return false;
}
return (bool) $this->findByOwner($oUser->getId(), $mEntity->getId());
}
/**
* Find entity by owner id
*
* @param int $iOwnerId owner id
* @param int $iEntityId entity id
*
* @return mixed
* @throws \Doctrine\ORM\NonUniqueResultException
*/
protected function findByOwner($iOwnerId, $iEntityId)
{
$oQuery = $this->oEm->createQuery('
SELECT t FROM ' . Transaction::class . ' t
WHERE t.oPayment IN (
SELECT p FROM ' . Payment::class . ' p
WHERE p.oApplication IN (
SELECT a.iId FROM ' . Application::class . ' a
WHERE a.oMerchant = :owner_id
)
) AND t.iId = :entity_id
');
$oQuery->setParameters(['owner_id' => $iOwnerId, 'entity_id' => $iEntityId]);
return $oQuery->getOneOrNullResult();
}
}