InterfaxChannel   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 21
c 4
b 1
f 0
dl 0
loc 48
ccs 15
cts 18
cp 0.8333
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B send() 0 30 7
1
<?php
2
3
namespace NotificationChannels\Interfax;
4
5
use Interfax\Client;
6
use NotificationChannels\Interfax\Contracts\InterfaxNotificationContract;
7
use NotificationChannels\Interfax\Exceptions\CouldNotSendNotification;
8
9
class InterfaxChannel
10
{
11
    protected $client;
12
    protected $fax;
13
14 7
    public function __construct(Client $client)
15
    {
16 7
        $this->client = $client;
17
    }
18
19
    /**
20
     * Send the given notification.
21
     *
22
     * @param  mixed  $notifiable
23
     * @param  \NotificationChannels\Interfax\Contracts\InterfaxNotificationContract  $notification
24
     *
25
     * @throws \NotificationChannels\Interfax\Exceptions\CouldNotSendNotification
26
     */
27 7
    public function send($notifiable, InterfaxNotificationContract $notification)
28
    {
29 7
        if (! $faxNumber = $notifiable->routeNotificationFor('interfax')) {
30 1
            return;
31
        }
32
33 6
        $message = $notification->toInterfax($notifiable);
34
35
        try {
36 6
            $this->fax = $this->client->deliver([
37
                'faxNumber' => $faxNumber,
38 6
                'files' => $message->makeFiles(),
39
            ]);
40
41 6
            if ($message->shouldCheckStatus()) {
42 2
                $message->sleep();
43
44 2
                while ($this->fax->refresh()->status < 0) {
45 1
                    $message->sleep();
46
                }
47
48 2
                if ($this->fax->refresh()->status > 0) {
49 6
                    throw CouldNotSendNotification::serviceRespondedWithAnError($message, $this->fax->attributes());
50
                }
51
            }
52 1
        } catch (\Interfax\Exception\RequestException $e) {
53
            $exceptionMessage = $e->getMessage().': '.($e->getWrappedException())->getMessage();
54
            $attributes = $this->fax ? $this->fax->attributes() : ['status' => $e->getStatusCode()];
55
56
            throw CouldNotSendNotification::serviceRespondedWithAnError($message, $attributes, $exceptionMessage);
57
        }
58
    }
59
}
60