Passed
Pull Request — master (#87)
by Romain
03:09
created

SendResponse::setRecipientId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Response;
6
7
class SendResponse extends AbstractResponse
8
{
9
    private const RECIPIENT_ID = 'recipient_id';
10
    private const MESSAGE_ID = 'message_id';
11
    private const ATTACHMENT_ID = 'attachment_id';
12
13
    private const ERROR = 'error';
14
    private const ERROR_MESSAGE = 'message';
15
    private const ERROR_TYPE = 'type';
16
    private const ERROR_CODE = 'code';
17
    private const ERROR_SUBCODE = 'error_subcode';
18
    private 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
     * @param array $response
62
     */
63
    protected function parseResponse(array $response): void
64
    {
65 9
        if (!$this->hasError($response)) {
66
            $this->recipientId = $response[self::RECIPIENT_ID] ?? null;
67 9
            $this->messageId = $response[self::MESSAGE_ID] ?? null;
68 9
            $this->attachmentId = $response[self::ATTACHMENT_ID] ?? null;
69
        }
70
    }
71
72
    /**
73 9
     * @param array $response
74
     *
75 9
     * @return bool
76 8
     */
77 8
    private function hasError(array $response): bool
78 8
    {
79
        if (isset($response[self::ERROR])) {
80 9
            $this->errorMessage = $response[self::ERROR][self::ERROR_MESSAGE] ?? null;
81
            $this->errorType = $response[self::ERROR][self::ERROR_TYPE] ?? null;
82
            $this->errorCode = $response[self::ERROR][self::ERROR_CODE] ?? null;
83
            $this->errorSubcode = $response[self::ERROR][self::ERROR_SUBCODE] ?? null;
84
            $this->errorFbtraceId = $response[self::ERROR][self::ERROR_FBTRACE_ID] ?? null;
85
86
            return true;
87 9
        }
88
89 9
        return false;
90 1
    }
91 1
92 1
    /**
93 1
     * @return null|string
94 1
     */
95
    public function getRecipientId(): ?string
96 1
    {
97
        return $this->recipientId;
98
    }
99 8
100
    /**
101
     * @return null|string
102
     */
103
    public function getMessageId(): ?string
104
    {
105 7
        return $this->messageId;
106
    }
107 7
108
    /**
109
     * @return null|string
110
     */
111
    public function getAttachmentId(): ?string
112
    {
113 8
        return $this->attachmentId;
114
    }
115 8
116 7
    /**
117
     * @return null|string
118 8
     */
119
    public function getErrorMessage(): ?string
120
    {
121
        return $this->errorMessage;
122
    }
123 7
124
    /**
125 7
     * @return null|string
126
     */
127
    public function getErrorType(): ?string
128
    {
129
        return $this->errorType;
130
    }
131 8
132
    /**
133 8
     * @return null|int
134 7
     */
135
    public function getErrorCode(): ?int
136 8
    {
137
        return $this->errorCode;
138
    }
139
140
    /**
141 4
     * @return null|int
142
     */
143 4
    public function getErrorSubcode(): ?int
144
    {
145
        return $this->errorSubcode;
146
    }
147
148
    /**
149 8
     * @return null|string
150
     */
151 8
    public function getErrorFbtraceId(): ?string
152 2
    {
153
        return $this->errorFbtraceId;
154 8
    }
155
}
156