Passed
Push — developer ( 43b188...198fe6 )
by Radosław
18:42
created

Report::get()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 18
rs 8.8333
cc 7
nc 3
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 Report extends \Api\SMS\BaseAction
20
{
21
	/** {@inheritdoc}  */
22
	public $allowedMethod = ['GET'];
23
24
	/** @var string Module name */
25
	private $moduleName = 'SMSNotifier';
26
27
	/**
28
	 * Get status for record by code.
29
	 *
30
	 * Statuses from api:
31
	 *  402 => 'EXPIRED',
32
	 *	403 => 'SENT',
33
	 *	404 => 'DELIVERED',
34
	 *	405 => 'UNDELIVERED',
35
	 *	406 => 'FAILED',
36
	 *	407 => 'REJECTED',
37
	 *	408 => 'UNKNOWN',
38
	 *	409 => 'QUEUE',
39
	 *	410 => 'ACCEPTED',
40
	 *	411 => 'RENEWAL',
41
	 *	412 => 'STOP'.
42
	 */
43
	private const STATUSES = [
44
		402 => 'PLL_FAILED',
45
		403 => 'PLL_SENT',
46
		404 => 'PLL_DELIVERED',
47
		405 => 'PLL_FAILED',
48
		406 => 'PLL_FAILED',
49
		407 => 'PLL_FAILED',
50
		408 => 'PLL_SENT',
51
		410 => 'PLL_DELIVERED',
52
	];
53
54
	/** {@inheritdoc}  */
55
	protected function checkPermission(): void
56
	{
57
		parent::checkPermission();
58
		if (!$this->controller->request->getByType('MsgId', \App\Purifier::ALNUM) || !$this->controller->request->getInteger('status') || !$this->controller->request->getByType('to', \App\Purifier::ALNUM)) {
59
			throw new \Api\Core\Exception('No permission - wrong data', 401);
60
		}
61
	}
62
63
	/** {@inheritdoc}  */
64
	protected function checkPermissionToModule(): void
65
	{
66
		if (!\Api\Core\Module::checkModuleAccess($this->moduleName)) {
67
			throw new \Api\Core\Exception('No permissions for module', 403);
68
		}
69
	}
70
71
	/**
72
	 * Add record.
73
	 *
74
	 * @return array
75
	 *
76
	 * @OA\Get(
77
	 *		path="/webservice/SMS/SMSAPI/Report",
78
	 *		summary="Report for sms",
79
	 *		tags={"SMSAPI"},
80
	 *		externalDocs={
81
	 *			"description" : "SMSApi Documentation",
82
	 *			"url" : "https://www.smsapi.pl/docs"
83
	 * 		},
84
	 * 		security={
85
	 *			{"ApiKeyAuth" : {}, "token" : {}}
86
	 *  	},
87
	 *		@OA\Response(
88
	 *				response=200,
89
	 *				description="Result",
90
	 *				@OA\JsonContent(ref="#/components/schemas/SMS_SMSAPI_Post_Report")
91
	 *		),
92
	 *		@OA\Response(
93
	 *				response=401,
94
	 *				description="`No sent token` OR `Invalid token` or `data provided in the request`",
95
	 *		),
96
	 *		@OA\Response(
97
	 *				response=403,
98
	 *				description="No permissions for module",
99
	 *		),
100
	 *		@OA\Response(
101
	 *				response=405,
102
	 *				description="Method Not Allowed",
103
	 *		),
104
	 * ),
105
	 * @OA\Schema(
106
	 *		schema="SMS_SMSAPI_Post_Report",
107
	 *		title="Response",
108
	 *		description="Response",
109
	 *		type="string",
110
	 *		example="OK"
111
	 *	),
112
	 */
113
	public function get()
114
	{
115
		$recordIds = $this->controller->request->getArray('idx', \App\Purifier::INTEGER);
116
		$statuses = $this->controller->request->getArray('status', \App\Purifier::INTEGER);
117
		$phoneNumbers = $this->controller->request->getArray('to', \App\Purifier::ALNUM);
118
		$provider = \App\Integrations\SMSProvider::getProviderByName('SMSAPI');
119
		foreach ($recordIds as $key => $recordId) {
120
			$phoneNumber = $phoneNumbers[$key] ?? null;
121
			if (\App\Record::isExists($recordId, $this->moduleName)
122
				&& ($recordModel = \Vtiger_Record_Model::getInstanceById($recordId, $this->moduleName))->isEditable()
123
				&& ($recordModel->isEmpty('phone') || $phoneNumber === $provider->setPhone($recordModel->get('phone'))->get('to'))
124
				&& $recordModel->set('smsnotifier_status', static::STATUSES[$statuses[$key]] ?? 'PLL_UNDEFINED')->getPreviousValue()
125
			) {
126
				$recordModel->save();
127
			}
128
		}
129
130
		echo 'OK';
131
	}
132
}
133