Passed
Pull Request — master (#5)
by Steven
02:13
created

AllowedRecipientsValidatorStrict::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.568
c 0
b 0
f 0
cc 4
nc 3
nop 4
crap 4
1
<?php
2
3
namespace Digitonic\Validation\Validators;
4
5
use Digitonic\Validation\Services\AllowedRecipientsSanitiser;
6
use Illuminate\Validation\Concerns\ValidatesAttributes;
7
8
class AllowedRecipientsValidatorStrict
9
{
10
    use ValidatesAttributes;
11
12
    protected $sanitiser;
13
14 1
    public function __construct()
15
    {
16 1
        $this->sanitiser = new AllowedRecipientsSanitiser();
17 1
    }
18
19
    /**
20
     * @param $attribute
21
     * @param $value
22
     * @param $parameters
23
     * @param $validator
24
     * @return bool
25
     */
26 1
    public function validate($attribute, $value, $parameters, $validator): bool
0 ignored issues
show
Unused Code introduced by
The parameter $attribute 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...
Unused Code introduced by
The parameter $parameters 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...
Unused Code introduced by
The parameter $validator 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...
27
    {
28 1
        $phoneNumbers = collect(explode(',', $value));
29
30 1
        $invalidNumbers = [];
31
32 1
        foreach ($phoneNumbers as $phoneNumber) {
33 1
            $proto = $this->sanitiser->getPhoneNumberObject($phoneNumber);
34
35 1
            if (!$proto ||
36 1
                !in_array(
37 1
                    $this->sanitiser->getRegionCodeForNumber($proto),
38 1
                    config('digitonic.validation.allowed_mobile_origins')
39
                )
40
            ) {
41 1
                $invalidNumbers[] = $phoneNumber;
42
            }
43
44
        }
45
46 1
        return count($invalidNumbers) === 0;
47
    }
48
}
49