Passed
Push — master ( 257e17...004729 )
by Tim
02:49
created

OTP::sanitizeMobilePhoneNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Utilities for SMS-based OTP.
5
 *
6
 * @package tvdijen/simplesamlphp-module-cmdotcom
7
 */
8
9
declare(strict_types=1);
10
11
namespace SimpleSAML\Module\cmdotcom\Utils;
12
13
use CMText\TextClient;
14
use CMText\TextClientResult;
15
use libphonenumber\PhoneNumberFormat;
16
use libphonenumber\PhoneNumberUtil;
17
use SimpleSAML\Assert\Assert;
18
use UnexpectedValueException;
19
20
class OTP
21
{
22
    /**
23
     * Send OTP SMS
24
     *
25
     * @param string $code
26
     * @param string $recipient
27
     * @return \CMText\TextClientResult
28
     */
29
    public function sendMessage(string $api_key, string $code, string $recipient, string $originator): TextClientResult
30
    {
31
        $client = new TextClient($api_key);
32
        $result = $client->SendMessage($code, $originator, [$recipient]);
33
        return $result;
34
    }
35
36
37
    /**
38
     * Generate a 6-digit random code
39
     *
40
     * @return string
41
     */
42
    public function generateOneTimePassword(): string
43
    {
44
        $code = sprintf("%06d", mt_rand(10000, 999999));
45
        $padded = str_pad($code, 6, '0', STR_PAD_LEFT);
46
47
        return $padded;
48
    }
49
}
50