Passed
Push — developer ( 6b5868...bed0f9 )
by Radosław
22:42 queued 03:39
created

Settings_YetiForce_Buy_Action::process()   B

Complexity

Conditions 10
Paths 47

Size

Total Lines 46
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 46
rs 7.6666
cc 10
nc 47
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * YetiForce register action class file.
5
 *
6
 * @package   Settings
7
 *
8
 * @copyright YetiForce S.A.
9
 * @license   YetiForce Public License 6.5 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Radosław Skrzypczak <[email protected]>
11
 */
12
13
/**
14
 * Class for YetiForce registration actions.
15
 */
16
class Settings_YetiForce_Buy_Action extends Settings_Vtiger_Save_Action
17
{
18
	/**
19
	 * Process user request.
20
	 *
21
	 * @param \App\Request $request
22
	 *
23
	 * @throws \App\Exceptions\NoPermitted
24
	 */
25
	public function process(App\Request $request)
26
	{
27
		try {
28
			$orderId = $message = '';
29
			$responseType = 'success';
30
			$order = new \App\YetiForce\Order();
31
			$order->setPackageId($request->getByType('packageId', \App\Purifier::ALNUM2));
32
			foreach ($order->getFieldInstances() as $field) {
33
				$fieldName = $field->getName();
34
				if ($request->has($fieldName) && !$request->isEmpty($fieldName)) {
35
					$value = $request->getRaw($fieldName);
36
					if ($value && preg_match('/[^A-Za-zÀ-ž\W\d\s]+/u', (string) $value, $mas)) {
37
						throw new \App\Exceptions\AppException('ERR_PLEASE_USE_LATIN_CHARACTERS');
38
					}
39
					$value = $request->getByType($fieldName, $field->get('purifyType'));
40
					$field->getUITypeModel()->validate($value, true);
41
					$order->set($fieldName, $field->getDBValue($value));
42
				} else {
43
					throw new \App\Exceptions\AppException('LBL_NOT_FILLED_MANDATORY_FIELDS');
44
				}
45
			}
46
			$result = $order->send();
47
			if ($error = $order->getError()) {
48
				throw new \App\Exceptions\AppException($error);
49
			}
50
			if ($result) {
51
				$orderId = $order->getId();
52
			}
53
		} catch (\App\Exceptions\AppException $e) {
54
			$result = false;
55
			$responseType = 'error';
56
			$message = $e->getDisplayMessage();
57
		} catch (\Throwable $e) {
58
			$result = false;
59
			$responseType = 'error';
60
			$message = $e->getMessage();
61
		}
62
63
		$response = new Vtiger_Response();
64
		$response->setResult([
65
			'success' => $result ?? false,
66
			'message' => $message ?? '',
67
			'type' => $responseType,
68
			'orderId' => $orderId
69
		]);
70
		$response->emit();
71
	}
72
}
73