Response::setAttributes()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 11
nc 16
nop 1
1
<?php namespace nyx\notify\transports\slack;
2
3
/**
4
 * Slack Response Message
5
 *
6
 * A Response is a special kind of Message that is sent after an Action of an Attachment has been invoked
7
 * inside Slack and Slack has called the designated callback URL or as a response to an invocation of
8
 * a registered Slack command.
9
 *
10
 * @package     Nyx\Notify
11
 * @version     0.1.0
12
 * @author      Michal Chojnacki <[email protected]>
13
 * @copyright   2012-2017 Nyx Dev Team
14
 * @link        https://github.com/unyx/nyx
15
 */
16
class Response extends Message
17
{
18
    /**
19
     * The types a Response can be of.
20
     */
21
    const TYPE_CHANNEL   = 'in_channel';
22
    const TYPE_EPHEMERAL = 'ephemeral';
23
24
    /**
25
     * @var string  The type of the Response. One of the TYPE_* class constants.
26
     */
27
    protected $type;
28
29
    /**
30
     * @var string  The URL this Response should be sent to.
31
     */
32
    protected $url;
33
34
    /**
35
     * @var bool Whether the original Message should be replaced by this Response. When false, this Response
36
     *           will be considered a brand new Message.
37
     */
38
    protected $replaceOriginal;
39
40
    /**
41
     * @var bool Whether the original Message should be deleted. If a new one is sent along this Response,
42
     *           it will be published as a brand new Message.
43
     */
44
    protected $deleteOriginal;
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function setAttributes(array $attributes) : Message
50
    {
51
        parent::setAttributes($attributes);
52
53
        if (isset($attributes['response_type'])) {
54
            $this->setType($attributes['response_type']);
55
        }
56
57
        if (isset($attributes['response_url'])) {
58
            $this->setResponseUrl($attributes['response_url']);
59
        }
60
61
        if (isset($attributes['replace_original'])) {
62
            $this->setReplaceOriginal($attributes['replace_original']);
63
        }
64
65
        if (isset($attributes['delete_original'])) {
66
            $this->setDeleteOriginal($attributes['delete_original']);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Returns the type of the Response.
74
     *
75
     * @return  string
76
     */
77
    public function getType() : ?string
78
    {
79
        return $this->type;
80
    }
81
82
    /**
83
     * Sets the type of the Response.
84
     *
85
     * @param   string  $type
86
     * @return  $this
87
     * @throws  \InvalidArgumentException
88
     */
89
    public function setType(string $type) : Response
90
    {
91
        if ($type !== self::TYPE_CHANNEL && $type !== self::TYPE_EPHEMERAL) {
92
            throw new \InvalidArgumentException('Expected type to be one of ['.self::TYPE_CHANNEL.', '.self::TYPE_CHANNEL.'], got ['.$type.'] instead.');
93
        }
94
95
        $this->type = $type;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Returns the URL this Response should be sent to.
102
     *
103
     * @return  string
104
     */
105
    public function getResponseUrl() : ?string
106
    {
107
        return $this->url;
108
    }
109
110
    /**
111
     * Sets the URL this Response should be sent to.
112
     *
113
     * @param   string  $url
114
     * @return  $this
115
     */
116
    public function setResponseUrl(string $url) : Response
117
    {
118
        $this->url = $url;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Checks whether the original Message should be replaced by this Response.
125
     *
126
     * @return  bool
127
     */
128
    public function shouldReplaceOriginal() : bool
129
    {
130
        return $this->replaceOriginal === true;
131
    }
132
133
    /**
134
     * Sets whether the original Message should be replaced by this Response.
135
     *
136
     * @param   bool    $replace
137
     * @return  $this
138
     */
139
    public function setReplaceOriginal(bool $replace) : Response
140
    {
141
        $this->replaceOriginal = $replace;
142
143
        return $this;
144
    }
145
146
    /**
147
     * Checks whether the original Message should be deleted.
148
     *
149
     * @return  bool
150
     */
151
    public function shouldDeleteOriginal() : bool
152
    {
153
        return $this->deleteOriginal === true;
154
    }
155
156
    /**
157
     * Sets whether the original Message should be deleted.
158
     *
159
     * @param   bool    $delete
160
     * @return  $this
161
     */
162
    public function setDeleteOriginal(bool $delete) : Response
163
    {
164
        $this->deleteOriginal = $delete;
165
166
        return $this;
167
    }
168
169
    /**
170
     * {@inheritDoc}
171
     */
172
    public function toArray() : array
173
    {
174
        return array_merge(parent::toArray(), [
175
            'response_type'    => $this->getType(),
176
            'response_url'     => $this->getResponseUrl(),
177
            'replace_original' => $this->shouldReplaceOriginal(),
178
            'delete_original'  => $this->shouldDeleteOriginal(),
179
        ]);
180
    }
181
}
182