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

UuidDriver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 46
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A code() 0 12 2
A createVersion1Uuid() 0 4 1
A createVersion3Uuid() 0 7 1
A createVersion4Uuid() 0 4 1
A createVersion5Uuid() 0 7 1
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