VatNumberCheck   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 28
c 5
b 0
f 0
dl 0
loc 101
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getUrlContent() 0 14 3
A constructUrl() 0 5 1
A check() 0 9 2
A toQueryString() 0 7 1
A normalize() 0 5 1
1
<?php
2
App::uses('VatNumberCheckAppModel', 'VatNumberCheck.Model');
3
App::uses('HttpSocket', 'Network/Http');
4
5
/**
6
 * VatNumberCheck Model
7
 *
8
 */
9
class VatNumberCheck extends VatNumberCheckAppModel {
10
11
/**
12
 * Use table.
13
 *
14
 * @var mixed False or table name
15
 */
16
	public $useTable = false;
17
18
/**
19
 * The (translation) domain to be used for extracted validation messages in models.
20
 *
21
 * @var string
22
 */
23
	public $validationDomain = 'vat_number_check';
24
25
/**
26
 * Url to check vat numbers.
27
 *
28
 */
29
	const CHECK_URL = 'http://ec.europa.eu/taxation_customs/vies/viesquer.do';
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($vatNumber) {
38
		$vatNumber = strtoupper($vatNumber);
39
		$vatNumber = preg_replace('/[^A-Z0-9]/', '', $vatNumber);
40
41
		return $vatNumber;
42
	}
43
44
/**
45
 * Splits a VAT number into querystring parameters.
46
 *
47
 * @param string $vatNumber A VAT number
48
 * @return array Querystring parameters
49
 */
50
	public function toQueryString($vatNumber) {
51
		$memberStateCode = substr($vatNumber, 0, 2);
52
		$number = substr($vatNumber, 2);
53
		$action = 'check';
54
		$check = 'Verify';
55
56
		return compact('memberStateCode', 'number', 'action', 'check');
57
	}
58
59
/**
60
 * Constructs an url for a given vat number.
61
 *
62
 * @param string $vatNumber A VAT number
63
 * @return string An url
64
 */
65
	public function constructUrl($vatNumber) {
66
		$queryString = $this->toQueryString($vatNumber);
67
		$queryString = http_build_query($queryString);
68
69
		return self::CHECK_URL . '?' . $queryString;
70
	}
71
72
/**
73
 * Downloads a given url.
74
 *
75
 * @param string $url An url
76
 * @return mixed Request body on success (string) otherwise false (boolean)
77
 */
78
	public function getUrlContent($url) {
79
		$config = (array)Configure::read('VatNumberCheck.socketConfig');
0 ignored issues
show
Bug introduced by
The type Configure was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
		$HttpSocket = new HttpSocket($config);
81
82
		try {
83
			$response = $HttpSocket->get($url);
84
85
			if ($response->isOk()) {
86
				return $response->body();
87
			}
88
		} catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
89
		}
90
91
		return false;
92
	}
93
94
/**
95
 * Checks a given VAT number.
96
 *
97
 * @param string $vatNumber A VAT number
98
 * @return bool Valid or not
99
 * @throws InternalErrorException
100
 */
101
	public function check($vatNumber) {
102
		$url = $this->constructUrl($vatNumber);
103
		$urlContent = $this->getUrlContent($url);
104
105
		if ($urlContent) {
106
			return (strpos($urlContent, 'Yes, valid VAT number') !== false);
107
		}
108
109
		throw new InternalErrorException(__d('vat_number_check', 'Service unavailable'));
0 ignored issues
show
Bug introduced by
The type InternalErrorException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The function __d was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
		throw new InternalErrorException(/** @scrutinizer ignore-call */ __d('vat_number_check', 'Service unavailable'));
Loading history...
110
	}
111
112
}
113