Completed
Push — master ( f93ccb...d2e371 )
by Alessandro
03:39
created

Publisher::publish()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
crap 2.0438
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 \GuzzleHttp\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 2
    public function __construct(Client $client, $key, $secret)
29
    {
30 2
        $this->client = $client;
31 2
        $this->key = $key;
32 2
        $this->secret = $secret;
33 2
    }
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 1
    public function publish($topic, array $args = null, array $kwargs = null)
43
    {
44 1
        $jsonBody = $this->prepareBody($topic, $args, $kwargs);
45
46
        try {
47 1
            $response = $this->client->post(
48 1
                '',
49
                [
50 1
                    'json' => $jsonBody,
51 1
                    'query' => $this->prepareSignature($jsonBody)
52
                ]
53
            );
54
        } catch (\Exception $e) {
55
            throw new PublishRequestException($e->getMessage(), 500, $e);
56
        }
57
58 1
        return json_decode($response->getBody(), true);
59
    }
60
61
    /**
62
     * @param $topic
63
     * @param $args
64
     * @param $kwargs
65
     * @return array
66
     */
67 1
    private function prepareBody($topic, $args, $kwargs)
68
    {
69 1
        $body = array();
70
71 1
        $body['topic'] = $topic;
72
73 1
        if (null !== $args) {
74
            $body['args'] = $args;
75
        }
76
77 1
        if (null !== $kwargs) {
78 1
            $body['kwargs'] = $kwargs;
79
        }
80
81 1
        return $body;
82
    }
83
84
    /**
85
     * @param array $body
86
     * @return array
87
     */
88 1
    private function prepareSignature($body)
89
    {
90 1
        $query = array();
91
92 1
        $seq = mt_rand(0, pow(2, 12));
93 1
        $now = new \DateTime('now', new \DateTimeZone('UTC'));
94 1
        $timestamp = $now->format("Y-m-d\TH:i:s.u\Z");
95
96 1
        $query['seq'] = $seq;
97 1
        $query['timestamp'] = $timestamp;
98
99 1
        if (null !== $this->key && null !== $this->secret) {
100
101
            $nonce = mt_rand(0, pow(2, 53));
102
            $signature = hash_hmac(
103
                'sha256',
104
                $this->key . $timestamp . $seq . $nonce . json_encode($body),
105
                $this->secret,
106
                true
107
            );
108
109
            $query['key'] = $this->key;
110
            $query['nonce'] = $nonce;
111
            $query['signature'] = strtr(base64_encode($signature), '+/', '-_');
112
        }
113
114 1
        return $query;
115
    }
116
}
117