Completed
Push — master ( e01736...ba6a43 )
by Adam
04:12
created

PhoneValidator::validatePhone()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 54
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9.081

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 54
ccs 18
cts 20
cp 0.9
rs 7.2551
cc 9
eloc 20
nc 10
nop 2
crap 9.081

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PhoneValidator.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:FormPhone!
9
 * @subpackage     Forms
10
 * @since          1.0.0
11
 *
12
 * @date           12.12.15
13
 */
14
15
namespace IPub\FormPhone\Forms;
16
17
use Nette;
18
use Nette\Forms;
19
20
use libphonenumber;
21
use libphonenumber\PhoneNumberUtil;
22
23
use IPub\FormPhone;
24
use IPub\FormPhone\Controls;
25
use IPub\FormPhone\Exceptions;
26
27
use IPub\Phone;
28
29
/**
30
 * Phone number control form field validator
31
 *
32
 * @package        iPublikuj:FormPhone!
33
 * @subpackage     Forms
34
 *
35
 * @author         Adam Kadlec <[email protected]>
36
 */
37
class PhoneValidator extends Phone\Forms\PhoneValidator
38 1
{
39
	/**
40
	 * Define class name
41
	 */
42
	const CLASS_NAME = __CLASS__;
43
44
	/**
45
	 * Define validator calling constant
46
	 */
47
	const PHONE = 'IPub\FormPhone\Forms\PhoneValidator::validatePhone';
48
49
	/**
50
	 * @param Forms\IControl $control
51
	 * @param array|NULL $params
52
	 *
53
	 * @return bool
54
	 *
55
	 * @throws Exceptions\NoValidCountryException
56
	 */
57
	public static function validatePhone(Forms\IControl $control, $params = [])
58
	{
59 1
		if (!$control instanceof Controls\Phone) {
60 1
			throw new Exceptions\InvalidArgumentException('This validator could be used only on text field. You used it on: "' . get_class($control) . '"');
61
		}
62
63
		// Get form element value
64 1
		$value = $control->getValue();
65
66
		// Get instance of phone number util
67 1
		$phoneNumberUtil = PhoneNumberUtil::getInstance();
68
69
		// Get list of allowed countries from params
70 1
		$allowedCountries = self::determineCountries($control->getCountries());
71
72
		// Get list of allowed phone types
73 1
		$allowedTypes = self::determineTypes($control->getPhoneTypes());
74
75
		// Perform validation
76 1
		foreach ($allowedCountries as $country) {
77
			try {
78
				// For default countries or country field, the following throws NumberParseException if
79
				// not parsed correctly against the supplied country
80
				// For automatic detection: tries to discover the country code using from the number itself
81 1
				$phoneProto = $phoneNumberUtil->parse($value, $country);
82
83
				// For automatic detection, the number should have a country code
84
				// Check if type is allowed
85
				if (
86 1
					$phoneProto->hasCountryCode() &&
87 1
					$allowedTypes === [] ||
88 1
					in_array($phoneNumberUtil->getNumberType($phoneProto), $allowedTypes)
89 1
				) {
90
					// Automatic detection:
91 1
					if ($country == 'ZZ') {
92
						// Validate if the international phone number is valid for its contained country
93
						return $phoneNumberUtil->isValidNumber($phoneProto);
94
					}
95
96
					// Validate number against the specified country. Return only if success
97
					// If failure, continue loop to next specified country
98 1
					if ($phoneNumberUtil->isValidNumberForRegion($phoneProto, $country)) {
99 1
						return TRUE;
100
					}
101
				}
102
103 1
			} catch (libphonenumber\NumberParseException $ex) {
104
				// Proceed to default validation error
105
			}
106 1
		}
107
108
		// All specified country validations have failed
109 1
		return FALSE;
110
	}
111
}
112