Completed
Push — master ( 29d6d4...6bd456 )
by Felix
03:19
created

Password::getJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * PhPsst.
4
 *
5
 * @copyright Copyright (c) 2016 Felix Sandström
6
 * @license   MIT
7
 */
8
9
namespace PhPsst;
10
11
/**
12
 * @author Felix Sandström <http://github.com/felixsand>
13
 */
14
class Password
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $id;
20
21
    /**
22
     * @var int
23
     */
24
    protected $ttl;
25
26
    /**
27
     * @var int
28
     */
29
    protected $views;
30
31
    /**
32
     * @var string
33
     */
34
    protected $password;
35
36
    /**
37
     * Password constructor.
38
     * @param string $id
39
     * @param string $password
40
     * @param int $ttl
41
     * @param int $views
42
     */
43 4
    public function __construct($id, $password, $ttl, $views)
44
    {
45 4
        $this->id = $id;
46 4
        $this->password = $password;
47 4
        $this->ttl = $ttl;
48 4
        $this->views = $views;
49 4
    }
50
51
    /**
52
     * @return string
53
     */
54 1
    public function getId()
55
    {
56 1
        return $this->id;
57
    }
58
59
    /**
60
     * @return string
61
     */
62 1
    public function getPassword()
63
    {
64 1
        return $this->password;
65
    }
66
67
    /**
68
     * @return int
69
     */
70 1
    public function getTtl()
71
    {
72 1
        return $this->ttl;
73
    }
74
75
    /**
76
     * @return int
77
     */
78 1
    public function getViews()
79
    {
80 1
        return $this->views;
81
    }
82
83
    /**
84
     */
85 2
    public function decreaseViews()
86
    {
87 2
        if (($this->views - 1) >= 0) {
88 2
            $this->views--;
89 2
        } else {
90 1
            throw new \LogicException('Passwords with negative views should be deleted');
91
        }
92 2
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getJson()
98
    {
99
        return json_encode([
100
            'id' => $this->getId(),
101
            'password' => $this->getPassword(),
102
            'ttl' => $this->getTtl(),
103
            'ttlTime' => time() + $this->getTtl(),
104
            'views' => $this->getViews(),
105
        ]);
106
    }
107
}
108