Skip to content

Commit 61d00f2

Browse files
authored
Merge pull request #6 from SoftCreatR/overhaul
Code overhaul
2 parents 4c7448f + 232bc80 commit 61d00f2

File tree

5 files changed

+89
-64
lines changed

5 files changed

+89
-64
lines changed

composer.json

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
{
2-
"name": "appwrite/php-clamav",
3-
"description": "ClamAV network and pipe client for PHP",
4-
"type": "library",
5-
"keywords": ["php", "clamav", "anti virus", "appwrite"],
6-
"license": "MIT",
7-
"authors": [
8-
{
9-
"name": "Eldad Fux",
10-
"email": "eldad@appwrite.io"
11-
}
12-
],
13-
"autoload": {
14-
"psr-4": {"Appwrite\\ClamAV\\": "src/ClamAV"}
15-
},
16-
"autoload-dev": {
17-
"psr-4": {"Utopia\\Tests\\": "tests/ClamAV"}
18-
},
19-
"require": {
20-
"php": ">=7.1"
21-
},
22-
"require-dev": {
23-
"phpunit/phpunit": "^7.0"
24-
},
25-
"minimum-stability": "stable"
2+
"name": "appwrite/php-clamav",
3+
"description": "ClamAV network and pipe client for PHP",
4+
"type": "library",
5+
"keywords": [
6+
"php",
7+
"clamav",
8+
"anti virus",
9+
"appwrite"
10+
],
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Eldad Fux",
15+
"email": "eldad@appwrite.io"
16+
}
17+
],
18+
"autoload": {
19+
"psr-4": {
20+
"Appwrite\\ClamAV\\": "src/ClamAV"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Appwrite\\ClamAV\\Tests\\": "tests/ClamAV"
26+
}
27+
},
28+
"require": {
29+
"php": ">=7.1",
30+
"ext-sockets": "*"
31+
},
32+
"require-dev": {
33+
"phpunit/phpunit": "^7.0"
34+
},
35+
"minimum-stability": "stable"
2636
}

src/ClamAV/ClamAV.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
namespace Appwrite\ClamAV;
44

5+
use function end;
6+
use function explode;
7+
use function socket_close;
8+
use function socket_recv;
9+
use function socket_send;
10+
use function strlen;
11+
use function trim;
12+
513
abstract class ClamAV
614
{
715
/**
816
* @var int
917
*/
10-
const CLAMAV_MAX = 20000;
18+
public const CLAMAV_MAX = 20000;
1119

1220
/**
1321
* @return resource
@@ -24,9 +32,9 @@ private function sendCommand($command)
2432

2533
$socket = $this->getSocket();
2634

27-
\socket_send($socket, $command, \strlen($command), 0);
28-
\socket_recv($socket, $return, self::CLAMAV_MAX, 0);
29-
\socket_close($socket);
35+
socket_send($socket, $command, strlen($command), 0);
36+
socket_recv($socket, $return, self::CLAMAV_MAX, 0);
37+
socket_close($socket);
3038

3139
return $return;
3240
}
@@ -38,20 +46,20 @@ private function sendCommand($command)
3846
*
3947
* @return bool
4048
*/
41-
public function ping()
49+
public function ping(): bool
4250
{
4351
$return = $this->sendCommand('PING');
44-
return \trim($return) === 'PONG';
52+
return trim($return) === 'PONG';
4553
}
4654

4755
/**
4856
* Check ClamAV Version
4957
*
5058
* @return string
5159
*/
52-
public function version()
60+
public function version(): string
5361
{
54-
return \trim($this->sendCommand('VERSION'));
62+
return trim($this->sendCommand('VERSION'));
5563
}
5664

5765
/**
@@ -81,14 +89,14 @@ public function shutdown()
8189
* @param string $file
8290
* @return bool return true if file is OK or false if not
8391
*/
84-
public function fileScan(string $file)
92+
public function fileScan(string $file): bool
8593
{
86-
$out = $this->sendCommand('SCAN ' . $file);
94+
$out = $this->sendCommand('SCAN ' . $file);
8795

88-
$out = \explode(':', $out);
89-
$stats = \end($out);
96+
$out = explode(':', $out);
97+
$stats = end($out);
9098

91-
$result = \trim($stats);
99+
$result = trim($stats);
92100

93101
return ($result === 'OK');
94102
}
@@ -100,13 +108,13 @@ public function fileScan(string $file)
100108
* @param string $file
101109
* @return array
102110
*/
103-
public function continueScan(string $file)
111+
public function continueScan(string $file): array
104112
{
105113
$return = [];
106114

107-
foreach(\explode("\n", \trim($this->sendCommand('CONTSCAN ' . $file))) as $results ) {
108-
list($file, $stats) = \explode(':', $results);
109-
\array_push($return, [ 'file' => $file, 'stats' => \trim($stats) ]);
115+
foreach (explode("\n", trim($this->sendCommand('CONTSCAN ' . $file))) as $results) {
116+
[$file, $stats] = explode(':', $results);
117+
$return[] = ['file' => $file, 'stats' => trim($stats)];
110118
}
111119

112120
return $return;

src/ClamAV/Network.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Appwrite\ClamAV;
44

5+
use RuntimeException;
6+
use function socket_connect;
7+
use function socket_create;
8+
59
class Network extends ClamAV
610
{
7-
const CLAMAV_HOST = '127.0.0.1';
8-
const CLAMAV_PORT = 3310;
11+
private const CLAMAV_HOST = '127.0.0.1';
12+
private const CLAMAV_PORT = 3310;
913

1014
/**
1115
* @var string
@@ -33,15 +37,15 @@ public function __construct(string $host = self::CLAMAV_HOST, int $port = self::
3337

3438
/**
3539
* @return resource
36-
* @throws \Exception
40+
* @throws RuntimeException
3741
*/
3842
protected function getSocket()
3943
{
40-
$socket = @\socket_create(AF_INET, SOCK_STREAM, 0);
41-
$status = @\socket_connect($socket, $this->host, $this->port);
44+
$socket = @socket_create(AF_INET, SOCK_STREAM, 0);
45+
$status = @socket_connect($socket, $this->host, $this->port);
4246

43-
if(!$status) {
44-
throw new \Exception('Unable to connect to ClamAV server');
47+
if (!$status) {
48+
throw new RuntimeException('Unable to connect to ClamAV server');
4549
}
4650
return $socket;
4751
}

src/ClamAV/Pipe.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Appwrite\ClamAV;
44

5+
use function socket_connect;
6+
use function socket_create;
7+
58
class Pipe extends ClamAV
69
{
7-
const CLAMAV_HOST = '/var/run/clamav/clamd.ctl';
10+
private const CLAMAV_HOST = '/var/run/clamav/clamd.ctl';
811

912
/**
1013
* @var string
@@ -29,8 +32,8 @@ public function __construct(string $pip = self::CLAMAV_HOST)
2932
*/
3033
protected function getSocket()
3134
{
32-
$socket = \socket_create(AF_UNIX, SOCK_STREAM, 0);
33-
\socket_connect($socket, $this->pip);
35+
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
36+
socket_connect($socket, $this->pip);
3437
return $socket;
3538
}
3639
}

tests/ClamAV/ClamAVTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @license The MIT License (MIT) <http://www.opensource.org/licenses/mit-license.php>
1212
*/
1313

14-
namespace Utopia\Tests;
14+
namespace Appwrite\ClamAV\Tests;
1515

1616
use Appwrite\ClamAV\Network;
1717
use Appwrite\ClamAV\Pipe;
@@ -22,12 +22,12 @@ class ClamAVTest extends TestCase
2222
/**
2323
* @var Network
2424
*/
25-
protected $network = null;
25+
protected $network;
2626

2727
/**
2828
* @var Pipe
2929
*/
30-
protected $pipe = null;
30+
protected $pipe;
3131

3232
protected function setUp(): void
3333
{
@@ -38,27 +38,27 @@ protected function setUp(): void
3838
protected function tearDown(): void
3939
{
4040
$this->network = null;
41-
$this->pipe= null;
41+
$this->pipe = null;
4242
}
4343

44-
public function testVersion()
44+
public function testVersion(): void
4545
{
46-
$this->assertStringStartsWith('ClamAV ', $this->network->version());
46+
self::assertStringStartsWith('ClamAV ', $this->network->version());
4747
}
4848

49-
public function testPing()
49+
public function testPing(): void
5050
{
51-
$this->assertTrue($this->network->ping());
51+
self::assertTrue($this->network->ping());
5252
}
5353

54-
public function testFileScan()
54+
public function testFileScan(): void
5555
{
56-
$this->assertTrue($this->network->fileScan('/home/NoVirus.txt'));
57-
$this->assertFalse($this->network->fileScan('/home/Virus.txt'));
56+
self::assertTrue($this->network->fileScan('/home/NoVirus.txt'));
57+
self::assertFalse($this->network->fileScan('/home/Virus.txt'));
5858
}
5959

60-
public function testReload()
60+
public function testReload(): void
6161
{
62-
$this->assertStringStartsWith('RELOADING', $this->network->reload());
62+
self::assertStringStartsWith('RELOADING', $this->network->reload());
6363
}
6464
}

0 commit comments

Comments
 (0)