Api::createChat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace erjanmx\nambaone;
4
5
use Closure;
6
use GuzzleHttp\Client as Guzzle;
7
use GuzzleHttp\Exception\BadResponseException;
8
use InvalidArgumentException;
9
10
class Api
11
{
12
    /**
13
     * Api endpoint
14
     * 
15
     * @var string
16
     */
17
    protected $api_url = 'https://api.namba1.co';
18
19
    /**
20
     * File managing endpoint
21
     *
22
     * @var string
23
     */
24
    protected $api_url_files = 'https://files.namba1.co';
25
26
    /**
27
     * @var Api token
28
     */
29
    protected $token;
30
31
    /**
32
     * @var Guzzle http client
33
     */
34
    protected $guzzle;
35
36
    /**
37
     * Api constructor.
38
     * 
39
     * @param $token
40
     */
41
    function __construct($token)
42
    {
43
        $this->token = $token;
44
45
        $this->guzzle = new Guzzle(['base_uri' => $this->api_url]);
46
    }
47
48
    /**
49
     * Prepare Auth-token
50
     * 
51
     * @return array
52
     */
53
    protected function getHeaders()
54
    {
55
        return [
56
            'X-Namba-Auth-Token' => $this->token
57
        ];
58
    }
59
60
    /**
61
     * Chat creation api call
62
     * 
63
     * @param $user_id
64
     * @param string $name
65
     * @param string $image
66
     * @return mixed
67
     */
68
    public function createChat($user_id, $name = '', $image = '')
69
    {
70
        $headers = $this->getHeaders();
71
72
        $form_params = array_merge(
73
            compact('name', 'image'), [
74
                'members[]' => $user_id
75
            ]
76
        );
77
78
        $options = compact('form_params', 'headers');
79
80
        return json_decode($this->guzzle->post('/chats/create', $options)->getBody());
81
    }
82
83
    /**
84
     * Message sending api call
85
     * 
86
     * @param Message $message
87
     * @return mixed
88
     */
89
    public function sendMessage(Message $message)
90
    {
91
        $headers = $this->getHeaders();
92
        $form_params = [
93
            'type' => $message->getType(),
94
            'content' => $message->getContent(),
95
        ];
96
97
        $options = compact('form_params', 'headers');
98
99
        $request = $this->guzzle->post(
100
            sprintf('/chats/%d/write', $message->getChatId())
101
        , $options);
102
103
        return json_decode($request->getBody());
104
    }
105
106
    /**
107
     * Gets content of uploaded file by its token
108
     *
109
     * @param Message $message
110
     * @return string|json
0 ignored issues
show
Bug introduced by
The type erjanmx\nambaone\json was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
     */
112
    public function getFile($token)
113
    {
114
        $response = $this->guzzle->get($this->api_url_files, [
115
            'query' => compact('token'),
116
        ])->getBody();
117
        
118
        json_decode($response);
119
        
120
        if (json_last_error() == JSON_ERROR_NONE) {
121
            throw new ClientException('File not found');
122
        }
123
        
124
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response returns the type Psr\Http\Message\StreamInterface which is incompatible with the documented return type erjanmx\nambaone\json|string.
Loading history...
125
    }
126
127
    /**
128
     * Uploads file to api endpoint and returns its token
129
     *
130
     * @param $filename
131
     * @return string|null
132
     */
133
    public function uploadFile($filename)
134
    {
135
        if (! file_exists($filename)) {
136
            throw new \BadMethodCallException('File not exists');
137
        }
138
        
139
        $response = json_decode($this->guzzle->post($this->api_url_files, [
140
             'multipart' => [
141
                 [
142
                    'name' => 'file',
143
                    'contents' => fopen($filename, 'r')
144
                 ]
145
             ]
146
        ])->getBody());
147
148
        if ((isset($response->success)) && ($response->success)) {
149
            return $response->file;
150
        } else {
151
            throw new ClientException('File is not uploaded');
152
        }
153
    }
154
}
155