Passed
Pull Request — master (#2)
by
unknown
05:11
created

VatNumberChecksController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 22
dl 0
loc 54
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFilter() 0 10 4
A check() 0 14 4
A __construct() 0 5 2
1
<?php
2
namespace VatNumberCheck\Controller;
3
4
use Cake\Event\Event;
5
use Cake\Network\Exception\InternalErrorException;
6
use VatNumberCheck\Utility\Model\VatNumberCheck;
7
/**
8
 * VatNumberChecks Controller.
9
 *
10
 * @property \Cake\Controller\Component\RequestHandlerComponent $RequestHandler
11
 * @property \VatNumberCheck\Utility\Model\VatNumberCheck $VatNumberCheck
12
 */
13
class VatNumberChecksController extends AppController
14
{
15
/**
16
 * Constructor
17
 *
18
 * @param CakeRequest $request Request instance.
0 ignored issues
show
Bug introduced by
The type VatNumberCheck\Controller\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...
19
 * @param CakeResponse $response Response instance.
0 ignored issues
show
Bug introduced by
The type VatNumberCheck\Controller\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...
20
 */
21
	public function __construct($request = null, $response = null) {
22
		parent::__construct($request, $response);
23
		$this->constructClasses();
24
		if (!$this->Components->attached('RequestHandler')) {
25
			$this->RequestHandler = $this->Components->load('RequestHandler');
26
		}
27
	}
28
29
/**
30
* Called before the controller action.
31
*
32
* @return void
33
*/
34
	public function beforeFilter() {
35
		parent::beforeFilter();
36
		if (in_array($this->request->action, ['check'], true)) {
37
			// Disable Security component checks
38
			if ($this->Components->enabled('Security')) {
39
				$this->Components->disable('Security');
40
			}
41
			// Allow action without authentication
42
			if ($this->Components->enabled('Auth')) {
43
				$this->Auth->allow($this->request->action);
44
			}
45
		}
46
	}
47
48
/**
49
* Checks a given vat number (from POST data).
50
*
51
* @return void
52
*/
53
	public function check() {
54
		$vatNumber = $this->request->data('vatNumber') ?: '';
55
		$vatNumber = $this->VatNumberCheck->normalize($vatNumber);
56
		$jsonData = array_merge(compact('vatNumber'), ['status' => 'failure']);
57
		try {
58
			$vatNumberValid = $this->VatNumberCheck->check($vatNumber);
59
			if ($vatNumberValid) {
60
				$jsonData = array_merge(compact('vatNumber'), ['status' => 'ok']);
61
			}
62
		} catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type VatNumberCheck\Controller\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
63
			$this->response->statusCode(503);
64
		}
65
		$this->set(compact('jsonData'));
66
		$this->set('_serialize', 'jsonData');
67
	}
68
}