Completed
Push — master ( 8803de...e3dc66 )
by Andrii
03:17
created

Token::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * Library for confirmation tokens
5
 *
6
 * @link      https://github.com/hiqdev/php-confirmator
7
 * @package   php-confirmator
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\php\confirmator;
13
14
class Token
15
{
16
    protected $service;
17
18
    protected $data = [];
19
20
    protected $string;
21
22 3
    public function __construct(ServiceInterface $service, array $data = [], $string = null)
23
    {
24 3
        $this->service = $service;
25 3
        $this->data = $this->prepareData($data);
26 3
        $this->string = $string ?: $this->genString();
27 3
    }
28
29 3
    protected function prepareData(array $data)
30
    {
31 3
        if (empty($data['notAfter'])) {
32 1
            $data['notAfter'] = '+ 1 week';
33
        }
34 3
        foreach ($data as $key => &$value) {
35 3
            if ($key === 'notAfter' || $key === 'notBefore') {
36 3
                $value = date('Y-m-d H:i:s', strtotime($value));
37
            }
38
        }
39
40 3
        return $data;
41
    }
42
43 3
    public function __toString()
44
    {
45 3
        return $this->string;
46
    }
47
48 3
    public function genString($length = 32)
49
    {
50 3
        $res = '';
51 3
        $chars = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
52 3
        $last = strlen($chars) - 1;
53 3
        for ($i = 0; $i < $length; ++$i) {
54 3
            $res .= $chars[mt_rand(0, $last)];
55
        }
56
57 3
        return $res;
58
    }
59
60 2
    public function get($key, $default = null)
61
    {
62 2
        return isset($this->data[$key]) ? $this->data[$key] : $default;
63
    }
64
65 3
    public function mget()
66
    {
67 3
        return $this->data;
68
    }
69
70
    public function remove()
71
    {
72
        $this->service->removeToken($this->string);
73
    }
74
75 2
    public function check(array $data)
76
    {
77 2
        return $this->checkNotAfter() && $this->checkNotBefore() && $this->checkData($data);
78
    }
79
80 1
    public function checkData(array $data)
81
    {
82 1
        foreach ($data as $key => $value) {
83 1
            if ($value !== $this->get($key)) {
84 1
                return false;
85
            }
86
        }
87
88 1
        return true;
89
    }
90
91 2
    public function checkNotBefore()
92
    {
93 2
        if (empty($this->data['notBefore'])) {
94
            return true;
95
        }
96
97 2
        return time() > strtotime($this->data['notBefore']);
98
    }
99
100 2
    public function checkNotAfter()
101
    {
102 2
        if (empty($this->data['notAfter'])) {
103
            return true;
104
        }
105
106 2
        return time() < strtotime($this->data['notAfter']);
107
    }
108
}
109