Passed
Pull Request — master (#14)
by Craig
32:44
created

InterfaxChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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