|
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\ClientInterface; |
|
22
|
|
|
use Psr\Log\LoggerInterface; |
|
23
|
|
|
use Surfnet\StepupBundle\Command\SendSmsCommand; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Sends SMSes by calling the Gateway API over HTTP. |
|
27
|
|
|
*/ |
|
28
|
|
|
class GatewayApiSmsService implements SmsService |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var ClientInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $guzzleClient; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var LoggerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $logger; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param ClientInterface $guzzleClient A Guzzle client configured with the SMS API base URL and authentication. |
|
42
|
|
|
* @param LoggerInterface $logger |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(ClientInterface $guzzleClient, LoggerInterface $logger) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->guzzleClient = $guzzleClient; |
|
47
|
|
|
$this->logger = $logger; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function sendSms(SendSmsCommand $command) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->logger->info('Requesting Gateway to send SMS'); |
|
53
|
|
|
|
|
54
|
|
|
$body = [ |
|
55
|
|
|
'requester' => ['institution' => $command->institution, 'identity' => $command->identity], |
|
56
|
|
|
'message' => [ |
|
57
|
|
|
'originator' => $command->originator, |
|
58
|
|
|
'recipient' => $command->recipient, |
|
59
|
|
|
'body' => $command->body |
|
60
|
|
|
], |
|
61
|
|
|
]; |
|
62
|
|
|
$response = $this->guzzleClient->post('api/send-sms', ['json' => $body, 'exceptions' => false]); |
|
63
|
|
|
$statusCode = $response->getStatusCode(); |
|
64
|
|
|
|
|
65
|
|
|
if ($statusCode != 200) { |
|
66
|
|
|
$this->logger->error( |
|
67
|
|
|
sprintf('SMS sending failed, error: [%s] %s', $response->getStatusCode(), $response->getReasonPhrase()), |
|
68
|
|
|
['http-body' => $response->getBody() ? $response->getBody()->getContents() : '',] |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
$result = $response->json(); |
|
76
|
|
|
} catch (\RuntimeException $e) { |
|
77
|
|
|
$this->logger->error('SMS sending failed; server responded with malformed JSON.'); |
|
78
|
|
|
|
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (!isset($result['status'])) { |
|
83
|
|
|
$this->logger->error('SMS sending failed; server responded without status report.'); |
|
84
|
|
|
|
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($result['status'] !== 'OK') { |
|
89
|
|
|
$this->logger->error('SMS sending failed; server responded with non-OK status report.'); |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|