Skip to content

Commit 9ac9934

Browse files
committed
Add constants, convert if to match and some tests
1 parent 278ff6f commit 9ac9934

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

src/Symfony/Component/Messenger/Bridge/Amqp/Compressor/CompressorFactory.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ class CompressorFactory
1717
{
1818
public static function createCompressor(string $mimeContentEncoding)
1919
{
20-
if ('gzip' === $mimeContentEncoding) {
21-
return new Gzip();
22-
} elseif ('deflate') {
23-
return new Deflate();
24-
}
25-
26-
throw new InvalidArgumentException(sprintf('The MIME content encoding of the message cannot be decompressed "%s".', $mimeContentEncoding));
20+
return match ($mimeContentEncoding) {
21+
Gzip::CONTENT_ENCODING => new Gzip(),
22+
Deflate::CONTENT_ENCODING => new Deflate(),
23+
default => throw new InvalidArgumentException(sprintf('The MIME content encoding of the message cannot be decompressed "%s".', $mimeContentEncoding)),
24+
};
2725
}
2826
}

src/Symfony/Component/Messenger/Bridge/Amqp/Compressor/Deflate.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313

1414
class Deflate implements CompressorInterface
1515
{
16+
public const CONTENT_ENCODING = 'deflate';
17+
1618
public function compress(mixed $data): string
1719
{
1820
return gzdeflate($data);
1921
}
2022

2123
public function decompress(mixed $data): mixed
2224
{
23-
$decompressData = gzinflate($data);
24-
if (false === $decompressData) {
25-
return $data;
25+
if (\function_exists('gzinflate')) {
26+
return @gzinflate($data) ?: $data;
2627
}
2728

28-
return $decompressData;
29+
return $data;
2930
}
3031
}

src/Symfony/Component/Messenger/Bridge/Amqp/Compressor/Gzip.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313

1414
class Gzip implements CompressorInterface
1515
{
16+
public const CONTENT_ENCODING = 'gzip';
17+
1618
public function compress(mixed $data): string
1719
{
1820
return gzencode($data);
1921
}
2022

2123
public function decompress(mixed $data): mixed
2224
{
23-
$decompressData = gzdecode($data);
24-
if (false === $decompressData) {
25-
return $data;
25+
if (\function_exists('gzdecode')) {
26+
return @gzdecode($data) ?: $data;
2627
}
2728

28-
return $decompressData;
29+
return $data;
2930
}
3031
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Compressor;
4+
5+
use Symfony\Component\Messenger\Bridge\Amqp\Compressor\Deflate;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class DeflateTest extends TestCase
9+
{
10+
public function testDecompress()
11+
{
12+
$compressor = new Deflate();
13+
14+
$actual = $compressor->decompress('string no compressed');
15+
$this->assertEquals('string no compressed', $actual);
16+
17+
$actual = $compressor->decompress(gzdeflate('string compressed'));
18+
$this->assertEquals('string compressed', $actual);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Compressor;
4+
5+
use Symfony\Component\Messenger\Bridge\Amqp\Compressor\Gzip;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class GzipTest extends TestCase
9+
{
10+
public function testDecompress()
11+
{
12+
$compressor = new Gzip();
13+
14+
$actual = $compressor->decompress('string no compressed');
15+
$this->assertEquals('string no compressed', $actual);
16+
17+
$actual = $compressor->decompress(gzencode('string compressed'));
18+
$this->assertEquals('string compressed', $actual);
19+
}
20+
}

0 commit comments

Comments
 (0)