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

HashExists   A

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 $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