Skip to content

Commit

Permalink
Merge pull request #6 from geekcom/develop
Browse files Browse the repository at this point in the history
countMessages method added, new tests, update docs
  • Loading branch information
geekcom authored Nov 14, 2019
2 parents 71966fd + 0162329 commit 5feb0c5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ $params = [
$felicio->deleteMessage($params);
```

## Count messages
```php
require __DIR__ . '/vendor/autoload.php';

use Felicio\Felicio;

$felicioDotFile = __DIR__ . '/.felicio';

$felicio = new Felicio($felicioDotFile);

$queueUrl = 'https://sqs.us-west-2.amazonaws.com/999999999/my_queue';

$messages = $felicio->countMessages($queueUrl);

var_dump($messages);
```

## Contributing

Feel free to contribute, make a fork!
Expand Down
37 changes: 21 additions & 16 deletions src/Felicio.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use Felicio\Contracts\FelicioContract;
use Aws\Sdk;
use Aws\Credentials\Credentials;
use Aws\Exception\AwsException;
use Aws\Credentials\Credentials;
use Symfony\Component\Dotenv\Dotenv;

final class Felicio implements FelicioContract
Expand Down Expand Up @@ -35,39 +35,44 @@ public function __construct($felicioDotFile)
$this->felicioClient = $sdk->createSqs();
}

public function sendMessage(array $params): bool
public function sendMessage(array $params)
{
try {
$this->felicioClient
->sendMessage($params);

return true;
return $this->felicioClient->sendMessage($params)->get('MessageId');
} catch (AwsException $e) {
throw new AwsException();
}
}

public function receiveMessage(array $params): array
public function receiveMessage(array $params)
{
try {
return $this->felicioClient
->receiveMessage($params)
->get('Messages');

return $this->felicioClient->receiveMessage($params)->get('Messages');
} catch (AwsException $e) {
throw new AwsException();
}
}

public function deleteMessage(array $params): bool
public function deleteMessage(array $params)
{
try {
$this->felicioClient
->deleteMessage($params);

return true;
return $this->felicioClient->deleteMessage($params);
} catch (AwsException $e) {
throw new AwsException();
}
}

public function countMessages($queue)
{
$response = $this->felicioClient->getQueueAttributes(
[
'QueueUrl' => $queue,
'AttributeNames' => ['ApproximateNumberOfMessages'],
]
);

$attributes = $response->get('Attributes');

return (int)$attributes['ApproximateNumberOfMessages'];
}
}
11 changes: 9 additions & 2 deletions tests/FelicioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testSendMessage()
'MessageBody' => 'Message 001'
];

$this->assertTrue($this->instance->sendMessage($params));
$this->assertIsString($this->instance->sendMessage($params));
}

public function testReceiveMessage()
Expand All @@ -66,7 +66,7 @@ public function testDeleteMessage()
'ReceiptHandle' => '', //required
];

$this->assertTrue($this->instance->deleteMessage($params));
$this->assertEmpty($this->instance->deleteMessage($params));
}

public function testDeleteMessageWithoutParameters()
Expand All @@ -80,4 +80,11 @@ public function testDeleteMessageWithoutParameters()

$this->instance->deleteMessage($params);
}

public function testIfExistsMessage()
{
$queueUrl = ''; //required

$this->assertGreaterThan(1, $this->instance->countMessages($queueUrl));
}
}

0 comments on commit 5feb0c5

Please sign in to comment.