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

ExecutionToken   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 37
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generate() 0 4 1
A isValid() 0 4 2
A hash() 0 3 1
A setTimeout() 0 4 1
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
}