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
|
9 |
|
public function code(): string |
21
|
|
|
{ |
22
|
9 |
|
$version = config('doorman.uuid.version', 4); |
23
|
|
|
|
24
|
9 |
|
$method = 'createVersion' . $version . 'Uuid'; |
25
|
|
|
|
26
|
9 |
|
if (method_exists($this, $method)) { |
27
|
8 |
|
return $this->$method(); |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
throw new InvalidArgumentException("Version [$version] not supported."); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
* @throws \Exception |
36
|
|
|
*/ |
37
|
1 |
|
protected function createVersion1Uuid(): string |
38
|
|
|
{ |
39
|
1 |
|
return Uuid::uuid1()->toString(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
* @throws \Throwable |
45
|
|
|
*/ |
46
|
3 |
View Code Duplication |
protected function createVersion3Uuid(): string |
|
|
|
|
47
|
|
|
{ |
48
|
3 |
|
throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 3'); |
49
|
2 |
|
throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 3'); |
50
|
|
|
|
51
|
1 |
|
return Uuid::uuid3(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
1 |
|
protected function createVersion4Uuid(): string |
59
|
|
|
{ |
60
|
1 |
|
return Uuid::uuid4()->toString(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
* @throws \Throwable |
66
|
|
|
*/ |
67
|
3 |
View Code Duplication |
protected function createVersion5Uuid(): string |
|
|
|
|
68
|
|
|
{ |
69
|
3 |
|
throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 5'); |
70
|
2 |
|
throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 5'); |
71
|
|
|
|
72
|
1 |
|
return Uuid::uuid5(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.