DeviceIdGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 25
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 12 2
A __construct() 0 5 1
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