Completed
Push — master ( ff496f...1b73c5 )
by Mahmoud
03:18
created

HashIdTrait::runEndpointsHashedIdsDecoder()   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 0
1
<?php
2
3
namespace App\Port\HashId\Traits;
4
5
use Illuminate\Support\Facades\Config;
6
use Route;
7
use Vinkla\Hashids\Facades\Hashids;
8
9
/**
10
 * Class HashIdTrait.
11
 *
12
 * @author  Mahmoud Zalt <[email protected]>
13
 */
14
trait HashIdTrait
15
{
16
17
    /**
18
     * All ID's passed with all endpoints will be decoded before entering the Application
19
     */
20
    public function runEndpointsHashedIdsDecoder()
21
    {
22
        if (Config::get('hello.hash-id')) {
23
            Route::bind('id', function ($id, $route) {
0 ignored issues
show
Unused Code introduced by
The parameter $route is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
                return Hashids::decode($id)[0];
25
            });
26
        }
27
    }
28
29
30
    /**
31
     * Will be used by the Eloquent Models (since it's used as trait there).
32
     *
33
     * @return  mixed
34
     */
35
    public function getHashedKey()
36
    {
37
        // hash the ID only if hash-id enabled in the config
38
        if (Config::get('hello.hash-id')) {
39
            return Hashids::encode($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...
40
        }
41
42
        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...
43
    }
44
45
}
46