GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Block   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 41
c 1
b 0
f 1
dl 0
loc 259
rs 10
wmc 19

15 Methods

Rating   Name   Duplication   Size   Complexity  
A regenerateHash() 0 6 1
A setIndex() 0 5 1
A getHash() 0 3 1
A getPreviousHash() 0 3 1
A setTimestamp() 0 9 2
A decrease() 0 5 1
A __construct() 0 11 2
A getTimestamp() 0 3 1
A setPreviousHash() 0 5 1
A calculateHash() 0 8 3
A getNonce() 0 3 1
A getIndex() 0 3 1
A setData() 0 5 1
A increase() 0 5 1
A getData() 0 3 1
1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Security\Blockchain;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class Block
20
 * @package O2System\Security\Blockchain
21
 */
22
class Block
23
{
24
    /**
25
     * Block::$nonce
26
     *
27
     * @var int
28
     */
29
    protected $nonce;
30
31
    /**
32
     * Block::$index
33
     *
34
     * @var int
35
     */
36
    protected $index;
37
38
    /**
39
     * Block::$timestamp
40
     *
41
     * @var int
42
     */
43
    protected $timestamp;
44
45
    /**
46
     * Block::$data
47
     *
48
     * @var mixed
49
     */
50
    protected $data;
51
52
    /**
53
     * Block::$hash
54
     *
55
     * @var string
56
     */
57
    protected $hash;
58
59
    /**
60
     * Block::$previousHash
61
     *
62
     * @var string
63
     */
64
    protected $previousHash;
65
66
    // ------------------------------------------------------------------------
67
68
    /**
69
     * Block::__construct
70
     */
71
    public function __construct($index, $timestamp, $data, $previousHash = null)
72
    {
73
        $this->setIndex($index);
74
        $this->setTimestamp($timestamp);
75
        $this->setData($data);
76
        $this->setPreviousHash($previousHash);
77
78
        $this->hash = $this->calculateHash();
79
80
        if(is_null($previousHash)) {
81
            $this->nonce = 0;
82
        }
83
    }
84
85
    // ------------------------------------------------------------------------
86
87
    /**
88
     * Block::getNonce
89
     *
90
     * @return int
91
     */
92
    public function getNonce()
93
    {
94
        return $this->nonce;
95
    }
96
97
    // ------------------------------------------------------------------------
98
99
    /**
100
     * Block::increase
101
     *
102
     * @return static
103
     */
104
    public function increase()
105
    {
106
        $this->nonce++;
107
108
        return $this;
109
    }
110
111
    // ------------------------------------------------------------------------
112
113
    /**
114
     * Block::decrease
115
     *
116
     * @return static
117
     */
118
    public function decrease()
119
    {
120
        $this->nonce--;
121
122
        return $this;
123
    }
124
125
    // ------------------------------------------------------------------------
126
127
    /**
128
     * Block::setIndex
129
     *
130
     * @param int $index
131
     *
132
     * @return static
133
     */
134
    public function setIndex($index)
135
    {
136
        $this->index = (int)$index;
137
138
        return $this;
139
    }
140
141
    // ------------------------------------------------------------------------
142
143
    /**
144
     * Block::getIndex
145
     *
146
     * @return int
147
     */
148
    public function getIndex()
149
    {
150
        return $this->index;
151
    }
152
153
    /**
154
     * Block::setTimestamp
155
     *
156
     * @param string|int $timestamp
157
     *
158
     * @return static
159
     */
160
    public function setTimestamp($timestamp)
161
    {
162
        if (is_string($timestamp)) {
163
            $timestamp = strtotime($timestamp);
164
        }
165
166
        $this->timestamp = $timestamp;
167
168
        return $this;
169
    }
170
171
    // ------------------------------------------------------------------------
172
173
    /**
174
     * Block::getTimestamp
175
     *
176
     * @return int
177
     */
178
    public function getTimestamp()
179
    {
180
        return $this->timestamp;
181
    }
182
183
    // ------------------------------------------------------------------------
184
185
    /**
186
     * Block::setData
187
     *
188
     * @param mixed $data
189
     *
190
     * @return static
191
     */
192
    public function setData($data)
193
    {
194
        $this->data = $data;
195
196
        return $this;
197
    }
198
199
    // ------------------------------------------------------------------------
200
201
    /**
202
     * Block::getData
203
     *
204
     * @return mixed
205
     */
206
    public function getData()
207
    {
208
        return $this->data;
209
    }
210
211
    // ------------------------------------------------------------------------
212
213
    /**
214
     * Block::setPreviousHash
215
     *
216
     * @param string $previousHash
217
     *
218
     * @return static
219
     */
220
    public function setPreviousHash($previousHash)
221
    {
222
        $this->previousHash = $previousHash;
223
224
        return $this;
225
    }
226
227
    // ------------------------------------------------------------------------
228
229
    /**
230
     * Block::getPreviousHash
231
     *
232
     * @return string
233
     */
234
    public function getPreviousHash()
235
    {
236
        return $this->previousHash;
237
    }
238
239
    // ------------------------------------------------------------------------
240
241
    /**
242
     * Block::getHash
243
     *
244
     * @return string
245
     */
246
    public function getHash()
247
    {
248
        return $this->hash;
249
    }
250
251
    // ------------------------------------------------------------------------
252
253
    /**
254
     * Block::getHash
255
     *
256
     * @return string
257
     */
258
    public function calculateHash()
259
    {
260
        $data = $this->data;
261
        if (is_array($this->data) or is_object($this->data)) {
262
            $data = json_encode($this->data);
263
        }
264
265
        return hash('sha256', $this->index . $this->previousHash . $this->timestamp . $data . $this->nonce);
266
    }
267
268
    // ------------------------------------------------------------------------
269
270
    /**
271
     * Block::regenerateHash
272
     *
273
     * @return string
274
     */
275
    public function regenerateHash()
276
    {
277
        $this->increase();
278
        $this->hash = $this->calculateHash();
279
280
        return $this->hash;
281
    }
282
}