Skip to content

Commit 12ff70d

Browse files
committed
Add functionality for increasing chunk size
1 parent 395f266 commit 12ff70d

File tree

6 files changed

+141
-4
lines changed

6 files changed

+141
-4
lines changed

src/InterfaxFile.php

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

src/InterfaxMessage.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,38 @@ public function makeFiles(): array
9393
$this->stream,
9494
[
9595
'name' => $this->filename,
96-
'mime_type' => app('filesystem')->mimeType(pathinfo($this->filename, PATHINFO_BASENAME)),
96+
'mime_type' => app('files')->mimeType($this->filename),
97+
'chunk_size' => config('services.interfax.chunk_size', 1048576),
9798
],
9899
],
99100
];
100101
}
101102

102-
return $this->files;
103+
return array_map('static::setChunkSize', $this->files);
103104
}
104105

105106
public function sleep(): void
106107
{
107108
$interval = config('services.interfax.interval', static::POLLING_INTERVAL_DEFAULT);
108109
sleep(max($interval, static::POLLING_INTERVAL_MINIMUM));
109110
}
111+
112+
protected static function setChunkSize($file)
113+
{
114+
$chunk_size = config('services.interfax.chunk_size', 1048576);
115+
116+
if (is_string($file)) {
117+
return [
118+
'location' => $file,
119+
'params' => [
120+
'chunk_size' => $chunk_size,
121+
],
122+
];
123+
} elseif (is_array($file)) {
124+
$file['params']['chunk_size'] = $chunk_size;
125+
return $file;
126+
} else {
127+
return $file;
128+
}
129+
}
110130
}

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)