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

WishlistToken   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 43
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getValue() 0 4 1
A setValue() 0 4 1
A __toString() 0 4 1
A generate() 0 14 2
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