Completed
Pull Request — master (#26)
by Valentyn
02:41
created

ApiTokenRepository::findByToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 8
cts 10
cp 0.8
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
crap 3.072
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Users\Repository;
5
6
use App\Users\Entity\ApiToken;
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Symfony\Bridge\Doctrine\RegistryInterface;
9
10
/**
11
 * @method ApiToken|null find($id, $lockMode = null, $lockVersion = null)
12
 * @method ApiToken|null findOneBy(array $criteria, array $orderBy = null)
13
 * @method ApiToken[]    findAll()
14
 * @method ApiToken[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
15
 */
16
class ApiTokenRepository extends ServiceEntityRepository
17
{
18 19
    public function __construct(RegistryInterface $registry)
19
    {
20 19
        parent::__construct($registry, ApiToken::class);
21 19
    }
22
23 5
    public function findByToken(string $token): ?ApiToken
24
    {
25 5
        $query = $this->getEntityManager()
26 5
            ->createQuery(
27 5
                'SELECT t, u FROM App\Users\Entity\ApiToken t JOIN t.user u WHERE t.token = :token'
28 5
            )->setParameter('token', $token);
29
30
        try {
31 5
            return $query->getSingleResult();
32 1
        } catch (\Doctrine\ORM\NoResultException $e) {
33 1
            return null;
34
        } catch (\Doctrine\ORM\NonUniqueResultException $e) {
35
            return null;
36
        }
37
    }
38
}
39