VatNumberCheck   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 48
rs 10
c 5
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 2 1
A check() 0 9 1
1
<?php
2
App::uses('VatNumberCheckAppModel', 'VatNumberCheck.Model');
3
4
/**
5
 * VatNumberCheck Model
6
 *
7
 */
8
class VatNumberCheck extends VatNumberCheckAppModel {
9
10
/**
11
 * The name of the DataSource connection that this Model uses.
12
 *
13
 * @var string
14
 */
15
	public $useDbConfig = 'vatNumberCheckWebservice';
16
17
/**
18
 * Use table.
19
 *
20
 * @var mixed False or table name
21
 */
22
	public $useTable = false;
23
24
/**
25
 * The (translation) domain to be used for extracted validation messages in models.
26
 *
27
 * @var string
28
 */
29
	public $validationDomain = 'vat_number_check';
30
31
/**
32
 * Normalizes a VAT number.
33
 *
34
 * @param string $vatNumber A VAT number
35
 * @return string A (normalized) VAT number
36
 */
37
	public function normalize(string $vatNumber) : string {
38
		return preg_replace('/[^A-Z0-9]/', '', strtoupper($vatNumber));
39
	}
40
41
/**
42
 * Checks a given VAT number.
43
 *
44
 * @param string $vatNumber A VAT number
45
 * @return bool Valid or not
46
 */
47
	public function check(string $vatNumber) : bool {
48
		$memberStateCode = substr($vatNumber, 0, 2);
49
		$number = substr($vatNumber, 2);
50
51
		$params = ['countryCode' => $memberStateCode, 'vatNumber' => $number];
52
53
		$result = $this->query('checkVat', $params);
54
55
		return $result->valid ?? false;
56
	}
57
}
58