Builder::update()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 3
nc 2
nop 0
1
<?php
2
namespace PortlandLabs\Slackbot\Slack\Api\Payload;
3
4
use CL\Slack\Payload\ChatPostMessagePayload;
5
use CL\Slack\Payload\ChatPostMessagePayloadResponse;
6
use CL\Slack\Payload\PayloadInterface;
7
use CL\Slack\Payload\PayloadResponseInterface;
8
use PortlandLabs\Slackbot\Slack\Api\Client;
9
10
class Builder
11
{
12
13
    /** @var ChatPostMessagePayload|ChatUpdatePayload */
14
    protected $payload;
15
16
    /** @var ChatPostMessagePayload */
17
    protected $lastPayload;
18
19
    /** @var ChatPostMessagePayloadResponse */
20
    protected $lastResponse;
21
22
    /** @var string */
23
    protected $username;
24
25
    /**
26
     * Prepare to start building
27
     *
28
     * @param string $username
29
     *
30
     * @return Builder
31
     * @internal
32
     */
33
    public function prepare(string $username): Builder
34
    {
35
        $this->username = $username;
36
37
        return $this;
38
    }
39
40
    /**
41
     * Create a new ChatPostMessagePayload builder flow
42
     *
43
     * @param string $text
44
     *
45
     * @return Builder
46
     */
47
    public function send($text = ''): Builder
48
    {
49
        $this->payload = new ChatPostMessagePayload();
50
        $this->payload->setText($text);
51
        $this->payload->setUsername($this->username);
52
        $this->lastResponse = null;
53
        $this->lastPayload = null;
54
55
        return $this;
56
    }
57
58
    /**
59
     * Create a new ChatUpdatePayload builder flow
60
     *
61
     * @return Builder
62
     */
63
    public function update(): Builder
64
    {
65
        if (!$this->lastPayload || !$this->lastResponse) {
66
            throw new \RuntimeException('No chat post to update.');
67
        }
68
69
        $response = $this->lastResponse;
70
        $message = $this->lastPayload;
71
72
        $payload = new ChatUpdatePayload();
73
        $payload->setSlackTimestamp($response->getSlackTimestamp());
74
        $payload->setChannelId($response->getChannelId());
75
        $payload->setText($message->getText());
76
        $payload->setLinkNames($message->getLinkNames());
77
        $payload->setParse($message->getParse());
78
79
        // Set the new payload on the new builder
80
        $this->payload = $payload;
81
82
        return $this;
83
    }
84
85
    public function updateRtmMessage(array $responseData): Builder
86
    {
87
        $payload = new ChatUpdatePayload();
88
        $payload->setSlackTimestamp(array_get($responseData, 'timestamp'));
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated: Arr::get() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
        $payload->setSlackTimestamp(/** @scrutinizer ignore-deprecated */ array_get($responseData, 'timestamp'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
89
        $payload->setChannelId(array_get($responseData, 'channel'));
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated: Arr::get() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

89
        $payload->setChannelId(/** @scrutinizer ignore-deprecated */ array_get($responseData, 'channel'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
90
        $payload->setText(array_get($responseData, 'text'));
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated: Arr::get() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

90
        $payload->setText(/** @scrutinizer ignore-deprecated */ array_get($responseData, 'text'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
91
92
        // Set the new payload on the new builder
93
        $this->payload = $payload;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Set the channel to send to
100
     * Note: Only works when sending new messages, not when updating
101
     *
102
     * @param $channel
103
     *
104
     * @return Builder
105
     */
106
    public function to($channel): Builder
107
    {
108
        if (!$this->payload instanceof ChatPostMessagePayload) {
109
            throw new \RuntimeException('You can\'t update the channel, use ->send to create a new message instead first');
110
        }
111
112
        $this->payload->setChannel($channel);
113
        return $this;
114
    }
115
116
    /**
117
     * Set the icon on the active builder
118
     * Note: Only works when sending new messages, not when updating
119
     *
120
     * @param string $icon
121
     * @return Builder
122
     */
123
    public function withIcon(string $icon): Builder
124
    {
125
        if (!$this->payload instanceof ChatPostMessagePayload) {
126
            throw new \RuntimeException('You can\'t update the icon');
127
        }
128
129
        $this->payload->setIconEmoji($icon);
130
        return $this;
131
    }
132
133
    /**
134
     * Set the icon on the active builder
135
     * Note: Only works when sending new messages, not when updating
136
     *
137
     * @param string $iconUrl
138
     * @return Builder
139
     */
140
    public function withIconUrl(string $iconUrl): Builder
141
    {
142
        if (!$this->payload instanceof ChatPostMessagePayload) {
143
            throw new \RuntimeException('You can\'t update the icon');
144
        }
145
146
        $this->payload->setIconUrl($iconUrl);
147
        return $this;
148
    }
149
150
    /**
151
     * Set the text on the active payload
152
     *
153
     * @param string $text
154
     * @return Builder
155
     */
156
    public function withText(string $text): Builder
157
    {
158
        $this->payload->setText($text);
159
        return $this;
160
    }
161
162
    /**
163
     * Add an attachment to the active payload
164
     *
165
     * @param AttachmentPayload $attachment
166
     * @return Builder
167
     */
168
    public function withAttachment(AttachmentPayload $attachment): Builder
169
    {
170
        $this->payload->addAttachment($attachment);
171
        return $this;
172
    }
173
174
    /**
175
     * Execute the active payload on the client
176
     *
177
     * @param Client $client
178
     *
179
     * @return PayloadResponseInterface
180
     *
181
     * @throws \CL\Slack\Exception\SlackException
182
     * @throws \GuzzleHttp\Exception\GuzzleException
183
     */
184
    public function execute(Client $client): PayloadResponseInterface
185
    {
186
        $result = $client->send($this->payload);
187
188
        if ($result instanceof ChatPostMessagePayloadResponse) {
189
            $this->lastPayload = clone $this->payload;
0 ignored issues
show
Documentation Bug introduced by
It seems like clone $this->payload can also be of type PortlandLabs\Slackbot\Sl...yload\ChatUpdatePayload. However, the property $lastPayload is declared as type CL\Slack\Payload\ChatPostMessagePayload. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
190
            $this->lastResponse = $result;
191
        }
192
193
        return $result;
194
    }
195
196
    /**
197
     * Get the active payload
198
     *
199
     * @return ChatPostMessagePayload|ChatUpdatePayload
200
     */
201
    public function payload(): PayloadInterface
202
    {
203
        return $this->payload;
204
    }
205
}