TokenLoader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 3
Metric Value
wmc 4
c 3
b 1
f 3
lcom 1
cbo 1
dl 0
loc 55
rs 10
ccs 0
cts 28
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A retrieveByHash() 0 13 1
A retrieveExpired() 0 10 2
1
<?php
2
3
namespace Majora\Component\OAuth\Loader\ORM;
4
5
use Majora\Component\OAuth\Loader\TokenLoaderInterface;
6
use Majora\Component\OAuth\Repository\ORM\TokenRepository;
7
use Majora\Framework\Date\Clock;
8
9
/**
10
 * ORM token loading implementation.
11
 */
12
class TokenLoader implements TokenLoaderInterface
13
{
14
    /**
15
     * @var TokenRepository
16
     */
17
    protected $tokenRepository;
18
19
    /**
20
     * @var Clock
21
     */
22
    protected $clock;
23
24
    /**
25
     * Construct.
26
     *
27
     * @param TokenRepository $tokenRepository
28
     * @param Clock           $clock
29
     */
30
    public function __construct(TokenRepository $tokenRepository, Clock $clock)
31
    {
32
        $this->tokenRepository = $tokenRepository;
33
        $this->clock = $clock;
34
    }
35
36
    /**
37
     * @see TokenLoaderInterface::retrieveByHash()
38
     */
39
    public function retrieveByHash($hash)
40
    {
41
        return $this->tokenRepository
42
            ->createQueryBuilder('t')
43
                ->where('t.hash = :hash')
44
                    ->setParameter('hash', $hash)
45
                ->andWhere('t.expireAt > :now')
46
                    ->setParameter('now', $this->clock->now())
47
            ->getQuery()
48
                ->setMaxResults(1)
49
                ->getOneOrNullResult()
50
        ;
51
    }
52
53
    /**
54
     * @see TokenLoaderInterface::retrieveExpired()
55
     */
56
    public function retrieveExpired(\DateTime $datetime = null)
57
    {
58
        return $this->tokenRepository
59
            ->createQueryBuilder('t')
60
                ->where('t.expireAt < :date')
61
                ->setParameter('date', $datetime ?: $this->clock->now())
62
            ->getQuery()
63
                ->getResult()
64
        ;
65
    }
66
}
67