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

InterfaxMessage::sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace NotificationChannels\Interfax;
4
5
use Illuminate\Support\Arr;
6
7
class InterfaxMessage
8
{
9
    protected array $files;
10
    protected $stream;
11
    protected string $filename;
12
    protected string $method;
13
    protected $statusCheck = false;
14
    public $user;
15
    public array $metadata = [];
16
17
    const FILES = 'files';
18
    const STREAM = 'stream';
19
20
    const POLLING_INTERVAL_DEFAULT = 15;
21
    const POLLING_INTERVAL_MINIMUM = 10;
22
23
    protected static $DEFAULT_CHUNK_SIZE = 1048576;
24
25 10
    public function file(string $file)
26
    {
27 10
        $this->files = Arr::wrap($file);
28 10
        $this->method = static::FILES;
29
30 10
        return $this;
31
    }
32
33 5
    public function files(array $files): InterfaxMessage
34
    {
35 5
        $this->files = $files;
36 5
        $this->method = static::FILES;
37
38 5
        return $this;
39
    }
40
41 2
    public function stream($stream, string $filename): InterfaxMessage
42
    {
43 2
        $this->stream = $stream;
44 2
        $this->filename = $filename;
45 2
        $this->method = static::STREAM;
46
47 2
        return $this;
48
    }
49
50 9
    public function checkStatus(bool $shouldCheck = true)
51
    {
52 9
        $this->statusCheck = $shouldCheck;
53
54 9
        return $this;
55
    }
56
57 9
    public function shouldCheckStatus(): bool
58
    {
59 9
        return $this->statusCheck;
60
    }
61
62
    /**
63
     * Set a user who can be notified in case the fax fails to send.
64
     *
65
     * @param  mixed  $notifiable  The user to notify
66
     * @return InterfaxMessage
67
     */
68 17
    public function user($notifiable): InterfaxMessage
69
    {
70 17
        $this->user = $notifiable;
71
72 17
        return $this;
73
    }
74
75
    /**
76
     * Add metadata to the message for logging purposes.
77
     *
78
     * @param  array  $data  The data to add to the metadata array
79
     * @return InterfaxMessage
80
     */
81 5
    public function addMetadata(array $data): InterfaxMessage
82
    {
83 5
        foreach ($data as $key => $value) {
84 5
            $this->metadata[$key] = $value;
85
        }
86
87 5
        return $this;
88
    }
89
90 9
    public function makeFiles(): array
91
    {
92 9
        if ($this->method === static::STREAM) {
93
            return [
94
                [
95 2
                    $this->stream,
96
                    [
97 2
                        'name' => $this->filename,
98 2
                        'mime_type' => app('files')->mimeType($this->filename),
99 2
                        'chunk_size' => config('services.interfax.chunk_size', static::$DEFAULT_CHUNK_SIZE),
100
                    ],
101
                ],
102
            ];
103
        }
104
105 7
        return array_map('static::setChunkSize', $this->files);
106
    }
107
108 2
    public function sleep(): void
109
    {
110 2
        $interval = config('services.interfax.interval', static::POLLING_INTERVAL_DEFAULT);
111 2
        sleep(max($interval, static::POLLING_INTERVAL_MINIMUM));
112
    }
113
114 7
    protected static function setChunkSize($file)
115
    {
116 7
        $chunk_size = config('services.interfax.chunk_size', static::$DEFAULT_CHUNK_SIZE);
117
118 7
        if (is_string($file)) {
119
            return [
120 5
                'location' => $file,
121
                'params' => [
122 5
                    'chunk_size' => $chunk_size,
123
                ],
124
            ];
125 2
        } elseif (is_array($file)) {
126 1
            $file['params']['chunk_size'] = $chunk_size;
127
128 1
            return $file;
129
        } else {
130 1
            return $file;
131
        }
132
    }
133
}
134