VatNumberChecksController::check()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 11
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 16
rs 9.9
1
<?php
2
App::uses('VatNumberChecksAppController', 'VatNumberCheck.Controller');
3
4
/**
5
 * VatNumberChecks Controller
6
 *
7
 * @property VatNumberCheck.VatNumberCheck $VatNumberCheck
8
 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment VatNumberCheck.VatNumberCheck at position 0 could not be parsed: Unknown type name 'VatNumberCheck.VatNumberCheck' at position 0 in VatNumberCheck.VatNumberCheck.
Loading history...
9
class VatNumberChecksController extends VatNumberChecksAppController {
10
11
/**
12
 * Constructor
13
 *
14
 * @param CakeRequest $request Request instance.
0 ignored issues
show
Bug introduced by
The type CakeRequest 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...
15
 * @param CakeResponse $response Response instance.
0 ignored issues
show
Bug introduced by
The type CakeResponse 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...
16
 */
17
	public function __construct($request = null, $response = null) {
18
		parent::__construct($request, $response);
19
		$this->constructClasses();
20
		if (!$this->Components->attached('RequestHandler')) {
21
			$this->RequestHandler = $this->Components->load('RequestHandler');
0 ignored issues
show
Bug Best Practice introduced by
The property RequestHandler does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
		}
23
	}
24
25
/**
26
 * Called before the controller action.
27
 *
28
 * @return void
29
 */
30
	public function beforeFilter() {
31
		parent::beforeFilter();
32
33
		if (in_array($this->request->action, ['check'], true)) {
34
			// Disable Security component checks
35
			if ($this->Components->enabled('Security')) {
36
				$this->Components->disable('Security');
37
			}
38
39
			// Allow action without authentication
40
			if ($this->Components->enabled('Auth')) {
41
				$this->Auth->allow($this->request->action);
42
			}
43
		}
44
	}
45
46
/**
47
 * Checks a given vat number (from POST data).
48
 *
49
 * @return void
50
 */
51
	public function check() {
52
		$vatNumber = $this->request->data('vatNumber') ?: '';
53
		$vatNumber = $this->VatNumberCheck->normalize($vatNumber);
54
55
		$jsonData = array_merge(compact('vatNumber'), ['status' => 'failure']);
56
		try {
57
			$vatNumberValid = $this->VatNumberCheck->check($vatNumber);
58
			if ($vatNumberValid) {
59
				$jsonData = array_merge(compact('vatNumber'), ['status' => 'ok']);
60
			}
61
		} catch (Exception $e) {
62
			$this->response->statusCode(503);
63
		}
64
65
		$this->set(compact('jsonData'));
66
		$this->set('_serialize', 'jsonData');
67
	}
68
69
}
70