|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Notification\Client\Sms; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Notification\Client\Sms\Sms; |
|
18
|
|
|
use Acme\App\Core\Port\Notification\Client\Sms\SmsNotifierInterface; |
|
19
|
|
|
use Acme\App\Core\Port\Validation\PhoneNumber\PhoneNumberCouldNotBeParsedException; |
|
20
|
|
|
use Acme\App\Core\Port\Validation\PhoneNumber\PhoneNumberException; |
|
21
|
|
|
use Acme\App\Core\Port\Validation\PhoneNumber\PhoneNumberValidatorInterface; |
|
22
|
|
|
use libphonenumber\NumberParseException; |
|
23
|
|
|
use libphonenumber\PhoneNumber; |
|
24
|
|
|
use libphonenumber\PhoneNumberFormat; |
|
25
|
|
|
use libphonenumber\PhoneNumberUtil; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Nicolae Nichifor |
|
29
|
|
|
* @author Herberto Graca <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class AbstractClient implements SmsNotifierInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var PhoneNumberValidatorInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $phoneNumberValidator; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var PhoneNumberUtil |
|
40
|
|
|
*/ |
|
41
|
|
|
private $phoneNumberUtil; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private $countryCode; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $defaultDestination; |
|
52
|
|
|
|
|
53
|
|
|
public function __construct( |
|
54
|
|
|
PhoneNumberValidatorInterface $phoneNumberValidator, |
|
55
|
|
|
PhoneNumberUtil $phoneNumberUtil, |
|
56
|
|
|
string $countryCode, |
|
57
|
|
|
string $defaultDestination = null |
|
58
|
|
|
) { |
|
59
|
|
|
$this->phoneNumberValidator = $phoneNumberValidator; |
|
60
|
|
|
$this->phoneNumberUtil = $phoneNumberUtil; |
|
61
|
|
|
$this->countryCode = $countryCode; |
|
62
|
|
|
$this->defaultDestination = $defaultDestination; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @throws PhoneNumberException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function sendNotification(Sms $smsNotification): void |
|
69
|
|
|
{ |
|
70
|
|
|
$phoneNumber = $this->formatToE164($this->defaultDestination ?? $smsNotification->getPhoneNumber()); |
|
71
|
|
|
|
|
72
|
|
|
$this->triggerNotification($phoneNumber, $smsNotification->getContent()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
abstract public function triggerNotification(string $phoneNumber, string $content): void; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
* |
|
80
|
|
|
* @throws PhoneNumberException |
|
81
|
|
|
*/ |
|
82
|
|
|
private function formatToE164(string $phoneNumber): string |
|
83
|
|
|
{ |
|
84
|
|
|
$phoneNumberObject = $this->parsePhoneNumberOrThrowException($phoneNumber); |
|
85
|
|
|
|
|
86
|
|
|
return $this->phoneNumberUtil->format($phoneNumberObject, PhoneNumberFormat::E164); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @throws PhoneNumberException |
|
91
|
|
|
*/ |
|
92
|
|
|
private function parsePhoneNumberOrThrowException(string $phoneNumber): PhoneNumber |
|
93
|
|
|
{ |
|
94
|
|
|
$this->phoneNumberValidator->validate($phoneNumber); |
|
95
|
|
|
|
|
96
|
|
|
try { |
|
97
|
|
|
return $this->phoneNumberUtil->parse($phoneNumber, $this->countryCode); |
|
98
|
|
|
} catch (NumberParseException $exception) { |
|
99
|
|
|
throw new PhoneNumberCouldNotBeParsedException($phoneNumber); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|