Passed
Push — master ( b8999c...a2f941 )
by Curtis
09:12 queued 03:15
created

ValidatePersonStore::withValidator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Http\Requests\enso\companies;
4
5
use App\Person;
6
7
class ValidatePersonStore extends ValidatePersonUpdate
8
{
9
    public function withValidator($validator)
10
    {
11
        if ($this->personExists()) {
12
            $validator->after(fn ($validator) => $validator->errors()
13
                ->add('id', __('The selected person is already associated to this company')));
14
        }
15
    }
16
17
    private function personExists()
18
    {
19
        return Person::whereId($this->get('id'))
20
            ->whereHas('companies', fn ($companies) => $companies
21
                ->whereId($this->get('company_id')))->exists();
22
    }
23
}
24