Issues (9)

src/SendberryApi.php (5 issues)

1
<?php
2
3
namespace NotificationChannels\Sendberry;
4
5
use GuzzleHttp\Client as HttpClient;
0 ignored issues
show
The type GuzzleHttp\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use NotificationChannels\Sendberry\Exceptions\CouldNotSendNotification;
7
use NotificationChannels\Sendberry\Exceptions\TransportException;
8
use Exception;
9
10
class SendberryApi
11
{
12
    /** @var HttpClient */
13
    protected $client;
14
    protected $authKey;
15
    protected $username;
16
    protected $password;
17
    protected $from;
18
    protected $webhook;
19
    protected $testMode;
20
21
    protected $baseUri = 'https://api.sendberry.com/';
22
23
    public function __construct(
24
        string $authKey,
25
        string $username,
26
        string $password,
27
        $from,
28
        $webhook,
29
        $testMode
30
    ) {
31
        $this->authKey = $authKey;
32
        $this->username = $username;
33
        $this->password = $password;
34
        $this->from = $from;
35
        $this->webhook = $webhook;
36
        $this->testMode = $testMode;
37
38
        $this->client = new HttpClient([
39
            'timeout' => 5,
40
            'connect_timeout' => 5,
41
        ]);
42
    }
43
44
    /**
45
     * @param $recipient
46
     * @param SendberryMessage $message
47
     * @return array
48
     */
49
    public function sendMessage($recipient, SendberryMessage $message)
50
    {
51
        if (!preg_match('/^[+]+[1-9][0-9]{9,14}$/', $this->from)) {
52
            if ($this->from === '') {
53
                throw CouldNotSendNotification::missingFrom();
54
            }
55
56
            if (!preg_match('/^[a-zA-Z0-9 ]+$/', $this->from)) {
57
                throw CouldNotSendNotification::invalidFrom();
58
            }
59
        }
60
61
        $body = [
62
            'key' => $this->authKey,
63
            'name' => $this->username,
64
            'password' => $this->password,
65
            'content' => $message->content,
66
            'from' => $this->from,
67
            'to' => [$recipient],
68
            'response' => 'JSON',
69
        ];
70
71
        if ($message->time) {
72
            $body['time'] = $message->time;
73
        }
74
75
        if ($message->date) {
76
            $body['date'] = $message->date;
77
        }
78
79
        if ($webhook = $message->webhook || $this->webhook) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $webhook = ($message->webhook || $this->webhook), Probably Intended Meaning: ($webhook = $message->webhook) || $this->webhook
Loading history...
80
            $body['webhook'] = $webhook;
81
        }
82
83
        if (!is_null($message->test)) {
0 ignored issues
show
The condition is_null($message->test) is always false.
Loading history...
84
            $this->testMode = $message->test;
85
        }
86
87
        $url = $this->baseUri . 'SMS/SEND';
88
89
        return $this->getResponse($url, $body);
90
    }
91
92
    /**
93
     * @param string $url
94
     * @param array $body
95
     * @return array
96
     */
97
    public function getResponse($url, $body)
98
    {
99
        if ($this->testMode) {
100
            return [
101
                'url' => $url,
102
                'body' => $body,
103
                'info' => 'sendberry.test_mode.ok',
104
            ];
105
        }
106
107
        $response = $this->client->request('POST', $url, [
108
            'json' => $body
109
        ]);
110
111
        try {
112
            $statusCode = $response->getStatusCode();
113
        } catch (TransportException $e) {
114
            throw new TransportException('Could not reach the remote Sendberry server.', $response, 0, $e);
0 ignored issues
show
0 of type integer is incompatible with the type Throwable|null expected by parameter $previous of NotificationChannels\Sen...xception::__construct(). ( Ignorable by Annotation )

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

114
            throw new TransportException('Could not reach the remote Sendberry server.', $response, /** @scrutinizer ignore-type */ 0, $e);
Loading history...
The call to NotificationChannels\Sen...xception::__construct() has too many arguments starting with $e. ( Ignorable by Annotation )

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

114
            throw /** @scrutinizer ignore-call */ new TransportException('Could not reach the remote Sendberry server.', $response, 0, $e);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
115
        }
116
117
        if ($statusCode !== 200) {
118
            throw new TransportException('Unable to send the SMS.', $response);
119
        }
120
121
        $responseArr = json_decode($response->getBody()->getContents(), true);
122
123
        if (isset($responseArr['status']) && $responseArr['status'] !== 'ok') {
124
            throw new TransportException(sprintf("Unable to send the SMS. \n%s\n.", implode("\n", $responseArr['message'])));
125
        }
126
127
        return $responseArr;
128
    }
129
}
130