Completed
Push — master ( c83b33...876c25 )
by Milroy
27s queued 25s
created

HashExists::passes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
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 $optimus */
13
    private $optimus;
14
15
    /** @var string */
16
    private $table;
17
18
    /** @var string */
19
    private $column;
20
21 9
    public function __construct(string $table, string $column)
22
    {
23 9
        $this->optimus = App::make(Optimus::class);
24 9
        $this->table = $table;
25 9
        $this->column = $column;
26 9
    }
27
28
    /**
29
     * Determine if the validation rule passes.
30
     *
31
     * @param  string  $attribute
32
     * @param  mixed  $value
33
     * @return bool
34
     */
35 9
    public function passes($attribute, $value)
36
    {
37 9
        if (! is_int($value)) {
38 3
            return false;
39
        }
40
41 6
        return DB::table($this->table)
42 6
            ->where($this->column, $this->optimus->decode($value))
43 6
            ->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