SnsContextTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 6
dl 0
loc 158
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
getS3Client() 0 1 ?
getBucketName() 0 1 ?
getAwsServiceConfig() 0 1 ?
createClaimCheckFactory() 0 1 ?
getQueueUrl() 0 1 ?
getMessage() 0 1 ?
getSqsClient() 0 1 ?
getSqsExtendedClientConfiguration() 0 1 ?
A getSnsClient() 0 10 2
A createSnsExtendedClientConfiguration() 0 8 1
A getTopicFixture() 0 6 1
A createTopicFixture() 0 8 1
A aQueueNamedIsSubscribedToATopicNamed() 0 15 1
A iSendTheMessageToATopicNamed() 0 17 1
1
<?php
2
3
namespace AbacaphiliacFeature\AwsSdk\ClaimCheck\Bootstrap\ContextTrait;
4
5
use Abacaphiliac\AwsSdk\ClaimCheck\ClaimCheckFactory;
6
use Abacaphiliac\AwsSdk\ClaimCheck\Exception\ExceptionInterface;
7
use Abacaphiliac\AwsSdk\ClaimCheck\Sns\SnsExtendedClient;
8
use Abacaphiliac\AwsSdk\ClaimCheck\Sns\SnsExtendedClientConfiguration;
9
use Abacaphiliac\AwsSdk\ClaimCheck\Sqs\SqsExtendedClientConfiguration;
10
use Aws\Result;
11
use Aws\S3\S3Client;
12
use Aws\Sns\SnsClient;
13
use Aws\Sqs\SqsClient;
14
15
trait SnsContextTrait
16
{
17
    /** @var  SnsClient */
18
    private $snsClient;
19
    
20
    /** @var  string */
21
    private $topicArn;
22
23
    /**
24
     * @return S3Client
25
     */
26
    abstract public function getS3Client();
27
28
    /**
29
     * @return string
30
     */
31
    abstract public function getBucketName();
32
33
    /**
34
     * @return mixed[]
35
     */
36
    abstract public function getAwsServiceConfig();
37
38
    /**
39
     * @return ClaimCheckFactory
40
     */
41
    abstract public function createClaimCheckFactory();
42
43
    /**
44
     * @param string $name
45
     * @return string
46
     */
47
    abstract public function getQueueUrl($name);
48
49
    /**
50
     * @return string
51
     */
52
    abstract public function getMessage();
53
54
    /**
55
     * @return SqsClient
56
     */
57
    abstract public function getSqsClient();
58
59
    /**
60
     * @return SqsExtendedClientConfiguration
61
     * @throws ExceptionInterface
62
     */
63
    abstract public function getSqsExtendedClientConfiguration();
64
65
    /**
66
     * @return SnsClient
67
     * @throws \InvalidArgumentException
68
     * @throws ExceptionInterface
69
     */
70
    public function getSnsClient()
71
    {
72
        if (!$this->snsClient) {
73
            $config = $this->getAwsServiceConfig();
74
            $client = new SnsClient($config);
75
            $this->snsClient = new SnsExtendedClient($client, $this->createSnsExtendedClientConfiguration());
76
        }
77
78
        return $this->snsClient;
79
    }
80
81
    /**
82
     * @return SnsExtendedClientConfiguration
83
     * @throws ExceptionInterface
84
     */
85
    private function createSnsExtendedClientConfiguration()
86
    {
87
        $s3Client = $this->getS3Client();
88
89
        $s3BucketName = $this->getBucketName();
90
91
        return new SnsExtendedClientConfiguration($s3Client, $s3BucketName);
92
    }
93
94
    /**
95
     * @Given /^a topic named "([^"]*)"$/
96
     * @param string $name
97
     * @return string
98
     * @throws ExceptionInterface
99
     * @throws \InvalidArgumentException
100
     */
101
    public function getTopicFixture($name)
102
    {
103
        $this->topicArn = $this->createTopicFixture($name);
104
        
105
        return $this->topicArn;
106
    }
107
108
    /**
109
     * @param string $name
110
     * @return string
111
     * @throws ExceptionInterface
112
     * @throws \InvalidArgumentException
113
     */
114
    public function createTopicFixture($name)
115
    {
116
        $result = $this->getSnsClient()->createTopic([
117
            'Name' => $name,
118
        ]);
119
120
        return $result->get('TopicArn');
121
    }
122
123
    /**
124
     * @Given /^a queue named "([^"]*)" is subscribed to a topic named "([^"]*)"$/
125
     * @param string $queueName
126
     * @param string $topicName
127
     * @throws ExceptionInterface
128
     * @throws \InvalidArgumentException
129
     * @throws \Zend\Json\Exception\RuntimeException
130
     */
131
    public function aQueueNamedIsSubscribedToATopicNamed($queueName, $topicName)
132
    {
133
        $topicArn = $this->createTopicFixture($topicName);
134
135
        $queueUrl = $this->getQueueUrl($queueName);
136
        
137
        $queueArn = $this->getSqsClient()->getQueueArn($queueUrl);
138
        
139
        // Subscribe SQS Queue to SNS Topic.
140
        $this->getSnsClient()->subscribe([
141
            'TopicArn' => $topicArn,
142
            'Protocol' => 'sqs',
143
            'Endpoint' => $queueArn,
144
        ]);
145
    }
146
147
    /**
148
     * @When /^I send the message to a topic named "([^"]*)"$/
149
     * @param string $name
150
     * @return Result
151
     * @throws ExceptionInterface
152
     * @throws \InvalidArgumentException
153
     */
154
    public function iSendTheMessageToATopicNamed($name)
155
    {
156
        $sqsExtendedClientConfiguration = $this->getSqsExtendedClientConfiguration();
157
        
158
        // Leaky abstraction???
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
        // We probably shouldn't be able to change the behavior of the extended-client after it is created.
160
        $sqsExtendedClientConfiguration->setDeleteFromS3(false);
161
        
162
        $topicArn = $this->createTopicFixture($name);
163
164
        $result = $this->getSnsClient()->publish([
165
            'TopicArn' => $topicArn,
166
            'Message' => $this->getMessage(),
167
        ]);
168
        
169
        return $result;
170
    }
171
172
}
173