| Total Complexity | 7 |
| Total Lines | 73 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 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() |
|
| 82 | } |
||
| 83 | } |
||
| 84 |