Completed
Push — master ( 99025e...c37940 )
by Adam
04:22 queued 19s
created

PhoneValidator::validatePhone()   C

Complexity

Conditions 10
Paths 11

Size

Total Lines 63
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10.0578

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 63
ccs 22
cts 24
cp 0.9167
rs 6.3636
cc 10
eloc 24
nc 11
nop 2
crap 10.0578

How to fix   Long Method    Complexity   

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
		// Value have to be phone entity
67 1
		if ($value instanceof Phone\Entities\Phone) {
68 1
			$number = $value->getRawOutput();
69
70
		// Wrong value
71 1
		} else {
72 1
			return FALSE;
73
		}
74
75
		// Get instance of phone number util
76 1
		$phoneNumberUtil = PhoneNumberUtil::getInstance();
77
78
		// Get list of allowed countries from params
79 1
		$allowedCountries = self::determineCountries($control->getAllowedCountries());
80
81
		// Get list of allowed phone types
82 1
		$allowedTypes = self::determineTypes($control->getAllowedPhoneTypes());
83
84
		// Perform validation
85 1
		foreach ($allowedCountries as $country) {
86
			try {
87
				// For default countries or country field, the following throws NumberParseException if
88
				// not parsed correctly against the supplied country
89
				// For automatic detection: tries to discover the country code using from the number itself
90 1
				$phoneProto = $phoneNumberUtil->parse($number, $country);
91
92
				// For automatic detection, the number should have a country code
93
				// Check if type is allowed
94
				if (
95 1
					$phoneProto->hasCountryCode() &&
96 1
					$allowedTypes === [] ||
97 1
					in_array($phoneNumberUtil->getNumberType($phoneProto), $allowedTypes)
98 1
				) {
99
					// Automatic detection:
100 1
					if ($country == 'ZZ') {
101
						// Validate if the international phone number is valid for its contained country
102
						return $phoneNumberUtil->isValidNumber($phoneProto);
103
					}
104
105
					// Validate number against the specified country. Return only if success
106
					// If failure, continue loop to next specified country
107 1
					if ($phoneNumberUtil->isValidNumberForRegion($phoneProto, $country)) {
108 1
						return TRUE;
109
					}
110
				}
111
112 1
			} catch (libphonenumber\NumberParseException $ex) {
113
				// Proceed to default validation error
114
			}
115 1
		}
116
117
		// All specified country validations have failed
118 1
		return FALSE;
119
	}
120
}
121