ZohoCliq::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 8
nop 3
1
<?php
2
3
namespace Asad\ZohoCliq;
4
5
use GuzzleHttp\Client as Guzzle;
6
use RuntimeException;
7
8
class ZohoCliq
9
{
10
    /**
11
     * Zoho Cliq Auth Token
12
     * @var string
13
     */
14
    protected $authtoken;
15
16
    /**
17
     * Default send to
18
     * @var string
19
     */
20
    protected $send_to;
21
22
    /**
23
     * Default Channel to send message
24
     * @var string
25
     */
26
    protected $channel;
27
28
    /**
29
     * Zoho Cliq message endpoint
30
     * @var string
31
     */
32
    protected $endpoint;
33
34
    /**
35
     * Zoho Cliq message
36
     * @var string
37
     */
38
    protected $message;
39
40
    /**
41
     * Zoho Cliq card
42
     * @var string
43
     */
44
    protected $card;
45
46
    /**
47
     * The Guzzle HTTP client instance.
48
     *
49
     * @var \GuzzleHttp\Client
50
     */
51
    protected $guzzle;
52
53
    /**
54
     * Instantiate a new Client.
55
     *
56
     * @param string $endpoint
0 ignored issues
show
Bug introduced by
There is no parameter named $endpoint. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
57
     * @param array $attributes
58
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
59
     */
60
    public function __construct($authtoken, array $attributes = [], Guzzle $guzzle = null)
61
    {
62
        $this->authtoken = $authtoken;
63
64
        if (isset($attributes['channel'])) {
65
            $this->setDefaultChannel($attributes['channel']);
66
        }
67
68
        if (isset($attributes['send_to'])) {
69
            $this->setDefaultSendTo($attributes['send_to']);
70
        }
71
72
        $this->guzzle = $guzzle ?: new Guzzle;
73
    }
74
75
    /**
76
     * Set Default AuthToken
77
     * @param string $authtoken
78
     * @return void
79
     */
80
    public function setAuthToken($authtoken)
81
    {
82
        $this->authtoken = $authtoken;
83
    }
84
85
    /**
86
     * Get Default Authtoken
87
     * @return string
88
     */
89
    public function getAuthToken(): string
90
    {
91
        return $this->authtoken;
92
    }
93
94
    /**
95
     * Set Default SendTo
96
     * @param string $send_to
97
     * @return void
98
     */
99
    public function setDefaultSendTo($send_to)
100
    {
101
        $this->send_to = $send_to;
102
    }
103
104
    /**
105
     * Get Default Send To
106
     * @return string
107
     */
108
    public function getDefaultSendTo(): string
109
    {
110
        return $this->send_to;
111
    }
112
113
    /**
114
     * Set Default Channel
115
     * @param string $channel
116
     * @return void
117
     */
118
    public function setDefaultChannel($channel)
119
    {
120
        $this->channel = $channel;
121
    }
122
123
    /**
124
     * Get Default Channel
125
     * @return string
126
     */
127
    public function getDefaultChannel(): string
128
    {
129
        return $this->channel;
130
    }
131
132
    /**
133
     * Generate Cliq Endpoint from send_to and channel name
134
     * @return void
135
     */
136
137
    public function generateCliqEndpoint()
138
    {
139
        $this->endpoint = "https://cliq.zoho.com/api/v2/" . $this->getDefaultSendTo() . "/" . $this->getDefaultChannel() . "/message";
140
    }
141
142
    /**
143
     * Get Default Endpoint
144
     * @return string
145
     */
146
    public function getEndpoint(): string
147
    {
148
        return $this->endpoint;
149
    }
150
151
    /**
152
     * Get Message
153
     * @return string
154
     */
155
    public function getMessage(): string
156
    {
157
        return $this->message;
158
    }
159
160
    /**
161
     * Set Send To onfly
162
     * @return self
163
     */
164
165
    public function toChannel(): self
166
    {
167
        $this->send_to = 'channelsbyname';
168
        return $this;
169
    }
170
171
    /**
172
     * Set Send To onfly
173
     * @return self
174
     */
175
176
    public function toBot(): self
177
    {
178
        $this->send_to = 'bots';
179
        return $this;
180
    }
181
182
    /**
183
     * Set Send To onfly
184
     * @return self
185
     */
186
187
    public function toChat(): self
188
    {
189
        $this->send_to = 'chats';
190
        return $this;
191
    }
192
193
    /**
194
     * Set Send To onfly
195
     * @return self
196
     */
197
198
    public function toBuddy(): self
199
    {
200
        $this->send_to = 'buddies';
201
        return $this;
202
    }
203
204
    /**
205
     * Change Channel on fly
206
     */
207
    public function to($channel)
208
    {
209
        $this->channel = $channel;
210
        return $this;
211
    }
212
213
    /**
214
     * Create Message Card
215
     * @return self
216
     */
217
    public function card(array $attributes): self
218
    {
219
        $title = isset($attributes['title']) ? $attributes['title'] : "BUG";
220
        $theme = isset($attributes['theme']) ? $attributes['theme'] : "modern-inline";
221
        $thumbnail = isset($attributes['thumbnail']) ? $attributes['thumbnail'] : "https://en.gravatar.com/userimage/57826719/1bcf7f90b22897258d2b3e4e84875218.jpg";
222
        $card = [
223
            'title' => $title,
224
            'theme' => $theme,
225
            'thumbnail' => $thumbnail
226
        ];
227
        $this->card = $card;
0 ignored issues
show
Documentation Bug introduced by
It seems like $card of type array<string,?,{"title":...":"?","thumbnail":"?"}> is incompatible with the declared type string of property $card.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
228
        return $this;
229
    }
230
231
    /**
232
     * Send message to selected destination
233
     * */
234
    public function send($message)
235
    {
236
        $this->message = $message;
237
        $this->generateCliqEndpoint();
238
        $payload = $this->createBody($message);
239
        $encoded = json_encode($payload, JSON_UNESCAPED_UNICODE);
240
241
        if ($encoded === false) {
242
            throw new RuntimeException(sprintf('JSON encoding error %s: %s', json_last_error(), json_last_error_msg()));
243
        }
244
        $this->guzzle->post($this->endpoint, [
245
            'query' => $this->prepareAuth(),
246
            'body' => $encoded
247
        ]);
248
    }
249
250
    public function createBody($message)
251
    {
252
        if (gettype($this->card) == 'array') {
253
            $payload['card'] = $this->card;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$payload was never initialized. Although not strictly required by PHP, it is generally a good practice to add $payload = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
254
        }
255
        $payload['text'] = $message;
0 ignored issues
show
Bug introduced by
The variable $payload does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
256
        return $payload;
257
    }
258
259
    public function prepareAuth()
260
    {
261
        return [
262
            'authtoken' => $this->getAuthToken()
263
        ];
264
    }
265
}
266