TwilioClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 6
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\Twilio;
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 Twilio\Exceptions\TwilioException;
22
use Twilio\Rest\Client;
23
24
/**
25
 * @author Nicolae Nichifor
26
 * @author Herberto Graca <[email protected]>
27
 */
28
final class TwilioClient extends AbstractClient
29
{
30
    /**
31
     * @var Client
32
     */
33
    private $client;
34
35
    /**
36
     * @var string
37
     */
38
    private $sender;
39
40 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
41
        PhoneNumberValidatorInterface $phoneNumberValidator,
42
        PhoneNumberUtil $phoneNumberUtil,
43
        string $countryCode,
44
        Client $client,
45
        string $sender,
46
        string $defaultDestination = null
47
    ) {
48
        parent::__construct($phoneNumberValidator, $phoneNumberUtil, $countryCode, $defaultDestination);
49
        $this->client = $client;
50
        $this->sender = $sender;
51
    }
52
53
    public function triggerNotification(string $phoneNumber, string $content): void
54
    {
55
        try {
56
            $this->client
57
                ->getAccount()
58
                ->messages
59
                ->create(
60
                    $phoneNumber,
61
                    [
62
                        'from' => $this->sender,
63
                        'body' => $content,
64
                    ]
65
                );
66
        } catch (TwilioException $e) {
67
            throw new SmsNotifierException($e->getMessage(), 0, $e);
68
        }
69
    }
70
}
71