NoncerAbstract::generate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Acquia\Rest;
4
5
abstract class NoncerAbstract implements NoncerInterface
6
{
7
    const DEFAULT_LENGTH = 24;
8
9
    /**
10
     * @var string
11
     */
12
    protected $lastNonce = '';
13
14
    /**
15
     * @var int
16
     */
17
    protected $length;
18
19
    /**
20
     * @param integer $length
21
     */
22 63
    public function __construct($length = self::DEFAULT_LENGTH)
23
    {
24 63
        $this->length = $length;
25 63
    }
26
27
    /**
28
     * @param int $length
29
     */
30 30
    public function setLength($length)
31
    {
32 30
        $this->length = $length;
33 30
    }
34
35
    /**
36
     * @return integer
37
     */
38 3
    public function getLength()
39
    {
40 3
        return $this->length;
41
    }
42
43
    /**
44
     * Generates the nonce.
45
     *
46
     * @return string
47
     */
48
    abstract protected function hash();
49
50
    /**
51
     * Generates and stores a nonce.
52
     *
53
     * @return string
54
     */
55 57
    public function generate()
56
    {
57 57
        $this->lastNonce = $this->hash();
58 57
        return $this->lastNonce;
59
    }
60
61
    /**
62
     * @return string
63
     */
64 48
    public function getLastNonce()
65
    {
66 48
        return $this->lastNonce;
67
    }
68
69
    /**
70
     * Generates and returns a nonce.
71
     *
72
     * @see NoncerAbstract::generate()
73
     */
74 3
    public function __toString()
75
    {
76 3
        return $this->generate();
77
    }
78
}
79