UuidDriver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 73
ccs 16
cts 16
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnection() 0 3 1
A encode() 0 5 2
A __construct() 0 8 2
A decode() 0 9 2
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use Exception;
6
use InvalidArgumentException;
7
use Ramsey\Uuid\Uuid;
8
9
class UuidDriver implements DriverInterface
10
{
11
    /**
12
     * The hashid manager instance.
13
     *
14
     * @var \ElfSundae\Laravel\Hashid\HashidManager
15
     */
16
    protected $manager;
17
18
    /**
19
     * The hashid connection name for encoding and decoding.
20
     *
21
     * @var string
22
     */
23
    protected $connection;
24
25
    /**
26
     * Create a new UUID driver instance.
27
     *
28
     * @param  \ElfSundae\Laravel\Hashid\HashidManager  $manager
29
     * @param  array  $config
30
     *
31
     * @throws \InvalidArgumentException
32
     */
33 16
    public function __construct(HashidManager $manager, array $config)
34
    {
35 16
        $this->manager = $manager;
36
37 16
        if (isset($config['connection'])) {
38 12
            $this->connection = $config['connection'];
39
        } else {
40 4
            throw new InvalidArgumentException('A connection name must be specified.');
41
        }
42 9
    }
43
44
    /**
45
     * Encode the data.
46
     *
47
     * @param  string|\Ramsey\Uuid\Uuid  $data
48
     * @return string
49
     */
50 4
    public function encode($data)
51
    {
52 4
        $uuid = $data instanceof Uuid ? $data : Uuid::fromString($data);
53
54 4
        return $this->getConnection()->encode($uuid->getBytes());
55
    }
56
57
    /**
58
     * Decode the data.
59
     *
60
     * @param  string  $data
61
     * @return \Ramsey\Uuid\Uuid
62
     */
63 8
    public function decode($data)
64
    {
65
        try {
66 8
            return Uuid::fromBytes($this->getConnection()->decode($data));
67 4
        } catch (Exception $e) {
68 4
            unset($e);
69
        }
70
71 4
        return Uuid::fromString(Uuid::NIL);
72
    }
73
74
    /**
75
     * Get the hashid connection instance.
76
     *
77
     * @return mixed
78
     */
79 8
    protected function getConnection()
80
    {
81 8
        return $this->manager->connection($this->connection);
82
    }
83
}
84