Felicio::sendMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Felicio;
6
7
use Aws\Sqs\SqsClient;
8
use Felicio\Contracts\FelicioContract;
9
use Aws\Sdk;
10
use Aws\Exception\AwsException;
11
use Aws\Credentials\Credentials;
12
use Aws\Result;
13
use Symfony\Component\Dotenv\Dotenv;
14
15
final class Felicio implements FelicioContract
16
{
17
    protected SqsClient $felicioClient;
18
19
    public function __construct($felicioDotFile)
20
    {
21
        $dotenv = new Dotenv();
22
        $dotenv->load($felicioDotFile);
23
24
        $credentials = new Credentials(
25
            $_ENV['AWS_SQS_ACCESS_KEY'],
26
            $_ENV['AWS_SQS_SECRET_KEY']
27
        );
28
29
        $configs = [
30
            'credentials' => $credentials,
31
            'region' => $_ENV['AWS_SQS_REGION'],
32
            'version' => $_ENV['AWS_SQS_API_VERSION'],
33
        ];
34
35
        $sdk = new Sdk($configs);
36
37
        $this->felicioClient = $sdk->createSqs();
38
    }
39
40
    public function sendMessage(array $params): string
41
    {
42
        try {
43
            return (string)$this->felicioClient->sendMessage($params)->get('MessageId');
44
        } catch (AwsException $e) {
45
            throw new AwsException();
0 ignored issues
show
Bug introduced by
The call to Aws\Exception\AwsException::__construct() has too few arguments starting with message. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            throw /** @scrutinizer ignore-call */ new AwsException();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
46
        }
47
    }
48
49
    public function receiveMessage(array $params): ?array
50
    {
51
        try {
52
            return $this->felicioClient->receiveMessage($params)->get('Messages');
53
        } catch (AwsException $e) {
54
            throw new AwsException();
0 ignored issues
show
Bug introduced by
The call to Aws\Exception\AwsException::__construct() has too few arguments starting with message. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            throw /** @scrutinizer ignore-call */ new AwsException();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
55
        }
56
    }
57
58
    public function deleteMessage(array $params): Result
59
    {
60
        try {
61
            return $this->felicioClient->deleteMessage($params);
62
        } catch (AwsException $e) {
63
            throw new AwsException();
0 ignored issues
show
Bug introduced by
The call to Aws\Exception\AwsException::__construct() has too few arguments starting with message. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
            throw /** @scrutinizer ignore-call */ new AwsException();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
64
        }
65
    }
66
67
    public function countMessages($queue): int
68
    {
69
        $response = $this->felicioClient->getQueueAttributes(
70
            [
71
                'QueueUrl' => $queue,
72
                'AttributeNames' => ['ApproximateNumberOfMessages'],
73
            ]
74
        );
75
76
        $attributes = $response->get('Attributes');
77
78
        return (int)$attributes['ApproximateNumberOfMessages'];
79
    }
80
}