Completed
Pull Request — master (#1)
by
unknown
01:29
created

WishlistToken::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitBag\SyliusWishlistPlugin\Entity;
6
7
class WishlistToken implements WishlistTokenInterface
8
{
9
    protected $value;
10
11
    public function __construct(?string $value = null)
12
    {
13
        if ($value === null) {
14
            $this->value = $this->generate(self::VALUE_LENGTH);
15
        } else {
16
            $this->setValue($value);
17
        }
18
    }
19
20
    public function getValue(): string
21
    {
22
        return $this->value;
23
    }
24
25
    public function setValue(string $value): void
26
    {
27
        $this->value = $value;
28
    }
29
30
    public function __toString()
31
    {
32
        return $this->getValue();
33
    }
34
35
    private function generate($length): string
36
    {
37
        $token = '';
38
        $codeAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
39
        $codeAlphabet .= 'abcdefghijklmnopqrstuvwxyz';
40
        $codeAlphabet .= '0123456789';
41
        $max = strlen($codeAlphabet); // edited
42
43
        for ($i = 0; $i < $length; ++$i) {
44
            $token .= $codeAlphabet[random_int(0, $max - 1)];
45
        }
46
47
        return $token;
48
    }
49
}
50