Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 libphonenumber\NumberParseException;
20
use Spatie\Cloneable\Cloneable;
21
use ValueError;
22
 
23
/**
24
 * A Message used in an SMS.
25
 *
26
 * Note: This class is immutable. All properties are readonly.
27
 *       The class itself will likely become readonly when PHP 8.2 is the minimum requirement.
28
 *
29
 * @package    core_sms
30
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @property-read int $timecreated The time that the message was created
33
 * @property-read string $recipientnumber The recipient of the message
34
 * @property-read null|string $content The content of the message
35
 * @property-read string $component The component that owns the message
36
 * @property-read string $messagetype The type of message within the component
37
 * @property-read int|null $recipientuserid The user id of the recipient if one exists
38
 * @property-read bool $issensitive Whether this message contains sensitive information
39
 * @property-read int|null $id The id of the message in the database
40
 * @property-read message_status $status The status of the message
41
 * @property-read int|null $gatewayid The id of the gateway that sent the message
42
 */
43
class message {
44
    use Cloneable {
45
        with as private _with;
46
    }
47
 
48
    /** @var int The time that the message was created */
49
    public readonly int $timecreated;
50
 
51
    /**
52
     * Create a new message.
53
     *
54
     * @param string $recipientnumber The phone number of the message recipient
55
     * @param null|string $content The content of the message
56
     * @param string $component The component that owns the message
57
     * @param string $messagetype The type of message within the component
58
     * @param int|null $recipientuserid The user id of the recipient if one exists
59
     * @param bool $issensitive Whether this message contains sensitive information
60
     * @param int|null $id The id of the message in the database
61
     * @param message_status $status The status of the message
62
     * @param int|null $gatewayid The id of the gateway that sent the message
63
     * @param int|null $timecreated The time that the message was created
64
     */
65
    public function __construct(
66
        /** @var string The phone number of the message recipient */
67
        public readonly string $recipientnumber,
68
        /** @var null|string The content of the message */
69
        public readonly ?string $content,
70
        /** @var string The component that owns the message */
71
        public readonly string $component,
72
        /** @var string The type of message within the component */
73
        public readonly string $messagetype,
74
        /** @var int|null The user id of the recipient if one exists */
75
        public readonly ?int $recipientuserid,
76
        /** @var bool Whether this message contains sensitive information */
77
        public readonly bool $issensitive,
78
        /** @var null|int The id of the message in the database */
79
        public readonly ?int $id = null,
80
        /** @var message_status The status of the message */
81
        public readonly message_status $status = message_status::UNKNOWN,
82
        /** @var null|int The id of the gateway that sent the message */
83
        public readonly ?int $gatewayid = null,
84
        ?int $timecreated = null,
85
    ) {
86
        if ($timecreated === null) {
87
            $this->timecreated = \core\di::get(\core\clock::class)->now()->getTimestamp();
88
        } else {
89
            $this->timecreated = $timecreated;
90
        }
91
    }
92
 
93
    /**
94
     * Convert the message to a record.
95
     *
96
     * @return \stdClass
97
     */
98
    public function to_record(): \stdClass {
99
        $record = (object) [
100
            'recipientnumber' => $this->recipientnumber,
101
            'content' => $this->content,
102
            'component' => $this->component,
103
            'messagetype' => $this->messagetype,
104
            'recipientuserid' => $this->recipientuserid,
105
            'issensitive' => $this->issensitive,
106
            'status' => $this->status->value,
107
            'gatewayid' => $this->gatewayid,
108
            'timecreated' => $this->timecreated,
109
        ];
110
 
111
        if ($this->id !== null) {
112
            $record->id = $this->id;
113
        }
114
 
115
        return $record;
116
    }
117
 
118
    /**
119
     * Update the message properties.
120
     *
121
     * Note: The message is immutable.
122
     * When setting the status a new object will be returned.
123
     *
124
     * @param mixed ...$args
125
     * @return message
126
     */
127
    public function with(...$args): self {
128
        if (isset($this->id) && array_key_exists('id', $args)) {
129
            throw new \coding_exception('Message already has an id');
130
        }
131
 
132
        return $this->_with(...$args);
133
    }
134
 
135
    /**
136
     * Get the region code for this messages.
137
     *
138
     * @return null|string
139
     */
140
    public function get_region(): ?string {
141
        $pnu = \core\di::get(\libphonenumber\PhoneNumberUtil::class);
142
        try {
143
            $recipient = $pnu->parse($this->recipientnumber);
144
        } catch (NumberParseException $e) {
145
            // Note: Throw errors which are not specific to libphonenumber here.
146
            // This is to avoid hard-trying use of this library.
147
            throw new ValueError(
148
                new \lang_string('phonenumbernotvalid', 'sms', ['message' => $e->getMessage()]),
149
                $e->getCode(),
150
                $e,
151
            );
152
        }
153
 
154
        return $pnu->getRegionCodeForNumber($recipient);
155
    }
156
 
157
    /**
158
     * Check if the message has been sent.
159
     *
160
     * @return bool
161
     */
162
    public function is_sent(): bool {
163
        return $this->status->is_sent();
164
    }
165
}