Cancelled
Push — master ( 33a3bb...e2379a )
by Dāvis
05:55
created

Generator::useUpper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Script\Utils;
4
5
class Generator
6
{
7
    const PASS_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
8
    const PASS_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
9
    const PASS_DIGITS = '0123456789';
10
    const PASS_SYMBOLS = '!@#$%^&*()_-=+;:.?';
11
12
    private $sets = [];
13
14
    public function generate($length = 20)
15
    {
16
        $all = '';
17
        $password = '';
18
        foreach ($this->sets as $set) {
19
            $password .= $set[$this->tweak(str_split($set))];
20
            $all .= $set;
21
        }
22
        $all = str_split($all);
23
        for ($i = 0; $i < $length - count($this->sets); $i++) {
24
            $password .= $all[$this->tweak($all)];
25
        }
26
27
        return str_shuffle($password);
28
    }
29
30
    public function tweak($array)
31
    {
32
        if (function_exists('random_int')) {
33
            return random_int(0, count($array) - 1);
34
        } elseif (function_exists('mt_rand')) {
35
            return mt_rand(0, count($array) - 1);
36
        } else {
37
            return array_rand($array);
38
        }
39
    }
40
41
    public function useLower()
42
    {
43
        $this->sets['lower'] = self::PASS_LOWERCASE;
44
45
        return $this;
46
    }
47
48
    public function useUpper()
49
    {
50
        $this->sets['upper'] = self::PASS_UPPERCASE;
51
52
        return $this;
53
    }
54
55
    public function useDigits()
56
    {
57
        $this->sets['digits'] = self::PASS_DIGITS;
58
59
        return $this;
60
    }
61
62
    public function useSymbols()
63
    {
64
        $this->sets['symbols'] = self::PASS_SYMBOLS;
65
66
        return $this;
67
    }
68
}
69