diff --git a/README.md b/README.md index ef8cd01..cd89009 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ Add the following lines to your `composer.json` file and then run `php composer. } ``` +[Basic usage](docs/index.mkd) + ## License Apache License 2.0. See [LICENSE](LICENSE) for details. diff --git a/composer.json b/composer.json index 3007985..d068f17 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "php": ">=5.3.0" }, "require-dev": { - "phpunit/phpunit": "~4.3.0" + "phpunit/phpunit": "~6" }, "autoload": { "classmap" : ["src/Spamassassin/"] diff --git a/docs/index.mkd b/docs/index.mkd new file mode 100644 index 0000000..7e1bfb9 --- /dev/null +++ b/docs/index.mkd @@ -0,0 +1,29 @@ +# Using spamassasin PHP client + +PHP spamassasin client will connect to existing +spam detection daemon and verify if message is a unwanted +spam or if it's a legit message the end user want to +receive. + +To create instance of spamassasin PHP client, create +new instance of `Client` class - this will use default +params which should connect to local spamassasin: + +```php +use Spamassassin\Client; + +$client = new Client(); +``` + +Then validate if message is market by spam by checking +`$result` variable of type `Spamassassin\Result`: + +```php +$result = $client->process($message); +``` + +The most important properties here are: + +* `isSpam` - whether spamassasin marked message as unwanted +* `score` - received score +* `threshold` - required score to be marked as spam \ No newline at end of file diff --git a/src/Spamassassin/Client.php b/src/Spamassassin/Client.php index 57549e2..56259c8 100644 --- a/src/Spamassassin/Client.php +++ b/src/Spamassassin/Client.php @@ -24,6 +24,7 @@ class Client protected $hostname = 'localhost'; protected $port = '783'; + protected $timeout = null; protected $socketPath; protected $socket; @@ -60,6 +61,11 @@ protected function getSocket() $socket = fsockopen($this->hostname, $this->port, $errno, $errstr); } + if(!empty($this->timeout)) + { + stream_set_timeout($socket, $this->timeout); + } + if (!$socket) { throw new Exception( "Could not connect to SpamAssassin: {$errstr}", $errno @@ -215,6 +221,7 @@ protected function parseOutput($header, $message) $result->score = (float) $matches[2]; $result->thresold = (float) $matches[3]; + $result->threshold = (float) $matches[3]; } else { /** @@ -234,6 +241,7 @@ protected function parseOutput($header, $message) $result->score = (float) $matches[2]; $result->thresold = (float) $matches[3]; + $result->threshold = (float) $matches[3]; } } diff --git a/src/Spamassassin/Client/Result.php b/src/Spamassassin/Client/Result.php index 4780ce8..838f0bb 100644 --- a/src/Spamassassin/Client/Result.php +++ b/src/Spamassassin/Client/Result.php @@ -49,14 +49,20 @@ class Result /** * How many points the message must score to be considered spam - * + * * @var float */ + public $threshold; + + /** + * Typo kept for backward compat. + * @see $threshold + */ public $thresold; /** * Is it spam or not? - * + * * @var boolean */ public $isSpam; diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index c5b5407..c6b46e8 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -1,7 +1,9 @@ params = $params; $this->sa = new Client($params); diff --git a/tests/CheckTest.php b/tests/CheckTest.php index d63ff04..f3eb83f 100644 --- a/tests/CheckTest.php +++ b/tests/CheckTest.php @@ -8,7 +8,7 @@ public function testShouldBeAbleToCheckSpamMessage() $return = $this->sa->check($message); $this->assertTrue($return->isSpam); - $this->assertEquals(5.0, $return->thresold); + $this->assertEquals(5.0, $return->threshold); $this->assertTrue($return->score >= 1000); } diff --git a/tests/LearnDisabledTest.php b/tests/LearnDisabledTest.php index 378fd09..dc08c5c 100644 --- a/tests/LearnDisabledTest.php +++ b/tests/LearnDisabledTest.php @@ -36,14 +36,14 @@ public function setUp() public function testShouldThrowExceptionIfLearningIsDisabled() { $message = $this->getMessage('Spam_GTUBE.txt'); - $this->setExpectedException('Spamassassin\Client\Exception'); + $this->expectException('Spamassassin\Client\Exception'); $this->sa->learn($message, Client::LEARN_SPAM); } public function testShouldThrowExceptionWhenForgettingIfLearningIsDisabled() { $message = $this->getMessage('Spam_GTUBE.txt'); - $this->setExpectedException('Spamassassin\Client\Exception'); + $this->expectException('Spamassassin\Client\Exception'); $this->sa->learn($message, Client::LEARN_FORGET); } diff --git a/tests/MaxSizeTest.php b/tests/MaxSizeTest.php index 8e49369..7865419 100644 --- a/tests/MaxSizeTest.php +++ b/tests/MaxSizeTest.php @@ -6,7 +6,7 @@ class MaxSizeTest extends BaseTestCase { public function testShouldThrowExceptionIfMessageExceedsMaxSize() { - $this->setExpectedException('Spamassassin\Client\Exception'); + $this->expectException('Spamassassin\Client\Exception'); $params = $this->params; diff --git a/tests/SpamReportTest.php b/tests/SpamReportTest.php index 60dc26c..9e0acf6 100644 --- a/tests/SpamReportTest.php +++ b/tests/SpamReportTest.php @@ -5,7 +5,7 @@ public function testShouldReturnReportIfMessageIsSpam() { $report = $this->sa->getSpamReport($this->gtube); - $this->assertContains("Content preview:", $report->message); + $this->assertContains("1000.0", $report->message); $this->assertContains("1000 GTUBE", $report->message); $this->assertTrue($report->isSpam); }