DeviceIdGenerator::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoman4eg\Nalog\Service\Generator;
5
6
/**
7
 * @author Artem Dubinin <[email protected]>
8
 */
9
final class DeviceIdGenerator
10
{
11
    private int $length;
12
    private bool $lowercased;
13
    private IdStrategyInterface $strategy;
14
15
    public function __construct(?IdStrategyInterface $strategy = null, int $length = 21, bool $lowercased = true)
16
    {
17
        $this->length = $length;
18
        $this->lowercased = $lowercased;
19
        $this->strategy = $strategy ?? new PlatformIdStrategy();
20
    }
21
22
    public function generate(): string
23
    {
24
        $generated = \substr(
25
            \str_replace(['+', '/', '='], '', base64_encode($this->strategy->getId())),
26
            0,
27
            $this->length
28
        );
29
        if ($this->lowercased) {
30
            $generated = \mb_strtolower($generated);
31
        }
32
33
        return $generated;
34
    }
35
}
36