Issues (29)

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
 */
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.
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
		) {
22
			$this->RequestHandler = $this->Components->load('RequestHandler');
23
		}
24
	}
25
26
/**
27
 * Called before the controller action.
28
 *
29
 * @return void
30
 */
31
	public function beforeFilter() {
32
		parent::beforeFilter();
33
34
		if (in_array($this->request->action, ['check'], true)) {
35
			// Disable Security component checks
36
			if ($this->Components->enabled('Security')) {
37
				$this->Components->disable('Security');
38
			}
39
40
			// Allow action without authentication
41
			if ($this->Components->enabled('Auth')) {
42
				$this->Auth->allow($this->request->action);
43
			}
44
		}
45
	}
46
47
/**
48
 * Checks a given vat number (from POST data).
49
 *
50
 * @return void
51
 */
52
	public function check() {
53
		$vatNumber = $this->request->data('vatNumber');
54
		$vatNumber = $this->VatNumberCheck->normalize($vatNumber);
55
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 (InternalErrorException $e) {
63
			$this->response->statusCode(503);
64
		}
65
66
		$this->set(compact('jsonData'));
67
		$this->set('_serialize', 'jsonData');
68
	}
69
70
}
71