1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the SmsGate package |
7
|
|
|
* |
8
|
|
|
* (c) SmsGate |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace SmsGate\Adapter; |
15
|
|
|
|
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Psr\Log\LogLevel; |
18
|
|
|
use SmsGate\Exception\SendSmsExceptionInterface; |
19
|
|
|
use SmsGate\Message; |
20
|
|
|
use SmsGate\Phone; |
21
|
|
|
use SmsGate\ResultCollection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The decorator of adapter for write result to log. |
25
|
|
|
* |
26
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class LoggingAdapterDecorator implements AdapterInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var AdapterInterface |
32
|
|
|
*/ |
33
|
|
|
private $originAdapter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var |
37
|
|
|
*/ |
38
|
|
|
private $logger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* |
43
|
|
|
* @param AdapterInterface $originAdapter |
44
|
|
|
* @param LoggerInterface $logger |
45
|
|
|
*/ |
46
|
2 |
|
public function __construct(AdapterInterface $originAdapter, LoggerInterface $logger) |
47
|
|
|
{ |
48
|
2 |
|
$this->originAdapter = $originAdapter; |
49
|
2 |
|
$this->logger = $logger; |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function send(Message $message, Phone ...$recipients): ResultCollection |
56
|
|
|
{ |
57
|
2 |
|
$phoneNumbers = array_map(function (Phone $recipient) { |
58
|
2 |
|
return $recipient->getValue(); |
59
|
2 |
|
}, $recipients); |
60
|
|
|
|
61
|
|
|
$this->logger->log(LogLevel::DEBUG, 'Start send sms.', [ |
62
|
|
|
'numbers' => $phoneNumbers, |
63
|
|
|
'message' => [ |
64
|
|
|
'text' => $message->getMessage(), |
65
|
|
|
'sender' => $message->getSender(), |
66
|
|
|
], |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$result = $this->originAdapter->send($message, ...$recipients); |
71
|
|
|
} catch (SendSmsExceptionInterface $e) { |
72
|
|
|
$this->logger->log(LogLevel::CRITICAL, 'Fail send sms.', [ |
73
|
|
|
'error' => $e->getMessage(), |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
throw $e; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
foreach ($result as $element) { |
80
|
|
|
$logMessage = sprintf( |
81
|
|
|
'%s send sms to %s.', |
82
|
|
|
$element->isSuccessfully() ? 'Success' : 'Fail', |
83
|
|
|
$element->getPhone() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$level = $element->isSuccessfully() ? LogLevel::INFO : LogLevel::ERROR; |
87
|
|
|
|
88
|
|
|
$context = [ |
89
|
|
|
'number' => $element->getPhone()->getValue(), |
90
|
|
|
'status' => $element->isSuccessfully(), |
91
|
|
|
'id' => $element->getMessageId(), |
92
|
|
|
]; |
93
|
|
|
|
94
|
|
|
if ($element->isFailed() && $element->getError()) { |
95
|
|
|
$context['error'] = [ |
96
|
|
|
'reason' => $element->getError()->getReason(), |
97
|
|
|
'message' => $element->getError()->getMessage(), |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->logger->log($level, $logMessage, $context); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|