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

AllowedRecipientsValidatorStrict::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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