HashExists   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 44
ccs 13
cts 13
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A passes() 0 9 2
A message() 0 3 1
A __construct() 0 5 1
1
<?php
2
3
namespace ApiChef\Obfuscate\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
use Illuminate\Support\Facades\App;
7
use Illuminate\Support\Facades\DB;
8
use Jenssegers\Optimus\Optimus;
9
10
class HashExists implements Rule
11
{
12
    /** @var Optimus */
13
    private $optimus;
14
15
    /** @var string */
16
    private $table;
17
18
    /** @var string */
19
    private $column;
20
21 12
    public function __construct(string $table, string $column = 'id')
22
    {
23 12
        $this->optimus = App::make(Optimus::class);
24 12
        $this->table = $table;
25 12
        $this->column = $column;
26 12
    }
27
28
    /**
29
     * Determine if the validation rule passes.
30
     *
31
     * @param  string  $attribute
32
     * @param  mixed  $value
33
     * @return bool
34
     */
35 12
    public function passes($attribute, $value)
36
    {
37 12
        if (! is_int($value)) {
38 3
            return false;
39
        }
40
41 9
        return DB::table($this->table)
42 9
            ->where($this->column, $this->optimus->decode($value))
43 9
            ->count() !== 0;
44
    }
45
46
    /**
47
     * Get the validation error message.
48
     *
49
     * @return string
50
     */
51 6
    public function message()
52
    {
53 6
        return trans('validation.exists');
54
    }
55
}
56