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

EmailVerification   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
eloc 46
c 1
b 0
f 0
dl 0
loc 150
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 14 3
A setCode() 0 5 1
A setType() 0 4 1
A postProcess() 0 9 3
A getData() 0 15 4
A setEmail() 0 5 1
A getError() 0 3 1
A setNewsletter() 0 5 1
1
<?php
2
/**
3
 * YetiForce admin email verification file.
4
 * Modifying this file or functions 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    Klaudia Łozowska <[email protected]>
11
 * @author    Radosław Skrzypczak <[email protected]>
12
 */
13
14
namespace App\YetiForce;
15
16
/**
17
 * YetiForce admin email verification class.
18
 */
19
class EmailVerification
20
{
21
	/** @var string URL */
22
	public const URL = 'https://api.yetiforce.eu/emails';
23
	/** Process identificator - init */
24
	public const PROCESS_INIT = 0;
25
	/** Process identificator - register */
26
	public const PROCESS_REGISTER = 1;
27
	/** @var string Endpoints */
28
	private const TYPES = [
29
		self::PROCESS_INIT => 'POST',
30
		self::PROCESS_REGISTER => 'PUT'
31
	];
32
33
	/** @var string|null Last eroor */
34
	protected ?string $error;
35
	/** @var int Type */
36
	protected int $type;
37
	/** @var bool Response result */
38
	protected bool $success;
39
	/** @var string E-mail address */
40
	private string $email;
41
	/** @var bool Newsletter agreement */
42
	private bool $newsletter;
43
	/** @var string Code */
44
	private string $token;
45
46
	/**
47
	 * Set type request.
48
	 *
49
	 * @param int $type Types {@see self::TYPES}
50
	 *
51
	 * @return $this
52
	 */
53
	public function setType(int $type): self
54
	{
55
		$this->type = $type;
56
		return $this;
57
	}
58
59
	/**
60
	 * Set e-mail address.
61
	 *
62
	 * @param string $email
63
	 *
64
	 * @return self
65
	 */
66
	public function setEmail(string $email): self
67
	{
68
		$this->email = $email;
69
70
		return $this;
71
	}
72
73
	/**
74
	 * Set newsletter agreement.
75
	 *
76
	 * @param bool $newsletter
77
	 *
78
	 * @return self
79
	 */
80
	public function setNewsletter(bool $newsletter): self
81
	{
82
		$this->newsletter = $newsletter;
83
84
		return $this;
85
	}
86
87
	/**
88
	 * Set code.
89
	 *
90
	 * @param string $code
91
	 *
92
	 * @return self
93
	 */
94
	public function setCode(string $code): self
95
	{
96
		$this->token = $code;
97
98
		return $this;
99
	}
100
101
	/**
102
	 * Request sending an email with a verification token.
103
	 *
104
	 * @return bool
105
	 */
106
	public function send(): bool
107
	{
108
		$this->success = false;
109
		$type = self::TYPES[$this->type];
110
		$client = new ApiClient();
111
		$client->send(self::URL, $type, ['form_params' => $this->getData()]);
112
		$this->error = $client->getError();
113
114
		if (409 === $client->getStatusCode() && false !== stripos($this->error, 'app')) {
115
			(new \App\YetiForce\Register())->recreate();
116
			throw new \App\Exceptions\AppException('ERR_RECREATE_APP_ACCESS');
117
		}
118
119
		return $this->success = 204 === $client->getStatusCode();
120
	}
121
122
	/**
123
	 * Get last error.
124
	 *
125
	 * @return string
126
	 */
127
	public function getError(): string
128
	{
129
		return $this->error ?? '';
130
	}
131
132
	/**
133
	 * Post processes.
134
	 *
135
	 * @return void
136
	 */
137
	public function postProcess(): void
138
	{
139
		if ($this->success && self::PROCESS_REGISTER === $this->type) {
140
			$fieldName = 'email';
141
			$recordModel = \Settings_Companies_Record_Model::getInstance();
142
			$fieldModel = $recordModel->getFieldInstanceByName($fieldName);
143
			$fieldModel->getUITypeModel()->validate($this->email, true);
144
			$recordModel->set($fieldName, $fieldModel->getDBValue($this->email));
0 ignored issues
show
Bug introduced by
$this->email of type string is incompatible with the type type expected by parameter $value of Vtiger_Field_Model::getDBValue(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
			$recordModel->set($fieldName, $fieldModel->getDBValue(/** @scrutinizer ignore-type */ $this->email));
Loading history...
145
			$recordModel->save();
146
		}
147
	}
148
149
	/**
150
	 * Get data for request.
151
	 *
152
	 * @return array
153
	 */
154
	private function getData(): array
155
	{
156
		$data = [
157
			'appId' => Register::getInstanceKey()
158
		];
159
160
		$reflect = new \ReflectionClass($this);
161
		foreach ($reflect->getProperties(\ReflectionProperty::IS_PRIVATE) as $property) {
162
			$property->setAccessible(true);
163
			if ($property->isInitialized($this) && null !== ($value = $property->getValue($this))) {
164
				$data[$property->getName()] = $value;
165
			}
166
		}
167
168
		return $data;
169
	}
170
}
171