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

Order::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * YetiForce register order file.
4
 * Modifying this file or functions that affect the footer appearance will violate the license terms!!!
5
 *
6
 * @package App
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
namespace App\YetiForce;
14
15
/**
16
 * YetiForce register class.
17
 */
18
final class Order
19
{
20
	/** @var string URL */
21
	public const URL = 'https://api.yetiforce.eu/registrations/orders';
22
23
	/**
24
	 * Array of company form fields.
25
	 */
26
	private const COMPANY_FORM_FIELDS = [
27
		'name',
28
		'vat_id',
29
		'country',
30
		'post_code',
31
		'city',
32
		'address',
33
	];
34
35
	/** @var string Last error. */
36
	public ?string $error = null;
37
	/** @var bool Response result */
38
	private bool $success;
39
	/** @var string Order ID */
40
	private ?string $id;
41
42
	/** @var array Raw data */
43
	private array $data = [];
44
	/** @var Product package ID */
0 ignored issues
show
Bug introduced by
The type App\YetiForce\Product 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...
45
	private string $packageId;
46
47
	/**
48
	 * Function determines fields available in payment view.
49
	 *
50
	 * @return \Settings_Vtiger_Field_Model[]
51
	 */
52
	public function getFieldInstances(): array
53
	{
54
		$company = \App\Company::getCompany();
55
		$fields = [];
56
		foreach (self::COMPANY_FORM_FIELDS as $fieldName) {
57
			$params = [
58
				'uitype' => 1,
59
				'fieldvalue' => $company[$fieldName] ?? null,
60
				'displaytype' => 1,
61
				'typeofdata' => 'V~M',
62
				'presence' => '',
63
				'isEditableReadOnly' => false,
64
				'maximumlength' => '255',
65
				'column' => $fieldName,
66
				'name' => $fieldName,
67
				'label' => 'LBL_' . strtoupper($fieldName),
68
				'purifyType' => \App\Purifier::TEXT
69
			];
70
71
			switch ($fieldName) {
72
				case 'city':
73
					$params['maximumlength'] = '100';
74
					break;
75
				case 'country':
76
					$params['uitype'] = 16;
77
					$params['maximumlength'] = '100';
78
					$params['picklistValues'] = [];
79
					foreach (\App\Fields\Country::getAll() as $country) {
80
						$params['picklistValues'][$country['name']] = \App\Language::translateSingleMod(
81
							$country['name'],
82
							'Other.Country'
83
						);
84
					}
85
					break;
86
				default:
87
					break;
88
			}
89
90
			$fields[$fieldName] = \Settings_Vtiger_Field_Model::init('Vtiger', $params);
91
		}
92
93
		return $fields;
94
	}
95
96
	/**
97
	 * Set value.
98
	 *
99
	 * @param string $key
100
	 * @param mixed  $value
101
	 *
102
	 * @return $this
103
	 */
104
	public function set(string $key, $value)
105
	{
106
		$this->data[$key] = $value;
107
		return $this;
108
	}
109
110
	/**
111
	 * Send order data.
112
	 *
113
	 * @return bool
114
	 */
115
	public function send(): bool
116
	{
117
		$this->success = false;
118
		$client = new ApiClient();
119
		$client->send(self::URL, 'POST', ['form_params' => $this->getData()]);
120
		$this->error = $client->getError();
121
		if (!$this->error && ($code = $client->getStatusCode())) {
122
			$content = $client->getResponseBody();
123
			$this->success = \in_array($code, [200, 201]) && $content;
124
			if ($this->success) {
125
				$this->setId(\App\Json::decode($content)['id']);
126
			}
127
		}
128
129
		return $this->success;
130
	}
131
132
	/**
133
	 * Get order ID.
134
	 *
135
	 * @return string
136
	 */
137
	public function getId()
138
	{
139
		return $this->id ?? '';
140
	}
141
142
	/**
143
	 * Get last error.
144
	 *
145
	 * @return string
146
	 */
147
	public function getError(): string
148
	{
149
		return $this->error ?? '';
150
	}
151
152
	/**
153
	 * Set product package ID.
154
	 *
155
	 * @param string $packageId
156
	 *
157
	 * @return self
158
	 */
159
	public function setPackageId(string $packageId): self
160
	{
161
		$this->packageId = $packageId;
0 ignored issues
show
Documentation Bug introduced by
It seems like $packageId of type string is incompatible with the declared type App\YetiForce\Product of property $packageId.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
162
163
		return $this;
164
	}
165
166
	/**
167
	 * set order ID.
168
	 *
169
	 * @param string $id
170
	 *
171
	 * @return self
172
	 */
173
	private function setId(string $id): self
174
	{
175
		$this->id = $id;
176
		return $this;
177
	}
178
179
	/**
180
	 * Get registration data.
181
	 *
182
	 * @return string[]
183
	 */
184
	private function getData(): array
185
	{
186
		return [
187
			'packageId' => $this->packageId,
188
			'company' => $this->data['name'],
189
			'city' => $this->data['city'],
190
			'vatId' => $this->data['vat_id'],
191
			'country' => $this->data['country'],
192
			'postCode' => $this->data['post_code'],
193
			'address' => $this->data['address'],
194
		];
195
	}
196
}
197