SMSCClient::sendRequest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 19
ccs 0
cts 15
cp 0
crap 12
rs 9.6333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Author: Facundo J Gonzalez
4
 * Date: 17/11/2017.
5
 */
6
7
namespace NotificationChannels\SMSC\Clients\Http;
8
9
use GuzzleHttp\Client as GuzzleHttpClient;
10
use NotificationChannels\SMSC\SMSCMessageInterface;
11
use NotificationChannels\SMSC\Clients\SMSCClientInterface;
12
use NotificationChannels\SMSC\Exceptions\CouldNotBootClient;
13
use NotificationChannels\SMSC\Clients\SMSCApiResponseInterface;
14
use NotificationChannels\SMSC\Exceptions\CouldNotSendNotification;
15
16
/**
17
 * Class SMSCClient.
18
 */
19
final class SMSCClient implements SMSCClientInterface
20
{
21
    /**
22
     * The end point.
23
     *
24
     * @var string
25
     */
26
    private static $endPoint;
27
28
    /**
29
     * The guzzle http client.
30
     *
31
     * @var GuzzleHttpClient
32
     */
33
    protected $httpClient;
34
35
    /**
36
     * The request parameters.
37
     *
38
     * @var array
39
     */
40
    protected $requestParams = [];
41
42
    /**
43
     * Initialize the dependencies.
44
     *
45
     * @param GuzzleHttpClient $client
46
     * @param string           $endpoint
47
     * @param string           $alias
48
     * @param string           $apikey
49
     */
50
    public function __construct(GuzzleHttpClient $client, $endpoint, $alias, $apikey)
51
    {
52
        $this->httpClient = $client;
53
        $this->boot($endpoint, $alias, $apikey);
54
    }
55
56
    /**
57
     * Load the sms service specific configurations.
58
     *
59
     * @param  string $endpoint
60
     * @param  string $alias
61
     * @param  string $apikey
62
     * @return void
63
     * @throws CouldNotBootClient
64
     */
65
    protected function boot($endpoint, $alias, $apikey)
66
    {
67
        if (! $this->hasCredentials($endpoint, $alias, $apikey)) {
68
            throw CouldNotBootClient::missingCredentials();
69
        }
70
71
        self::$endPoint = $endpoint;
72
        $this->requestParams['alias'] = $alias;
73
        $this->requestParams['apikey'] = $apikey;
74
        $this->requestParams['cmd'] = 'enviar';
75
    }
76
77
    /**
78
     * Add an sms message to request.
79
     *
80
     * @param  SMSCMessageInterface $smsMessage
81
     * @return void
82
     */
83
    public function addToRequest(SMSCMessageInterface $smsMessage)
84
    {
85
        $smsParams = array_filter($smsMessage->toRequestParams());
86
87
        $this->requestParams = array_merge($this->requestParams, $smsParams);
88
    }
89
90
    /**
91
     * Send the client request to the service.
92
     *
93
     * @throws CouldNotSendNotification If SMS Api returns false.
94
     * @return SMSCApiResponseInterface
95
     */
96
    public function sendRequest()
97
    {
98
        $guzzleResponse = $this->httpClient->request('GET', self::$endPoint, [
99
            'query' => $this->requestParams,
100
        ]);
101
102
        if ($guzzleResponse->getStatusCode() != 200) {
103
            throw CouldNotSendNotification::apiFailed($guzzleResponse->getReasonPhrase());
104
        }
105
106
        $response = new SMSCHttpApiResponse((string) $guzzleResponse->getBody());
107
108
        if (! $response->isSuccess()) {
109
            $message = $response->errorMessage().'['.$response->errorCode().']';
110
            throw CouldNotSendNotification::apiFailed($message);
111
        }
112
113
        return $response;
114
    }
115
116
    /**
117
     * Validate the configuration provided api credentials.
118
     *
119
     * @param  string $endpoint
120
     * @param  string $alias
121
     * @param  string $apikey
122
     * @throws CouldNotBootClient
123
     * @return bool
124
     */
125
    private function hasCredentials($endpoint, $alias, $apikey)
126
    {
127
        return $endpoint != '' && $alias != '' && $apikey != '';
128
    }
129
}
130