Passed
Push — master ( ac4415...6c0960 )
by Zing
04:55
created

Connector::formatPhoneNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Zing\LaravelSms\Connectors;
4
5
use Exception;
6
use GrahamCampbell\Manager\ConnectorInterface;
7
use Illuminate\Support\Facades\Log;
8
use function is_array;
9
use Overtrue\EasySms\Contracts\GatewayInterface;
10
use Overtrue\EasySms\Contracts\MessageInterface;
11
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
12
use Overtrue\EasySms\Support\Config;
13
use function trim;
14
use Zing\LaravelSms\Exceptions\CouldNotSendNotification;
15
use Zing\LaravelSms\Exceptions\InvalidArgumentException;
16
17
class Connector implements ConnectorInterface
18
{
19
    /** @var GatewayInterface */
20
    protected $driver;
21
22
    /** @var \Overtrue\EasySms\Support\Config */
23
    protected $config;
24
25
    /**
26
     * Connector constructor.
27
     *
28
     * @param $config
29
     */
30 22
    public function __construct($config)
31
    {
32 22
        $this->config = new Config($config);
33 22
    }
34
35 20
    public function connect(array $config)
36
    {
37 20
        if (! isset($config['driver'])) {
38 1
            throw new InvalidArgumentException('A driver must be specified.');
39
        }
40 19
        $driver = $config['driver'];
41 19
        if (! class_exists($driver) || ! in_array(GatewayInterface::class, class_implements($driver), true)) {
42 1
            throw new InvalidArgumentException("Unsupported driver [{$config['driver']}].");
43
        }
44
45 18
        $this->driver = new $driver($config);
46
47 18
        return $this;
48
    }
49
50
    /**
51
     * @param string|\Overtrue\EasySms\Contracts\PhoneNumberInterface $number
52
     *
53
     * @return \Overtrue\EasySms\PhoneNumber
54
     */
55 20
    protected function formatPhoneNumber($number)
56
    {
57 20
        if ($number instanceof PhoneNumberInterface) {
58 9
            return $number;
59
        }
60
61 11
        return new \Overtrue\EasySms\PhoneNumber(trim($number));
0 ignored issues
show
Bug introduced by
trim($number) of type string is incompatible with the type integer expected by parameter $numberWithoutIDDCode of Overtrue\EasySms\PhoneNumber::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        return new \Overtrue\EasySms\PhoneNumber(/** @scrutinizer ignore-type */ trim($number));
Loading history...
62
    }
63
64
    /**
65
     * @param array|string|\Overtrue\EasySms\Contracts\MessageInterface $message
66
     *
67
     * @return \Overtrue\EasySms\Contracts\MessageInterface
68
     */
69 20
    protected function formatMessage($message)
70
    {
71 20
        if (! ($message instanceof MessageInterface)) {
72 6
            if (! is_array($message)) {
73
                $message = [
74 5
                    'content' => $message,
75 5
                    'template' => $message,
76
                ];
77
            }
78
79 6
            $message = new \Overtrue\EasySms\Message($message);
80
        }
81
82 20
        return $message;
83
    }
84
85
    /**
86
     * @param mixed $number
87
     * @param mixed $message
88
     *
89
     * @return bool|mixed
90
     *
91
     * @throws \Zing\LaravelSms\Exceptions\CouldNotSendNotification
92
     * @throws \Throwable
93
     */
94 20
    public function send($number, $message)
95
    {
96
        try {
97 20
            $number = $this->formatPhoneNumber($number);
98 20
            $message = $this->formatMessage($message);
99
100 20
            $this->sending($number, $message);
101 18
            Log::debug(sprintf('number: %s, message: "%s", template: "%s", data: %s, type: %s', $number, $message->getContent($this->driver), $message->getTemplate($this->driver), json_encode($message->getData($this->driver)), $message->getMessageType()));
102 18
            $result = $this->driver->send($number, $message, $this->config);
103 18
            Log::debug(sprintf('number: %s, message: "%s", template: "%s", data: %s, type: %s', $number, $message->getContent($this->driver), $message->getTemplate($this->driver), json_encode($message->getData($this->driver)), $message->getMessageType()), (array) $result);
104 18
            $this->sent($number, $message, $result);
105
106 18
            return $result;
107 2
        } catch (Exception $exception) {
108 2
            throw CouldNotSendNotification::captureExceptionInDriver($exception);
109
        }
110
    }
111
112 18
    public function sending($number, $message)
0 ignored issues
show
Unused Code introduced by
The parameter $number is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

112
    public function sending(/** @scrutinizer ignore-unused */ $number, $message)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

112
    public function sending($number, /** @scrutinizer ignore-unused */ $message)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114 18
    }
115
116 18
    public function sent($number, $message, $result)
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
    public function sent($number, /** @scrutinizer ignore-unused */ $message, $result)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $result is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
    public function sent($number, $message, /** @scrutinizer ignore-unused */ $result)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $number is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
    public function sent(/** @scrutinizer ignore-unused */ $number, $message, $result)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118 18
    }
119
}
120