| 1 | <?php |
||
| 9 | class Logger |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var LoggerInterface |
||
| 13 | */ |
||
| 14 | protected $logger; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $smsList = array(); |
||
| 20 | |||
| 21 | public function __construct(LoggerInterface $logger = null) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Log a SMS. |
||
| 28 | * |
||
| 29 | * @param Resultinterface $sms The SMS to log. |
||
| 30 | * @param float $duration The time required to send the SMS (in seconds). |
||
| 31 | * @param string $provider_class The class name of the provider which sent the SMS. |
||
| 32 | */ |
||
| 33 | public function logMessage(ResultInterface $sms, $duration, $provider_class) |
||
| 34 | { |
||
| 35 | $this->smsList[] = array( |
||
| 36 | 'sms' => $sms, |
||
| 37 | 'duration' => $duration, |
||
| 38 | 'provider_class' => $provider_class, |
||
| 39 | ); |
||
| 40 | |||
| 41 | if ($this->logger !== null) { |
||
| 42 | $message = sprintf( |
||
| 43 | 'SMS sent to %s, from %s %0.2f ms (%s), status: %s', |
||
| 44 | $sms->getRecipient(), |
||
| 45 | $sms->getOriginator(), |
||
| 46 | $duration * 1000, |
||
| 47 | $provider_class, |
||
| 48 | $sms->getStatus() |
||
| 49 | ); |
||
| 50 | $this->logger->info($message); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param WrappedException $error |
||
| 56 | * @param $provider_class |
||
| 57 | */ |
||
| 58 | public function logError(WrappedException $error, $provider_class) |
||
| 59 | { |
||
| 60 | if ($this->logger === null) { |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | $realError = $error->getWrappedException(); |
||
| 65 | $sms = $error->getSms(); |
||
| 66 | $message = sprintf( |
||
| 67 | 'Failed to sent SMS to %s, from %s. Error message: "%s", code %d (%s)', |
||
| 68 | $sms['recipient'], |
||
| 69 | $sms['originator'], |
||
| 70 | $realError->getMessage(), |
||
| 71 | $realError->getCode(), |
||
| 72 | $provider_class |
||
| 73 | ); |
||
| 74 | |||
| 75 | $this->logger->error($message); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function getSms() |
||
| 85 | } |
||
| 86 |