Logger   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSms() 0 4 1
A logMessage() 0 20 2
A logError() 0 19 2
1
<?php
2
3
namespace DoS\SMSBundle\SMS;
4
5
use Psr\Log\LoggerInterface;
6
use SmsSender\Exception\WrappedException;
7
use SmsSender\Result\ResultInterface;
8
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)
22
    {
23
        $this->logger = $logger;
24
    }
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()
82
    {
83
        return $this->smsList;
84
    }
85
}
86