<?php

namespace RxConnexionBundle\Repository;

/**
 * RxApiSessionRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class RxApiSessionRepository extends \Doctrine\ORM\EntityRepository
{

    public function isSessionTokenValid($accountUserId,$token) {

        $query = $this->getEntityManager()->getConnection()->prepare("
            SELECT ras.`id`
            FROM `rx_api_session` ras
            WHERE ras.`account_user_id` = :accountUserId
            AND ras.`token` = :token
            AND ras.`expiry` >= :now
            LIMIT 1
        ");
        $query->execute(array(
            'accountUserId' => $accountUserId,
            'token' => $token,
            'now' => (new \DateTime())->format('Y-m-d H:i:s'),
        ));

        return $query->rowCount();
    }
}
