Completed
Push — master ( e5e129...73cdcc )
by Vaibhavraj
01:47
created

FlockMessage::sendAs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
ccs 0
cts 10
cp 0
rs 9.2
c 1
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
crap 20
1
<?php
2
3
namespace NotificationChannels\Flock;
4
use NotificationChannels\Flock\Exceptions\CouldNotSendNotification;
5
use Closure;
6
7
class FlockMessage
8
{
9
    /**
10
     * @var array Parameters payload
11
     */
12
    public $payload = [];
13
14
    /**
15
     * @param string $content
16
     *
17
     * @return static
18
     */
19 2
    public static function create($content = '')
20
    {
21 2
        return new static($content);
22
    }
23
24
    /**
25
     * Message constructor.
26
     *
27
     * @param string $content
28
     */
29 5
    public function __construct($content = '')
30
    {
31 5
        $this->content($content);
32 5
    }
33
34
    /**
35
     * Notification message.
36
     *
37
     * @param string $content
38
     *
39
     * @return $this
40
     */
41 5
    public function content($content)
42
    {
43 5
        $this->payload['text'] = $content;
44
45 5
        return $this;
46
    }
47
48
    /**
49
     * Message body as FlockML.
50
     *
51
     * @param string $content
52
     *
53
     * @return $this
54
     */
55
    public function flockml($content)
56
    {
57
        $this->payload['flockml'] = $content;
58
59
        return $this;
60
    }
61
62
    /**
63
     * Text to be shown as the message's notification. (Default is text.)
64
     *
65
     * @param string $notification
66
     *
67
     * @return $this
68
     */
69
    public function notification($notification)
70
    {
71
        $this->payload['notification'] = $notification;
72
73
        return $this;
74
    }
75
76
    /**
77
     * This field is used this if the sender would want to display 
78
     * another name and image as the sender.
79
     *
80
     * @param string $name
81
     * @param string $profileImage
82
     *
83
     * @return $this
84
     */
85
    public function sendAs($name, $profileImage = null)
86
    {
87
        if (!is_string($name)) {
88
            throw CouldNotSendNotification::flockMessageException('Name should be string in sendAs field');
89
        }
90
91
        if (!$profileImage) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $profileImage of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
92
            throw CouldNotSendNotification::flockMessageException('Profile image URL in sendAs field');
93
        }
94
95
        if (! filter_var($profileImage, FILTER_VALIDATE_URL)) {
96
            throw CouldNotSendNotification::flockMessageException('Profile image URL is invalid');
97
        }
98
99
        $this->payload['sendAs']['name'] = $name;
100
        $this->payload['sendAs']['profileImage'] = $profileImage;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Returns params payload.
107
     *
108
     * @return array
109
     */
110 2
    public function toArray()
111
    {
112 2
        return $this->payload;
113
    }
114
115
    /**
116
     * Define an attachment for the message.
117
     *
118
     * @param  \Closure  $callback
119
     * @return $this
120
     */
121
    public function attachments(Closure $callback)
122
    {
123
        $this->payload['attachments'][] = $attachment = new FlockAttachment;
124
125
        $callback($attachment);
126
127
        return $this;
128
    }
129
}
130