1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core_sms;
|
|
|
18 |
|
|
|
19 |
use coding_exception;
|
|
|
20 |
use Spatie\Cloneable\Cloneable;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Class gateway.
|
|
|
25 |
*
|
|
|
26 |
* @package core_sms
|
|
|
27 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
* @property-read int $id The id of the gateway in the database
|
|
|
30 |
* @property-read bool $enabled Whether the gateway is enabled
|
|
|
31 |
* @property-read stdClass $config The configuration for this instance
|
|
|
32 |
* @property string $name The name of the gateway config
|
|
|
33 |
*/
|
|
|
34 |
abstract class gateway {
|
|
|
35 |
use Cloneable;
|
|
|
36 |
|
|
|
37 |
/** @var int The maximum length of a message. */
|
|
|
38 |
protected const MESSAGE_LENGTH_LIMIT = 160 * 3;
|
|
|
39 |
|
|
|
40 |
/** @var stdClass The configuration for this instance */
|
|
|
41 |
public readonly stdClass $config;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Create a new gateway.
|
|
|
45 |
*
|
|
|
46 |
* @param bool $enabled Whether the gateway is enabled
|
|
|
47 |
* @param string $name The name of the gateway config
|
|
|
48 |
* @param string $config The configuration for this instance
|
|
|
49 |
* @param int|null $id The id of the gateway in the database
|
|
|
50 |
*/
|
|
|
51 |
public function __construct(
|
|
|
52 |
/** @var bool Whether the gateway is enabled */
|
|
|
53 |
public readonly bool $enabled,
|
|
|
54 |
/** @var string The name of the gateway config */
|
|
|
55 |
public string $name,
|
|
|
56 |
string $config,
|
|
|
57 |
/** @var null|int The ID of the gateway in the database, or null if it has not been persisted yet */
|
|
|
58 |
public readonly ?int $id = null,
|
|
|
59 |
) {
|
|
|
60 |
$this->config = json_decode($config);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Convert this object to a stdClass.
|
|
|
65 |
*
|
|
|
66 |
* @return stdClass
|
|
|
67 |
*/
|
|
|
68 |
public function to_record(): stdClass {
|
|
|
69 |
return (object) [
|
|
|
70 |
'id' => $this->id,
|
|
|
71 |
'name' => $this->name,
|
|
|
72 |
'gateway' => get_class($this),
|
|
|
73 |
'enabled' => $this->enabled,
|
|
|
74 |
'config' => json_encode($this->config),
|
|
|
75 |
];
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Send the given message.
|
|
|
80 |
*
|
|
|
81 |
* @param message $message
|
|
|
82 |
* @return message
|
|
|
83 |
*/
|
|
|
84 |
abstract public function send(message $message): message;
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Confirm whether this gateway can send the given message.
|
|
|
88 |
*
|
|
|
89 |
* @param message $message
|
|
|
90 |
* @return bool
|
|
|
91 |
*/
|
|
|
92 |
public function can_send(message $message): bool {
|
|
|
93 |
return $this->get_send_priority($message) > 0;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Get the priority of this gateway for sending the given message.
|
|
|
98 |
*
|
|
|
99 |
* A priority of 0 means that the gateway cannot send the message.
|
|
|
100 |
* Higher values are higher priority.
|
|
|
101 |
*
|
|
|
102 |
* This method is called frequently, so should be fast.
|
|
|
103 |
* If calculation is expensive the value should be cached.
|
|
|
104 |
*
|
|
|
105 |
* @param message $message
|
|
|
106 |
* @return int
|
|
|
107 |
*/
|
|
|
108 |
abstract public function get_send_priority(message $message): int;
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Update the status of the given message from the gateway.
|
|
|
112 |
*
|
|
|
113 |
* @param message $message
|
|
|
114 |
* @return message
|
|
|
115 |
* @throws coding_exception
|
|
|
116 |
*/
|
|
|
117 |
public function update_message_status(message $message): message {
|
|
|
118 |
if ($message->gatewayid !== $this->id) {
|
|
|
119 |
throw new \coding_exception('This gateway cannot update the status of this message');
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
return $message;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Update the statuses of the given messages from the gateway.
|
|
|
127 |
*
|
|
|
128 |
* @param message[] $messages
|
|
|
129 |
* @return message[]
|
|
|
130 |
*/
|
|
|
131 |
public function update_message_statuses(array $messages): array {
|
|
|
132 |
return array_map([$this, 'update_message_status'], $messages);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Truncates the given message to fit the constraints.
|
|
|
137 |
*
|
|
|
138 |
* @param string $message The message to be truncated.
|
|
|
139 |
* @return string The truncated message.
|
|
|
140 |
*/
|
|
|
141 |
public function truncate_message(string $message): string {
|
|
|
142 |
return \core_text::substr($message, 0, static::MESSAGE_LENGTH_LIMIT);
|
|
|
143 |
}
|
|
|
144 |
}
|