DoctrineORMTokenRepository::findExisting()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 0
cts 10
cp 0
rs 9.6666
cc 4
nc 3
nop 3
crap 20
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Repository;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Yokai\SecurityTokenBundle\Entity\Token;
8
use Yokai\SecurityTokenBundle\Exception\TokenExpiredException;
9
use Yokai\SecurityTokenBundle\Exception\TokenNotFoundException;
10
use Yokai\SecurityTokenBundle\Exception\TokenConsumedException;
11
12
/**
13
 * Doctrine ORM token repository;
14
 *
15
 * @author Yann Eugoné <[email protected]>
16
 */
17
class DoctrineORMTokenRepository implements TokenRepositoryInterface
18
{
19
    /**
20
     * @var EntityManager
21
     */
22
    private $manager;
23
24
    /**
25
     * @var EntityRepository
26
     */
27
    private $repository;
28
29
    /**
30
     * @param EntityManager    $manager    The token entity manager
31
     * @param EntityRepository $repository The token entity repository
32
     */
33 7
    public function __construct(EntityManager $manager, EntityRepository $repository)
34
    {
35 7
        $this->manager = $manager;
36 7
        $this->repository = $repository;
37 7
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 5
    public function get($value, $purpose)
43
    {
44 5
        $token = $this->repository->findOneBy(
45
            [
46 5
                'value' => $value,
47 5
                'purpose' => $purpose,
48
            ]
49
        );
50
51 5
        if (!$token instanceof Token) {
52 1
            throw TokenNotFoundException::create($value, $purpose);
53
        }
54 4
        if ($token->isExpired()) {
55 1
            throw TokenExpiredException::create($value, $purpose, $token->getExpiresAt());
56
        }
57 3
        if ($token->isConsumed()) {
58 2
            throw TokenConsumedException::create($value, $purpose, $token->getCountUsages());
59
        }
60
61 1
        return $token;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67
    public function findExisting($userClass, $userId, $purpose)
68
    {
69
        $token = $this->repository->findOneBy(
70
            [
71
                'userClass' => $userClass,
72
                'userId' => $userId,
73
                'purpose' => $purpose,
74
            ]
75
        );
76
        if (!$token instanceof Token) {
77
            return null;
78
        }
79
        if ($token->isConsumed() || $token->isExpired()) {
80
            return null;
81
        }
82
83
        return $token;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 1
    public function create(Token $token)
90
    {
91 1
        $this->manager->persist($token);
92 1
        $this->manager->flush($token);
93 1
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 1
    public function update(Token $token)
99
    {
100 1
        $this->manager->persist($token);
101 1
        $this->manager->flush($token);
102 1
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function exists($value, $purpose)
108
    {
109
        $builder = $this->repository->createQueryBuilder('token');
110
        $builder
111
            ->select('COUNT(token.id)')
112
            ->where('token.value = :value')
113
            ->andWhere('token.purpose = :purpose')
114
            ->setParameters(
115
                [
116
                    'value' => $value,
117
                    'purpose' => $purpose,
118
                ]
119
            )
120
        ;
121
122
        return intval($builder->getQuery()->getSingleScalarResult()) > 0;
123
    }
124
}
125