Passed
Pull Request — master (#20)
by Hilmi Erdem
10:06
created

NumericNo0Generator::getRandomDigitWithNo0()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
/*
4
 * @copyright 2021 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\Otp\Generators;
9
10
use Throwable;
11
use Erdemkeren\Otp\Contracts\GeneratorContract;
12
13
class NumericNo0Generator extends NumericGenerator implements GeneratorContract
14
{
15
    public function generate(int $length = null): string
16
    {
17
        return (string) str_replace(
18
            0,
19
            $this->getRandomDigitWithNo0(),
20
            parent::generate($length)
21
        );
22
    }
23
24
    private function getRandomDigitWithNo0(): int
25
    {
26
        try {
27
            $int = random_int(1, 9);
28
        } catch (Throwable) {
29
            $int = rand(1, 9);
30
        }
31
32
        return $int;
33
    }
34
}
35