Issues (31)

Controller/VatNumberChecksController.php (1 issue)

1
<?php
2
App::uses('VatNumberChecksAppController', 'VatNumberCheck.Controller');
3
4
/**
5
 * VatNumberChecks Controller
6
 *
7
 * @property VatNumberCheck.VatNumberCheck $VatNumberCheck
8
 */
9
class VatNumberChecksController extends VatNumberChecksAppController {
10
11
/**
12
 * Constructor
13
 *
14
 * @param CakeRequest $request Request instance.
15
 * @param CakeResponse $response Response instance.
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