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\MessageBird; |
16
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Notification\Client\Sms\Exception\SmsNotifierException; |
18
|
|
|
use Acme\App\Core\Port\Validation\PhoneNumber\PhoneNumberValidatorInterface; |
19
|
|
|
use Acme\App\Infrastructure\Notification\Client\Sms\AbstractClient; |
20
|
|
|
use libphonenumber\PhoneNumberUtil; |
21
|
|
|
use MessageBird\Client; |
22
|
|
|
use MessageBird\Exceptions\MessageBirdException; |
23
|
|
|
use MessageBird\Objects\Message; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Winfred Peereboom |
27
|
|
|
* @author Herberto Graca <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class MessageBirdClient extends AbstractClient |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var Client |
33
|
|
|
*/ |
34
|
|
|
private $client; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $sender; |
40
|
|
|
|
41
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
42
|
|
|
PhoneNumberValidatorInterface $phoneNumberValidator, |
43
|
|
|
PhoneNumberUtil $phoneNumberUtil, |
44
|
|
|
string $countryCode, |
45
|
|
|
Client $client, |
46
|
|
|
string $smsSender, |
47
|
|
|
string $defaultDestination = null |
48
|
|
|
) { |
49
|
|
|
parent::__construct($phoneNumberValidator, $phoneNumberUtil, $countryCode, $defaultDestination); |
50
|
|
|
$this->client = $client; |
51
|
|
|
$this->sender = $smsSender; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function triggerNotification(string $phoneNumber, string $content): void |
55
|
|
|
{ |
56
|
|
|
$message = new Message(); |
57
|
|
|
$message->originator = $this->sender; |
58
|
|
|
$message->recipients = [$phoneNumber]; |
59
|
|
|
$message->body = $content; |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
$this->client->messages->create($message); |
63
|
|
|
} catch (MessageBirdException $e) { |
64
|
|
|
throw new SmsNotifierException($e->getMessage(), 0, $e); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.