File::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
namespace Sesshin\EntropyGenerator;
3
4
use Sesshin\Exception;
5
6
class File implements EntropyGeneratorInterface
7
{
8
    private $file;
9
    private $length;
10
11
    /**
12
     * @param string $file
13
     * @param int $length
14
     */
15
    public function __construct($file = '/dev/urandom', $length = 512)
16
    {
17
        $this->file = $file;
18
        $this->length = $length;
19
    }
20
21
    /**
22
     * @return string
23
     * @throws Exception
24
     */
25
    public function generate()
26
    {
27
        $entropy = file_get_contents($this->file, false, null, 0, $this->length);
28
        if (empty($entropy)) {
29
            throw new Exception('Entropy file is empty.');
30
        }
31
32
        return $entropy;
33
    }
34
}
35