Completed
Push — master ( 40ea99...4590e9 )
by Elf
02:06
created

Base62Driver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A encode() 0 4 1
A decode() 0 4 1
1
<?php
2
3
namespace ElfSundae\Laravel\Hashid;
4
5
use Illuminate\Support\Arr;
6
use Tuupola\Base62\GmpEncoder;
7
use Tuupola\Base62\PhpEncoder;
8
9
class Base62Driver implements DriverInterface
10
{
11
    /**
12
     * The Base62 instance.
13
     *
14
     * @var \Tuupola\Base62\GmpEncoder|\Tuupola\Base62\PhpEncoder
15
     */
16
    protected $base62;
17
18
    /**
19
     * Indicates integer encoding.
20
     *
21
     * @var bool
22
     */
23
    protected $integer = false;
24
25
    /**
26
     * Create a new hashid driver instance.
27
     *
28
     * @param  array  $config
29
     */
30 3
    public function __construct(array $config = [])
31
    {
32 3
        $options = Arr::only($config, ['characters']);
33 3
        $this->base62 = extension_loaded('gmp') ?
34 3
            new GmpEncoder($options) :
35
            new PhpEncoder($options);
36 3
    }
37
38
    /**
39
     * Encode the data.
40
     *
41
     * @param  mixed  $data
42
     * @return string
43
     */
44 2
    public function encode($data)
45
    {
46 2
        return $this->base62->encode($data, $this->integer);
47
    }
48
49
    /**
50
     * Decode the data.
51
     *
52
     * @param  mixed  $data
53
     * @return mixed
54
     */
55 2
    public function decode($data)
56
    {
57 2
        return $this->base62->decode($data, $this->integer);
58
    }
59
}
60