Completed
Push — master ( ef74c4...7e6611 )
by Alessandro
03:26
created

Publisher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 105
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A publish() 0 18 2
A prepareBody() 0 16 3
B prepareSignature() 0 28 3
1
<?php
2
3
namespace Facile\CrossbarHTTPPublisherBundle\Publisher;
4
5
use Facile\CrossbarHTTPPublisherBundle\Exception\PublishRequestException;
6
use GuzzleHttp\Client;
7
8
/**
9
 * Class Publisher
10
 * @package Facile\CrossbarHTTPPublisherBundle\Publisher
11
 */
12
class Publisher
13
{
14
    /** @var Client */
15
    private $client;
16
17
    /** @var string */
18
    private $key;
19
20
    /** @var string */
21
    private $secret;
22
23
    /**
24
     * @param Client $client
25
     * @param $key
26
     * @param $secret
27
     */
28 4
    public function __construct(Client $client, $key, $secret)
29
    {
30 4
        $this->client = $client;
31 4
        $this->key = $key;
32 4
        $this->secret = $secret;
33 4
    }
34
35
    /**
36
     * @param $topic string
37
     * @param $args array|null
38
     * @param $kwargs array|null
39
     * @return array JSON decoded response
40
     * @throws \Exception
41
     */
42 3
    public function publish($topic, array $args = null, array $kwargs = null)
43
    {
44 3
        $jsonBody = $this->prepareBody($topic, $args, $kwargs);
45
46
        try {
47 3
            $response = $this->client->post(
48 3
                '',
49
                [
50 3
                    'json' => $jsonBody,
51 3
                    'query' => $this->prepareSignature($jsonBody)
52
                ]
53
            );
54 1
        } catch (\Exception $e) {
55 1
            throw new PublishRequestException($e->getMessage(), 500, $e);
56
        }
57
58 2
        return json_decode($response->getBody(), true);
59
    }
60
61
    /**
62
     * @param $topic
63
     * @param $args
64
     * @param $kwargs
65
     * @return array
66
     */
67 3
    private function prepareBody($topic, $args, $kwargs)
68
    {
69 3
        $body = [];
70
71 3
        $body['topic'] = $topic;
72
73 3
        if (null !== $args) {
74 1
            $body['args'] = $args;
75
        }
76
77 3
        if (null !== $kwargs) {
78 2
            $body['kwargs'] = $kwargs;
79
        }
80
81 3
        return $body;
82
    }
83
84
    /**
85
     * @param array $body
86
     * @return array
87
     */
88 3
    private function prepareSignature($body)
89
    {
90 3
        $query = [];
91
92 3
        $seq = mt_rand(0, pow(2, 12));
93 3
        $now = new \DateTime('now', new \DateTimeZone('UTC'));
94 3
        $timestamp = $now->format("Y-m-d\TH:i:s.u\Z");
95
96 3
        $query['seq'] = $seq;
97 3
        $query['timestamp'] = $timestamp;
98
99 3
        if (null !== $this->key && null !== $this->secret) {
100
101 2
            $nonce = mt_rand(0, pow(2, 53));
102 2
            $signature = hash_hmac(
103 2
                'sha256',
104 2
                $this->key . $timestamp . $seq . $nonce . json_encode($body),
105 2
                $this->secret,
106 2
                true
107
            );
108
109 2
            $query['key'] = $this->key;
110 2
            $query['nonce'] = $nonce;
111 2
            $query['signature'] = strtr(base64_encode($signature), '+/', '-_');
112
        }
113
114 3
        return $query;
115
    }
116
}
117