SqsContextTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 173
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
getS3Client() 0 1 ?
getBucketName() 0 1 ?
getMessage() 0 1 ?
getAwsServiceConfig() 0 1 ?
createClaimCheckFactory() 0 1 ?
A getSqsClient() 0 10 2
A getSqsExtendedClientConfiguration() 0 12 2
A getQueueFixture() 0 6 1
A getQueueUrl() 0 14 2
A getQueueArn() 0 6 1
A iSendTheMessageToQueue() 0 11 1
B iCanFetchTheMessageFromQueue() 0 30 1
1
<?php
2
3
namespace AbacaphiliacFeature\AwsSdk\ClaimCheck\Bootstrap\ContextTrait;
4
5
use Abacaphiliac\AwsSdk\ClaimCheck\Exception\ExceptionInterface;
6
use Abacaphiliac\AwsSdk\ClaimCheck\ClaimCheckFactory;
7
use Abacaphiliac\AwsSdk\ClaimCheck\Sqs\SqsExtendedClientConfiguration;
8
use Abacaphiliac\AwsSdk\ClaimCheck\Sqs\SqsExtendedClient;
9
use Aws\Result;
10
use Aws\S3\S3Client;
11
use Aws\Sqs\SqsClient;
12
13
trait SqsContextTrait
14
{
15
    /** @var  SqsClient */
16
    private $sqsClient;
17
    
18
    /** @var  SqsExtendedClientConfiguration */
19
    private $sqsExtendedClientConfiguration;
20
    
21
    /** @var  string */
22
    private $queueUrl;
23
    
24
    /** @var  string[] */
25
    private $queueUrls = [];
26
27
    /**
28
     * @return S3Client
29
     */
30
    abstract public function getS3Client();
31
32
    /**
33
     * @return string
34
     */
35
    abstract public function getBucketName();
36
37
    /**
38
     * @return string
39
     */
40
    abstract public function getMessage();
41
42
    /**
43
     * @return mixed[]
44
     */
45
    abstract public function getAwsServiceConfig();
46
47
    /**
48
     * @return ClaimCheckFactory
49
     */
50
    abstract public function createClaimCheckFactory();
51
52
    /**
53
     * @return SqsClient
54
     * @throws ExceptionInterface
55
     */
56
    public function getSqsClient()
57
    {
58
        if (!$this->sqsClient) {
59
            $config = $this->getAwsServiceConfig();
60
            $client = new SqsClient($config);
61
            $this->sqsClient = new SqsExtendedClient($client, $this->getSqsExtendedClientConfiguration());
62
        }
63
64
        return $this->sqsClient;
65
    }
66
67
    /**
68
     * @return SqsExtendedClientConfiguration
69
     * @throws ExceptionInterface
70
     */
71
    public function getSqsExtendedClientConfiguration()
72
    {
73
        if (!$this->sqsExtendedClientConfiguration) {
74
            $s3Client = $this->getS3Client();
75
76
            $s3BucketName = $this->getBucketName();
77
78
            $this->sqsExtendedClientConfiguration = new SqsExtendedClientConfiguration($s3Client, $s3BucketName);
79
        }
80
        
81
        return $this->sqsExtendedClientConfiguration;
82
    }
83
84
    /**
85
     * @Given /^a queue named "([^"]*)"$/
86
     * @param string $name
87
     * @return string
88
     * @throws ExceptionInterface
89
     */
90
    public function getQueueFixture($name)
91
    {
92
        $this->queueUrl = $this->getQueueUrl($name);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getQueueUrl($name) can also be of type boolean. However, the property $queueUrl is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
93
        
94
        return $this->queueUrl;
95
    }
96
97
    /**
98
     * @param string $name
99
     * @return string|bool
100
     * @throws ExceptionInterface
101
     */
102
    public function getQueueUrl($name)
103
    {
104
        if (array_key_exists($name, $this->queueUrls)) {
105
            return $this->queueUrls[$name];
106
        }
107
        
108
        $result = $this->getSqsClient()->getQueueUrl([
109
            'QueueName' => $name,
110
        ]);
111
112
        $this->queueUrls[$name] = $result->get('QueueUrl');
113
        
114
        return $this->queueUrls[$name];
115
    }
116
117
    /**
118
     * @param string $name
119
     * @return string
120
     * @throws ExceptionInterface
121
     */
122
    public function getQueueArn($name)
123
    {
124
        $queueUrl = $this->getQueueUrl($name);
125
        
126
        return $this->getSqsClient()->getQueueArn($queueUrl);
0 ignored issues
show
Bug introduced by
It seems like $queueUrl defined by $this->getQueueUrl($name) on line 124 can also be of type boolean; however, Aws\Sqs\SqsClient::getQueueArn() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
127
    }
128
129
    /**
130
     * @When /^I send the message to a queue named "([^"]*)"$/
131
     * @param string $name
132
     * @return Result
133
     * @throws ExceptionInterface
134
     */
135
    public function iSendTheMessageToQueue($name)
136
    {
137
        $sqsClient = $this->getSqsClient();
138
        $queueUrl = $this->getQueueUrl($name);
139
        $message = $this->getMessage();
140
141
        return $sqsClient->sendMessage([
142
            'QueueUrl' => $queueUrl,
143
            'MessageBody' => $message,
144
        ]);
145
    }
146
147
    /**
148
     * @Then /^I can fetch the message from a queue named "([^"]*)"$/
149
     * @param string $name
150
     * @return Result
151
     * @throws ExceptionInterface
152
     * @throws \RuntimeException
153
     * @throws \PHPUnit_Framework_AssertionFailedError
154
     */
155
    public function iCanFetchTheMessageFromQueue($name)
156
    {
157
        $expectedMessage = $this->getMessage();
158
        \PHPUnit_Framework_Assert::assertNotEmpty($expectedMessage);
159
160
        $sqsClient = $this->getSqsClient();
161
        
162
        $queueUrl = $this->getQueueUrl($name);
163
        
164
        $result = $sqsClient->receiveMessage([
165
            'QueueUrl' => $queueUrl,
166
            'MaxNumberOfMessages' => 1,
167
        ]);
168
        
169
        $actualMessage = $result->search('Messages[].Body|[0]');
170
        \PHPUnit_Framework_Assert::assertNotEmpty($actualMessage);
171
        
172
        \PHPUnit_Framework_Assert::assertEquals(
173
            $expectedMessage,
174
            $actualMessage,
175
            'Purge the queues before running the feature suite.'
176
        );
177
178
        $sqsClient->deleteMessage([
179
            'QueueUrl' => $queueUrl,
180
            'ReceiptHandle' => $result->search('Messages[].ReceiptHandle|[0]'),
181
        ]);
182
        
183
        return $result;
184
    }
185
}
186