Passed
Pull Request — master (#20)
by Mathias
01:54
created

Hashids   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 10
eloc 26
c 4
b 0
f 0
dl 0
loc 78
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A decodeWithCustomHashLength() 0 9 1
A encodeHex() 0 3 1
A encodeWithCustomHashLength() 0 9 1
A __construct() 0 3 1
A setMinHashLength() 0 11 2
A decode() 0 3 1
A encode() 0 3 1
A restoreMinHashLength() 0 4 1
A decodeHex() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roukmoute\HashidsBundle;
6
7
use Hashids\HashidsInterface;
8
use ReflectionObject;
9
use ReflectionProperty;
10
use RuntimeException;
11
12
class Hashids implements HashidsInterface
13
{
14
    private HashidsInterface $hashids;
15
    private ReflectionProperty $minHashLengthReflection;
16
    private int $minHashLength;
17
18
    public function __construct(HashidsInterface $hashids)
19
    {
20
        $this->hashids = $hashids;
21
    }
22
23
    public function encode(...$numbers)
24
    {
25
        return $this->hashids->encode($numbers);
26
    }
27
28
    public function decode($hash)
29
    {
30
        return $this->hashids->decode($hash);
31
    }
32
33
    public function encodeHex($str)
34
    {
35
        return $this->hashids->encodeHex($str);
36
    }
37
38
    public function decodeHex($hash)
39
    {
40
        return $this->hashids->decodeHex($hash);
41
    }
42
43
    /**
44
     * Encode parameters to generate a hash with custom minimum hash length.
45
     *
46
     * @param array ...$numbers parameters to encode
47
     */
48
    public function encodeWithCustomHashLength(int $minHashLength, int ...$numbers): string
49
    {
50
        $this->setMinHashLength($minHashLength);
51
52
        $hashid = $this->hashids->encode($numbers);
53
54
        $this->restoreMinHashLength();
55
56
        return $hashid;
57
    }
58
59
    /**
60
     * Decode parameter to generate a decoded hash with custom minimum hash length.
61
     */
62
    public function decodeWithCustomHashLength(int $minHashLength, string $hash): array
63
    {
64
        $this->setMinHashLength($minHashLength);
65
66
        $decodesHashids = $this->hashids->decode($hash);
67
68
        $this->restoreMinHashLength();
69
70
        return $decodesHashids;
71
    }
72
73
    public function setMinHashLength(int $minHashLength): void
74
    {
75
        $hashIdsReflection = new ReflectionObject($this->hashids);
76
        if (!$hashIdsReflection->hasProperty('minHashLength')) {
77
            throw new RuntimeException(sprintf('Missing "minHashLength" property in class "%s"', get_class($this->hashids)));
78
        }
79
80
        $this->minHashLengthReflection = new ReflectionProperty(get_class($this->hashids), 'minHashLength');
81
        $this->minHashLengthReflection->setAccessible(true);
82
        $this->minHashLength = (int) $this->minHashLengthReflection->getValue($this->hashids);
83
        $this->minHashLengthReflection->setValue($this->hashids, $minHashLength);
84
    }
85
86
    private function restoreMinHashLength(): void
87
    {
88
        $this->minHashLengthReflection->setValue($this->hashids, $this->minHashLength);
89
        $this->minHashLengthReflection->setAccessible(false);
90
    }
91
}
92