Passed
Push — developer ( 222604...e778d2 )
by Radosław
18:37
created

Reception::post()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 25
rs 9.3554
cc 5
nc 2
nop 0
1
<?php
2
/**
3
 * The file contains: Report operations.
4
 *
5
 * @package Api
6
 *
7
 * @copyright YetiForce S.A.
8
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Radosław Skrzypczak <[email protected]>
10
 */
11
12
namespace Api\SMS\SMSAPI;
13
14
use OpenApi\Annotations as OA;
0 ignored issues
show
Bug introduced by
The type OpenApi\Annotations 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...
15
16
/**
17
 * Report class.
18
 */
19
class Reception extends \Api\SMS\BaseAction
20
{
21
	/** {@inheritdoc}  */
22
	public $allowedMethod = ['POST'];
23
24
	/** @var string Module name */
25
	private $moduleName = 'SMSNotifier';
26
27
	/** {@inheritdoc}  */
28
	protected function checkPermission(): void
29
	{
30
		parent::checkPermission();
31
		if ($this->controller->request->isEmpty('MsgId', true)) {
32
			throw new \Api\Core\Exception('No permission - wrong data', 401);
33
		}
34
	}
35
36
	/** {@inheritdoc}  */
37
	protected function checkPermissionToModule(): void
38
	{
39
		if (!\Api\Core\Module::checkModuleAccess($this->moduleName) || !\App\Privilege::isPermitted($this->moduleName, 'CreateView') || !($provider = \App\Integrations\SMSProvider::getDefaultProvider()) || 'SMSAPI' !== $provider->getName()) {
40
			throw new \Api\Core\Exception('No permissions for module', 403);
41
		}
42
	}
43
44
	/**
45
	 * Add record.
46
	 *
47
	 * @return array
48
	 *
49
	 * @OA\Get(
50
	 *		path="/webservice/SMS/SMSAPI/Reception",
51
	 *		summary="Receipt of SMS",
52
	 *		tags={"SMSAPI"},
53
	 *		externalDocs={
54
	 *			"description" : "SMSApi Documentation",
55
	 *			"url" : "https://www.smsapi.pl/docs"
56
	 * 		},
57
	 * 		security={
58
	 *			{"ApiKeyAuth" : {}, "token" : {}}
59
	 *  	},
60
	 *		@OA\Response(
61
	 *				response=200,
62
	 *				description="Result",
63
	 *				@OA\JsonContent(ref="#/components/schemas/SMS_SMSAPI_Post_Reception")
64
	 *		),
65
	 *		@OA\Response(
66
	 *				response=401,
67
	 *				description="`No sent token` OR `Invalid token` OR `wrong data provided in the request`",
68
	 *		),
69
	 *		@OA\Response(
70
	 *				response=403,
71
	 *				description="No permissions for module",
72
	 *		),
73
	 *		@OA\Response(
74
	 *				response=405,
75
	 *				description="Method Not Allowed",
76
	 *		),
77
	 * ),
78
	 * @OA\Schema(
79
	 *		schema="SMS_SMSAPI_Post_Reception",
80
	 *		title="Response",
81
	 *		description="Response",
82
	 *		type="string",
83
	 *		example="OK"
84
	 *	),
85
	 */
86
	public function post()
87
	{
88
		$msgId = $this->controller->request->getByType('MsgId', \App\Purifier::ALNUM);
89
		$message = $this->controller->request->getByType('sms_text', \App\Purifier::HTML);
90
		$smsFrom = $this->controller->request->getByType('sms_from', \App\Purifier::DIGITS);
91
92
		$provider = \App\Integrations\SMSProvider::getProviderByName('SMSAPI');
93
		$queryGenerator = (new \App\QueryGenerator($this->moduleName));
94
		$recordId = $queryGenerator->setFields(['id'])->addCondition('msgid', $msgId, 'e')->createQuery()->scalar();
95
96
		if ($recordId && \App\Record::isExists($recordId, $this->moduleName)
97
			&& ($recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $this->moduleName))
98
			&& $smsFrom === $provider->setPhone($recordModel->get('phone'))->get('to')
99
		) {
100
			$newRecordModel = \Vtiger_Record_Model::getCleanInstance($this->moduleName);
101
			$msgField = $newRecordModel->getField('message');
102
			$newRecordModel->set($msgField->getName(), $msgField->getDBValue($message))
103
				->set('parentid', $recordModel->getId())
104
				->set('related_to', $recordModel->get('related_to'))
105
				->set('smsnotifier_status', 'PLL_REPLY')
106
				->set('phone', $recordModel->get('phone'));
107
			$newRecordModel->save();
108
		}
109
110
		echo 'OK';
111
	}
112
}
113