FlockAttachment   C
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Coupling/Cohesion

Components 10
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 20
lcom 10
cbo 2
dl 0
loc 238
ccs 0
cts 73
cp 0
rs 6.6666
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 6 1
A title() 0 6 1
A description() 0 6 1
A appId() 0 6 1
A color() 0 6 1
A forward() 0 9 2
A url() 0 9 2
A views() 0 8 1
A downloads() 0 12 3
B buttons() 0 20 7
1
<?php
2
3
namespace NotificationChannels\Flock;
4
5
use Closure;
6
use NotificationChannels\Flock\Exceptions\CouldNotSendNotification;
7
8
class FlockAttachment
9
{
10
    /**
11
     * A unique identifier for the attachment as provided by your app.
12
     *
13
     * @var string | null
14
     */
15
    public $id;
16
17
    /**
18
     * The title of the attachment.
19
     *
20
     * @var string
21
     */
22
    public $title;
23
24
    /**
25
     * A longer description of the attachment.
26
     *
27
     * @var string
28
     */
29
    public $description;
30
31
    /**
32
     * App id for the app that sent the attachment. Any value that your app provides for this attribute
33
     * will be overwritten with your app's actual id by Flock.
34
     *
35
     * @var string
36
     */
37
    public $appId;
38
39
    /**
40
     * A hex value (e.g. "#0ABE51") for the color bar.
41
     *
42
     * @var string
43
     */
44
    public $color;
45
46
    /**
47
     * Provides user visible views for the attachment. See below for more details.
48
     *
49
     * @var views
50
     */
51
    public $views;
52
53
    /**
54
     * The URL to open when user clicks an attachment, if no widget or FlockML is provided.
55
     * When generating a URL Preview this should always be set.
56
     *
57
     * @var string
58
     */
59
    public $url;
60
61
    /**
62
     * If true, the attachment can be forwarded. Default value is false.
63
     *
64
     * @var bool
65
     */
66
    public $forward = false;
67
68
    /**
69
     * An array of download objects. Note: As of now this array should contain at max one object.
70
     *
71
     * @var array
72
     */
73
    public $downloads;
74
75
    /**
76
     * An array of attachment buttons.
77
     *
78
     * @var array
79
     */
80
    public $buttons;
81
82
    /**
83
     * Set the id of attachment.
84
     *
85
     * @param   string  $id
86
     *
87
     * @return $this
88
     */
89
    public function id($id)
90
    {
91
        $this->id = $id;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Set title of attachment.
98
     *
99
     * @param string $title
100
     *
101
     * @return $this
102
     */
103
    public function title($title)
104
    {
105
        $this->title = $title;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Set description of attachment.
112
     * @param string $description
113
     *
114
     * @return $this
115
     */
116
    public function description($description)
117
    {
118
        $this->description = $description;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Set application id for attachment.
125
     *
126
     * @param string $appId
127
     *
128
     * @return $this
129
     */
130
    public function appId($appId)
131
    {
132
        $this->appId = $appId;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Set color of attachment.
139
     *
140
     * @param string $color
141
     *
142
     * @return $this
143
     */
144
    public function color($color)
145
    {
146
        $this->color = $color;
147
148
        return $this;
149
    }
150
151
    /**
152
     * Set forward option. Default false.
153
     *
154
     * @param bool $forward
155
     *
156
     * @return $this
157
     */
158
    public function forward($forward)
159
    {
160
        if (! is_bool($forward)) {
161
            throw CouldNotSendNotification::flockAttachmentForwardException('Forward field should be boolean.');
162
        }
163
        $this->forward = $forward;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Set url for preview.
170
     *
171
     * @param string $url
172
     *
173
     * @return $this
174
     */
175
    public function url($url)
176
    {
177
        if (! filter_var($url, FILTER_VALIDATE_URL)) {
178
            throw CouldNotSendNotification::flockAttachmentUrlException('Invalid URL in attachment');
179
        }
180
        $this->url = $url;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Define an view for the attachment.
187
     *
188
     * @param  \Closure  $callback
189
     * @return $this
190
     */
191
    public function views(Closure $callback)
192
    {
193
        $this->views = $view = new FlockAttachmentView;
0 ignored issues
show
Documentation Bug introduced by
It seems like $view = new \Notificatio...k\FlockAttachmentView() of type object<NotificationChann...ck\FlockAttachmentView> is incompatible with the declared type object<NotificationChannels\Flock\views> of property $views.

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...
194
195
        $callback($view);
196
197
        return $this;
198
    }
199
200
    /**
201
     * Define an downloads for the attachment.
202
     *
203
     * @param  array $files
204
     * @return $this
205
     */
206
    public function downloads($files)
207
    {
208
        foreach ($files as $key => $file) {
209
            if (! filter_var($file['src'], FILTER_VALIDATE_URL)) {
210
                throw CouldNotSendNotification::flockAttachmentDownloadException('Invalid source for attachment download.');
211
            }
212
213
            $this->downloads[] = $file;
214
        }
215
216
        return $this;
217
    }
218
219
    /**
220
     * Define an buttons for the attachment.
221
     *
222
     * @param  array    $buttons
223
     * @return $this
224
     */
225
    public function buttons($buttons)
226
    {
227
        foreach ($buttons as $key => $button) {
228
            if (! filter_var($button['icon'], FILTER_VALIDATE_URL)) {
229
                throw CouldNotSendNotification::flockAttachmentButtonException('Invalid Icon URL for attachment button.');
230
            }
231
232
            if (! isset($button['action']) || ! is_array($button['action'])) {
233
                throw CouldNotSendNotification::flockAttachmentButtonException('Attachment button action is required and needs to be an array');
234
            }
235
236
            if (! isset($button['action']['url']) || ! filter_var($button['action']['url'], FILTER_VALIDATE_URL)) {
237
                throw CouldNotSendNotification::flockAttachmentButtonException('Attachment button action url is invalid or missing.');
238
            }
239
240
            $this->buttons[] = $button;
241
        }
242
243
        return $this;
244
    }
245
}
246