1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Service; |
20
|
|
|
|
21
|
|
|
use GuzzleHttp\Client; |
22
|
|
|
use Psr\Log\LoggerInterface; |
23
|
|
|
use Surfnet\StepupBundle\Command\SendSmsCommand; |
24
|
|
|
use Surfnet\StepupBundle\Http\JsonHelper; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Sends SMSes by calling the Gateway API over HTTP. |
28
|
|
|
*/ |
29
|
|
|
class GatewayApiSmsService implements SmsService |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var Client |
33
|
|
|
*/ |
34
|
|
|
private $guzzleClient; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var LoggerInterface |
38
|
|
|
*/ |
39
|
|
|
private $logger; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Client $guzzleClient A Guzzle client configured with the SMS API base URL and authentication. |
43
|
|
|
* @param LoggerInterface $logger |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Client $guzzleClient, LoggerInterface $logger) |
46
|
|
|
{ |
47
|
|
|
$this->guzzleClient = $guzzleClient; |
48
|
|
|
$this->logger = $logger; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function sendSms(SendSmsCommand $command) |
52
|
|
|
{ |
53
|
|
|
$this->logger->info('Requesting Gateway to send SMS'); |
54
|
|
|
|
55
|
|
|
$body = [ |
56
|
|
|
'requester' => ['institution' => $command->institution, 'identity' => $command->identity], |
57
|
|
|
'message' => [ |
58
|
|
|
'originator' => $command->originator, |
59
|
|
|
'recipient' => $command->recipient, |
60
|
|
|
'body' => $command->body |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
$response = $this->guzzleClient->post('api/send-sms', ['json' => $body]); |
64
|
|
|
$statusCode = $response->getStatusCode(); |
65
|
|
|
|
66
|
|
|
if ($statusCode != 200) { |
67
|
|
|
$this->logger->error( |
68
|
|
|
sprintf('SMS sending failed, error: [%s] %s', $response->getStatusCode(), $response->getReasonPhrase()), |
69
|
|
|
['http-body' => $response->getBody() ? $response->getBody()->getContents() : '',] |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$result = JsonHelper::decode((string) $response->getBody()); |
77
|
|
|
} catch (\RuntimeException $e) { |
78
|
|
|
$this->logger->error('SMS sending failed; server responded with malformed JSON.'); |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!isset($result['status'])) { |
84
|
|
|
$this->logger->error('SMS sending failed; server responded without status report.'); |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($result['status'] !== 'OK') { |
90
|
|
|
$this->logger->error('SMS sending failed; server responded with non-OK status report.'); |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|