Completed
Branch develop (8166a6)
by Romain
01:52
created

SendResponse::parseResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
namespace Kerox\Messenger\Response;
3
4
use Psr\Http\Message\ResponseInterface;
5
6
class SendResponse extends AbstractResponse
7
{
8
9
    const RECIPIENT_ID = 'recipient_id';
10
    const MESSAGE_ID = 'message_id';
11
    const ATTACHMENT_ID = 'attachment_id';
12
13
    const ERROR = 'error';
14
    const ERROR_MESSAGE = 'message';
15
    const ERROR_TYPE = 'type';
16
    const ERROR_CODE = 'code';
17
    const ERROR_SUBCODE = 'error_subcode';
18
    const ERROR_FBTRACE_ID = 'fbtrace_id';
19
20
    /**
21
     * @var null|string
22
     */
23
    protected $recipientId;
24
25
    /**
26
     * @var null|string
27
     */
28
    protected $messageId;
29
30
    /**
31
     * @var null|string
32
     */
33
    protected $attachmentId;
34
35
    /**
36
     * @var null|string
37
     */
38
    protected $errorMessage;
39
40
    /**
41
     * @var null|string
42
     */
43
    protected $errorType;
44
45
    /**
46
     * @var null|int
47
     */
48
    protected $errorCode;
49
50
    /**
51
     * @var null|int
52
     */
53
    protected $errorSubcode;
54
55
    /**
56
     * @var null|string
57
     */
58
    protected $errorFbtraceId;
59
60
    /**
61
     * Response constructor.
62
     *
63
     * @param \Psr\Http\Message\ResponseInterface $response
64
     */
65
    public function __construct(ResponseInterface $response)
66
    {
67
        parent::__construct($response);
68
    }
69
70
    /**
71
     * @param array $response
72
     * @return void
73
     */
74
    protected function parseResponse(array $response)
75
    {
76
        if (!$this->hasError($response)) {
77
            $this->setRecipientId($response);
78
            $this->setMessageId($response);
79
            $this->setAttachmentId($response);
80
        }
81
    }
82
83
    /**
84
     * @param array $response
85
     * @return bool
86
     */
87
    private function hasError(array $response): bool
88
    {
89
        if (isset($response[self::ERROR])) {
90
            $this->setErrorMessage($response[self::ERROR]);
91
            $this->setErrorType($response[self::ERROR]);
92
            $this->setErrorCode($response[self::ERROR]);
93
            $this->setErrorSubcode($response[self::ERROR]);
94
            $this->setErrorFbtraceId($response[self::ERROR]);
95
96
            return true;
97
        }
98
99
        return false;
100
    }
101
102
    /**
103
     * @return null|string
104
     */
105
    public function getRecipientId()
106
    {
107
        return $this->recipientId;
108
    }
109
110
    /**
111
     * @param array $response
112
     * @return void
113
     */
114
    private function setRecipientId(array $response)
115
    {
116
        if (isset($response[self::RECIPIENT_ID])) {
117
            $this->recipientId = $response[self::RECIPIENT_ID];
118
        }
119
    }
120
121
    /**
122
     * @return null|string
123
     */
124
    public function getMessageId()
125
    {
126
        return $this->messageId;
127
    }
128
129
    /**
130
     * @param array $response
131
     * @return void
132
     */
133
    private function setMessageId(array $response)
134
    {
135
        if (isset($response[self::MESSAGE_ID])) {
136
            $this->messageId = $response[self::MESSAGE_ID];
137
        }
138
    }
139
140
    /**
141
     * @return null|string
142
     */
143
    public function getAttachmentId()
144
    {
145
        return $this->attachmentId;
146
    }
147
148
    /**
149
     * @param array $response
150
     * @return void
151
     */
152
    private function setAttachmentId(array $response)
153
    {
154
        if (isset($response[self::ATTACHMENT_ID])) {
155
            $this->attachmentId = $response[self::ATTACHMENT_ID];
156
        }
157
    }
158
159
    /**
160
     * @return null|string
161
     */
162
    public function getErrorMessage()
163
    {
164
        return $this->errorMessage;
165
    }
166
167
    /**
168
     * @param array $error
169
     * @return void
170
     */
171
    private function setErrorMessage(array $error)
172
    {
173
        if (isset($error[self::ERROR_MESSAGE])) {
174
            $this->errorMessage = $error[self::ERROR_MESSAGE];
175
        }
176
    }
177
178
    /**
179
     * @return null|string
180
     */
181
    public function getErrorType()
182
    {
183
        return $this->errorType;
184
    }
185
186
    /**
187
     * @param array $error
188
     * @return void
189
     */
190
    private function setErrorType(array $error)
191
    {
192
        if (isset($error[self::ERROR_TYPE])) {
193
            $this->errorType = $error[self::ERROR_TYPE];
194
        }
195
    }
196
197
    /**
198
     * @return int|null
199
     */
200
    public function getErrorCode()
201
    {
202
        return $this->errorCode;
203
    }
204
205
    /**
206
     * @param array $error
207
     * @return void
208
     */
209
    private function setErrorCode(array $error)
210
    {
211
        if (isset($error[self::ERROR_CODE])) {
212
            $this->errorCode = $error[self::ERROR_CODE];
213
        }
214
    }
215
216
    /**
217
     * @return int|null
218
     */
219
    public function getErrorSubcode()
220
    {
221
        return $this->errorSubcode;
222
    }
223
224
    /**
225
     * @param array $error
226
     * @return void
227
     */
228
    private function setErrorSubcode(array $error)
229
    {
230
        if (isset($error[self::ERROR_SUBCODE])) {
231
            $this->errorSubcode = $error[self::ERROR_SUBCODE];
232
        }
233
    }
234
235
    /**
236
     * @return null|string
237
     */
238
    public function getErrorFbtraceId()
239
    {
240
        return $this->errorFbtraceId;
241
    }
242
243
    /**
244
     * @param array $error
245
     * @return void
246
     */
247
    private function setErrorFbtraceId(array $error)
248
    {
249
        if (isset($error[self::ERROR_FBTRACE_ID])) {
250
            $this->errorFbtraceId = $error[self::ERROR_FBTRACE_ID];
251
        }
252
    }
253
}
254