Skip to content

Commit 382e75f

Browse files
authored
Merge pull request #11 from laravel-notification-channels/dev
Add functionality for increasing chunk size
2 parents 3068db0 + 9bf0703 commit 382e75f

File tree

6 files changed

+146
-4
lines changed

6 files changed

+146
-4
lines changed

src/InterfaxFile.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace NotificationChannels\Interfax;
4+
5+
class InterfaxFile extends \Interfax\File
6+
{
7+
/**
8+
* File constructor.
9+
*
10+
* @param $location
11+
* @param array $params
12+
*
13+
* @throws \InvalidArgumentException
14+
*/
15+
public function __construct(\Interfax\Client $client, $location, $params = [], \Interfax\GenericFactory $factory = null)
16+
{
17+
if ($chunkSize = config('services.interfax.chunk_size')) {
18+
static::$DEFAULT_CHUNK_SIZE = $chunkSize;
19+
}
20+
21+
parent::__construct($client, $location, $params, $factory);
22+
}
23+
}

src/InterfaxMessage.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class InterfaxMessage
2020
const POLLING_INTERVAL_DEFAULT = 15;
2121
const POLLING_INTERVAL_MINIMUM = 10;
2222

23+
protected static $DEFAULT_CHUNK_SIZE = 1048576;
24+
2325
public function file(string $file)
2426
{
2527
$this->files = Arr::wrap($file);
@@ -93,18 +95,39 @@ public function makeFiles(): array
9395
$this->stream,
9496
[
9597
'name' => $this->filename,
96-
'mime_type' => app('filesystem')->mimeType(pathinfo($this->filename, PATHINFO_BASENAME)),
98+
'mime_type' => app('files')->mimeType($this->filename),
99+
'chunk_size' => config('services.interfax.chunk_size', static::$DEFAULT_CHUNK_SIZE),
97100
],
98101
],
99102
];
100103
}
101104

102-
return $this->files;
105+
return array_map('static::setChunkSize', $this->files);
103106
}
104107

105108
public function sleep(): void
106109
{
107110
$interval = config('services.interfax.interval', static::POLLING_INTERVAL_DEFAULT);
108111
sleep(max($interval, static::POLLING_INTERVAL_MINIMUM));
109112
}
113+
114+
protected static function setChunkSize($file)
115+
{
116+
$chunk_size = config('services.interfax.chunk_size', static::$DEFAULT_CHUNK_SIZE);
117+
118+
if (is_string($file)) {
119+
return [
120+
'location' => $file,
121+
'params' => [
122+
'chunk_size' => $chunk_size,
123+
],
124+
];
125+
} elseif (is_array($file)) {
126+
$file['params']['chunk_size'] = $chunk_size;
127+
128+
return $file;
129+
} else {
130+
return $file;
131+
}
132+
}
110133
}

tests/InterfaxChannelTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ public function it_can_send_notification_with_a_single_file()
4141
->once()
4242
->with([
4343
'faxNumber' => '12345678901',
44-
'files' => ['test-file.pdf'],
44+
'files' => [
45+
[
46+
'location' => 'test-file.pdf',
47+
'params' => [
48+
'chunk_size' => $this->chunkSize,
49+
],
50+
],
51+
],
4552
]);
4653

4754
$this->channel->send(new TestNotifiable, new TestNotificationWithSingleFile);
@@ -54,7 +61,20 @@ public function it_can_send_notification_with_files()
5461
->once()
5562
->with([
5663
'faxNumber' => '12345678901',
57-
'files' => ['test-file-1.pdf', 'test-file-2.pdf'],
64+
'files' => [
65+
[
66+
'location' => 'test-file-1.pdf',
67+
'params' => [
68+
'chunk_size' => $this->chunkSize,
69+
],
70+
],
71+
[
72+
'location' => 'test-file-2.pdf',
73+
'params' => [
74+
'chunk_size' => $this->chunkSize,
75+
],
76+
],
77+
],
5878
]);
5979

6080
$this->channel->send(new TestNotifiable, new TestNotificationWithFiles);

tests/InterfaxMessageTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,72 @@ public function it_should_not_check_the_status_via_refresh_default()
3737

3838
$this->assertFalse($message->shouldCheckStatus());
3939
}
40+
41+
/** @test */
42+
public function it_should_set_the_file_chunk_size_filename()
43+
{
44+
$this->increaseChunkSize();
45+
46+
$message = (new InterfaxMessage)
47+
->user(new TestNotifiable)
48+
->file(__DIR__.'/resources/test.pdf');
49+
50+
$files = $message->makeFiles();
51+
$delivery = new \Interfax\Outbound\Delivery(new \Interfax\Client, ['faxNumber'=>'0000000000', 'files'=>$files]);
52+
53+
$this->assertSame($this->chunkSize, $this->getChunkSize($delivery));
54+
}
55+
56+
/** @test */
57+
public function it_should_set_the_file_chunk_size_file_array()
58+
{
59+
$this->increaseChunkSize();
60+
61+
$message = (new InterfaxMessage)
62+
->user(new TestNotifiable)
63+
->files([['location' => __DIR__.'/resources/test.pdf']]);
64+
65+
$files = $message->makeFiles();
66+
$delivery = new \Interfax\Outbound\Delivery(new \Interfax\Client, ['faxNumber'=>'0000000000', 'files'=>$files]);
67+
68+
$this->assertSame($this->chunkSize, $this->getChunkSize($delivery));
69+
}
70+
71+
/** @test */
72+
public function it_should_set_the_file_chunk_size_file_object()
73+
{
74+
$this->increaseChunkSize();
75+
$client = new \Interfax\Client;
76+
77+
$file = new \NotificationChannels\Interfax\InterfaxFile($client, __DIR__.'/resources/test.pdf');
78+
79+
$message = (new InterfaxMessage)
80+
->user(new TestNotifiable)
81+
->files([$file]);
82+
83+
$files = $message->makeFiles();
84+
$delivery = new \Interfax\Outbound\Delivery($client, ['faxNumber'=>'0000000000', 'files'=>$files]);
85+
86+
$this->assertSame($this->chunkSize, $this->getChunkSize($delivery));
87+
}
88+
89+
protected function getChunkSize($delivery)
90+
{
91+
$deliveryReflection = new \ReflectionClass($delivery);
92+
$filesProperty = $deliveryReflection->getProperty('files');
93+
$filesProperty->setAccessible(true);
94+
95+
$files = $filesProperty->getValue($delivery);
96+
97+
$fileReflection = new \ReflectionClass($files[0]);
98+
99+
if ($files[0] instanceof \NotificationChannels\Interfax\InterfaxFile) {
100+
$fileReflection = $fileReflection->getParentClass();
101+
}
102+
103+
$chunkProperty = $fileReflection->getProperty('chunk_size');
104+
$chunkProperty->setAccessible(true);
105+
106+
return $chunkProperty->getValue($files[0]);
107+
}
40108
}

tests/TestCase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
1212
'test-file-2.pdf',
1313
];
1414

15+
protected $chunkSize = 1048576;
16+
1517
protected function getPackageProviders($app)
1618
{
1719
config([
@@ -53,6 +55,12 @@ protected function addFile(string $filename): void
5355
{
5456
$this->testFiles[] = $filename;
5557
}
58+
59+
protected function increaseChunkSize(int $size = 8000000)
60+
{
61+
$this->chunkSize = $size;
62+
config(['services.interfax.chunk_size' => $this->chunkSize]);
63+
}
5664
}
5765

5866
class TestNotifiable

tests/resources/test.pdf

7.94 KB
Binary file not shown.

0 commit comments

Comments
 (0)