1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Abacaphiliac\AwsSdk\ClaimCheck\Sns; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\AwsSdk\ClaimCheck\ClaimCheck; |
6
|
|
|
use Abacaphiliac\AwsSdk\ClaimCheck\Exception\ExceptionInterface; |
7
|
|
|
use Aws\Result; |
8
|
|
|
use Aws\Sns\SnsClient; |
9
|
|
|
use GuzzleHttp\Promise\Promise; |
10
|
|
|
|
11
|
|
|
class SnsExtendedClient extends SnsClient |
12
|
|
|
{ |
13
|
|
|
/** @var SnsClient */ |
14
|
|
|
private $snsClient; |
15
|
|
|
|
16
|
|
|
/** @var SnsExtendedClientConfiguration */ |
17
|
|
|
private $configuration; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* SnsExtendedClient constructor. |
21
|
|
|
* @param SnsClient $snsClient |
22
|
|
|
* @param SnsExtendedClientConfiguration $configuration |
23
|
|
|
*/ |
24
|
1 |
|
public function __construct(SnsClient $snsClient, SnsExtendedClientConfiguration $configuration) |
25
|
|
|
{ |
26
|
1 |
|
$this->snsClient = $snsClient; |
27
|
1 |
|
$this->configuration = $configuration; |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $name |
32
|
|
|
* @param mixed[] $args |
33
|
|
|
* @return Result|Promise |
34
|
|
|
* @throws ExceptionInterface |
35
|
|
|
*/ |
36
|
1 |
|
public function __call($name, array $args) |
37
|
|
|
{ |
38
|
1 |
|
$params = array_key_exists(0, $args) ? $args[0] : []; |
39
|
|
|
|
40
|
1 |
|
if (strcasecmp($name, 'publish') === 0) { |
41
|
1 |
|
return $this->publishClaimCheck($params); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->snsClient->{$name}($params); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed[] $args |
49
|
|
|
* @return Result |
50
|
|
|
* @throws ExceptionInterface |
51
|
|
|
*/ |
52
|
1 |
View Code Duplication |
private function publishClaimCheck(array $args = []) |
|
|
|
|
53
|
|
|
{ |
54
|
1 |
|
$claimCheckSerializer = $this->configuration->getClaimCheckSerializer(); |
55
|
|
|
|
56
|
1 |
|
$message = array_key_exists('Message', $args) ? $args['Message'] : ''; |
57
|
|
|
|
58
|
1 |
|
$claimCheck = $this->storeMessageInS3($message); |
59
|
|
|
|
60
|
1 |
|
$args['Message'] = $claimCheckSerializer->serialize($claimCheck); |
61
|
|
|
|
62
|
1 |
|
return $this->snsClient->publish($args); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $message |
67
|
|
|
* @return ClaimCheck |
68
|
|
|
*/ |
69
|
1 |
View Code Duplication |
private function storeMessageInS3($message) |
|
|
|
|
70
|
|
|
{ |
71
|
1 |
|
$s3Client = $this->configuration->getS3Client(); |
72
|
1 |
|
$s3BucketName = $this->configuration->getS3BucketName(); |
73
|
1 |
|
$claimCheckFactory = $this->configuration->getClaimCheckFactory(); |
74
|
|
|
|
75
|
1 |
|
$claimCheck = $claimCheckFactory->create(); |
76
|
|
|
|
77
|
1 |
|
$s3Client->putObject([ |
78
|
1 |
|
'Bucket' => $s3BucketName, |
79
|
1 |
|
'Key' => $claimCheck->getS3Key(), |
80
|
1 |
|
'Body' => $message, |
81
|
1 |
|
]); |
82
|
|
|
|
83
|
1 |
|
return $claimCheck; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.