NonceService   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 82
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createNonce() 0 14 2
A findNonce() 0 4 1
A isValid() 0 10 3
A consumeNonce() 0 14 2
1
<?php
2
/**
3
 * @author Stefano Torresi (http://stefanotorresi.it)
4
 * @license See the file LICENSE.txt for copying permission.
5
 * ************************************************
6
 */
7
8
namespace Thorr\Nonce\Service;
9
10
use DateInterval;
11
use DateTime;
12
use Thorr\Nonce\DataMapper\NonceMapperInterface;
13
use Thorr\Nonce\Entity\Nonce;
14
use Thorr\Nonce\Entity\NonceOwnerInterface;
15
use Thorr\Nonce\Options\ModuleOptions;
16
17
class NonceService implements NonceServiceInterface
18
{
19
    /**
20
     * @var NonceMapperInterface
21
     */
22
    protected $nonceMapper;
23
24
    /**
25
     * @var ModuleOptions
26
     */
27
    protected $moduleOptions;
28
29
    /**
30
     * @param NonceMapperInterface $nonceMapper
31
     * @param ModuleOptions        $moduleOptions
32
     */
33 12
    public function __construct(NonceMapperInterface $nonceMapper, ModuleOptions $moduleOptions)
34
    {
35 12
        $this->nonceMapper   = $nonceMapper;
36 12
        $this->moduleOptions = $moduleOptions;
37 12
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    public function createNonce(NonceOwnerInterface $owner, DateTime $expirationDate = null, $namespace = null)
43
    {
44 4
        if (! $expirationDate) {
45 3
            $interval       = new DateInterval($this->moduleOptions->getDefaultNonceExpirationInterval());
46 3
            $expirationDate = (new DateTime())->add($interval);
47 4
        }
48
49 4
        $nonce = new Nonce(null, $expirationDate, $namespace);
50 4
        $nonce->setOwner($owner);
51
52 4
        $this->nonceMapper->save($nonce);
53
54 4
        return $nonce;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * proxies to the data mapper
61
     */
62 2
    public function findNonce($uuid, $namespace = null)
63
    {
64 2
        return $this->nonceMapper->find($uuid, $namespace);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 7
    public function isValid(Nonce $nonce)
71
    {
72 7
        $now = new DateTime();
73
74 7
        if ($nonce->getExpirationDate() && $nonce->getExpirationDate() <= $now) {
75 3
            return false;
76
        }
77
78 5
        return true;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 4
    public function consumeNonce(Nonce $nonce)
85
    {
86 4
        $now = new DateTime();
87
88 4
        if (! $this->isValid($nonce)) {
89 2
            throw new Exception\NonceHasExpiredException();
90
        }
91
92 3
        $nonce->setExpirationDate($now);
93
94 3
        $this->nonceMapper->remove($nonce);
95
96 3
        return true;
97
    }
98
}
99