Completed
Push — master ( ca7ebd...ffd42a )
by Nikolas
03:40
created

ExecutionToken::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace rtens\domin\delivery\web;
3
4
class ExecutionToken {
5
6
    const SEPARATOR = '_';
7
    const DEFAULT_TIMEOUT_SEC = 600;
8
9
    /** @var int */
10
    private $timeoutSec = self::DEFAULT_TIMEOUT_SEC;
11
12
    /** @var string */
13
    private $secret;
14
15
    /**
16
     * @param string $secret
17
     */
18
    public function __construct($secret) {
19
        $this->secret = $secret;
20
    }
21
22
    public function generate($actionId) {
23
        $time = time();
24
        return $this->hash($actionId, $time) . self::SEPARATOR . $time;
25
    }
26
27
    public function isValid($token, $actionId) {
28
        list($hash, $time) = explode(self::SEPARATOR, $token);
29
        return $this->hash($actionId, $time) == $hash && $time > time() - $this->timeoutSec;
30
    }
31
32
    private function hash($actionId, $time) {
33
        return md5($this->secret . $actionId . $time);
34
    }
35
36
    public function setTimeout($sec) {
37
        $this->timeoutSec = $sec;
38
        return $this;
39
    }
40
}