Completed
Push — master ( 349a1f...8ae7aa )
by Ashley
07:19
created

UuidDriver::createVersion4Uuid()   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 0
1
<?php
2
3
namespace Clarkeash\Doorman\Drivers;
4
5
use InvalidArgumentException;
6
use Ramsey\Uuid\Uuid;
7
8
/**
9
 * Class UuidDriver
10
 *
11
 * @package \Clarkeash\Doorman\Drivers
12
 */
13
class UuidDriver implements DriverInterface
14
{
15
    /**
16
     * Create an invite code.
17
     *
18
     * @return string
19
     */
20
    public function code(): string
21
    {
22
        $version = config('doorman.uuid.version', 4);
23
24
        $method = 'createVersion' . $version . 'Uuid';
25
26
        if (method_exists($this, $method)) {
27
            return $this->$method();
28
        }
29
30
        throw new InvalidArgumentException("Version [$version] not supported.");
31
    }
32
33
    protected function createVersion1Uuid(): string
34
    {
35
        return Uuid::uuid1()->toString();
36
    }
37
38
    protected function createVersion3Uuid(): string
39
    {
40
        throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 3');
41
        throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 3');
42
43
        return Uuid::uuid3(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString();
44
    }
45
46
    protected function createVersion4Uuid(): string
47
    {
48
        return Uuid::uuid4()->toString();
49
    }
50
51
    protected function createVersion5Uuid(): string
52
    {
53
        throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 5');
54
        throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 5');
55
56
        return Uuid::uuid5(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString();
57
    }
58
}
59