Completed
Push — master ( 646735...a247e9 )
by Mikołaj
8s
created

WishlistToken::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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