Completed
Push — master ( 26dc4b...11fcfc )
by Mahmoud
03:27
created

HashIdTrait::decodeThisId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace App\Port\HashId\Traits;
4
5
use App\Port\Exception\Exceptions\IncorrectIdException;
6
use Illuminate\Support\Facades\Config;
7
use Route;
8
use Vinkla\Hashids\Facades\Hashids;
9
10
/**
11
 * Class HashIdTrait.
12
 *
13
 * @author  Mahmoud Zalt <[email protected]>
14
 */
15
trait HashIdTrait
16
{
17
18
    /**
19
     * endpoint to be skipped from decoding their ID's (example for external ID's)
20
     * @var  array
21
     */
22
    private $skippedEndpoints = [
23
//        'orders/{id}/external',
24
    ];
25
26
    /**
27
     * All ID's passed with all endpoints will be decoded before entering the Application
28
     */
29
    public function runEndpointsHashedIdsDecoder()
30
    {
31
        if (Config::get('hello.hash-id')) {
32
            Route::bind('id', function ($id, $route) {
33
                // skip decoding some endpoints
34
                if (!in_array($route->getUri(), $this->skippedEndpoints)) {
35
36
                    // decode the ID in the URL
37
                    $decoded = $this->decoder($id);
38
39
                    if (empty($decoded)) {
40
                        throw new IncorrectIdException('ID (' . $id . ') is incorrect, consider using the hashed ID 
41
                        instead of the numeric ID.');
42
                    }
43
44
                    return $decoded[0];
45
                }
46
            });
47
        }
48
    }
49
50
    /**
51
     * @param $id
52
     *
53
     * @return  mixed
54
     */
55
    public function decodeThisId($id)
56
    {
57
        if (Config::get('hello.hash-id')) {
58
            return $this->decoder($id)[0];
59
        }
60
61
        return $id;
62
    }
63
64
    /**
65
     * Will be used by the Eloquent Models (since it's used as trait there).
66
     *
67
     * @return  mixed
68
     */
69
    public function getHashedKey()
70
    {
71
        // hash the ID only if hash-id enabled in the config
72
        if (Config::get('hello.hash-id')) {
73
            return $this->encoder($this->getKey());
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
        }
75
76
        return $this->getKey();
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
77
    }
78
79
    /**
80
     * @param $id
81
     *
82
     * @return  mixed
83
     */
84
    private function decoder($id)
85
    {
86
        return Hashids::decode($id);
87
    }
88
89
    /**
90
     * @param $id
91
     *
92
     * @return  mixed
93
     */
94
    private function encoder($id)
95
    {
96
        return Hashids::encode($id);
97
    }
98
99
}
100