Completed
Push — master ( e15c58...b150a8 )
by Changwan
07:08
created

NullAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 57
ccs 0
cts 34
cp 0
rs 10
c 1
b 0
f 0
wmc 4
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A enqueue() 0 7 1
A dequeue() 0 15 2
1
<?php
2
namespace Wandu\Q\Adapter;
3
4
use Aws\Sqs\SqsClient;
5
use Wandu\Q\Contracts\AdapterInterface;
6
use Wandu\Q\Contracts\SerializerInterface;
7
use Wandu\Q\Job\SqsJob;
8
9
class NullAdapter implements AdapterInterface
10
{
11
    /** @var \Aws\Sqs\SqsClient */
12
    protected $client;
13
14
    /** @var string */
15
    protected $url;
16
17
    /**
18
     * @param string $key
19
     * @param string $secret
20
     * @param string $region
21
     * @param string $url
22
     */
23
    public function __construct($key, $secret, $region, $url)
24
    {
25
        $this->client = new SqsClient([
26
            'version' => 'latest',
27
            'credentials' => [
28
                'key' => $key,
29
                'secret' => $secret,
30
            ],
31
            'region' => $region,
32
        ]);
33
        $this->url = $url;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function enqueue(SerializerInterface $serializer, $payload)
40
    {
41
        $this->client->sendMessage([
42
            'QueueUrl' => $this->url,
43
            'MessageBody' => $serializer->serialize($payload),
44
        ]);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function dequeue(SerializerInterface $serializer)
51
    {
52
        $message = $this->client->receiveMessage([
53
            'QueueUrl' => $this->url,
54
        ])->get('Messages');
55
        if (count($message)) {
56
            return new SqsJob(
57
                $this->client,
58
                $this->url,
59
                $message[0]['ReceiptHandle'],
60
                $serializer->unserialize($message[0]['Body'])
61
            );
62
        }
63
        return null;
64
    }
65
}
66