Completed
Push — master ( 820cfa...bfe298 )
by
unknown
03:40
created

HipChatFile::room()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace NotificationChannels\HipChat;
4
5
class HipChatFile
6
{
7
    /**
8
     * The HipChat room identifier.
9
     *
10
     * @var int|string
11
     */
12
    public $room;
13
14
    /**
15
     * Message content that is sent along with the file.
16
     *
17
     * @var string
18
     */
19
    public $content = '';
20
21
    /**
22
     * File content.
23
     * Can be a resource, stream, string.
24
     * 
25
     * @var mixed
26
     */
27
    public $fileContent;
28
29
    /**
30
     * A new file name for the file. 
31
     * 
32
     * @var string
33
     */
34
    public $fileName = '';
35
36
    /**
37
     * A valid mime type of the file content.
38
     * 
39
     * @var string
40
     */
41
    public $fileType = '';
42
43
    /**
44
     * Create an instance of HipChatFile.
45
     *
46
     * @param string $path
47
     */
48
    public function __construct($path = '')
49
    {
50
        if (!empty($path)) {
51
            $this->path($path);
52
        }
53
    }
54
55
    /**
56
     * Create an instance of HipChatFile.
57
     *
58
     * @param string $path
59
     * @return static
60
     */
61
    public static function create($path = '')
62
    {
63
        return new static($path);
64
    }
65
66
    /**
67
     * Set the HipChat room to share the file in.
68
     *
69
     * @param string|integer $room
70
     * @return $this
71
     */
72
    public function room($room)
73
    {
74
        $this->room = $room;
75
        return $this;
76
    }
77
78
    /**
79
     * Set the content of the message.
80
     *
81
     * @param  string  $content
82
     * @return $this
83
     */
84
    public function content($content)
85
    {
86
        $this->content = trim($content);
87
        return $this;
88
    }
89
90
    /**
91
     * Alias for content().
92
     *
93
     * @param  string  $text
94
     * @return $this
95
     */
96
    public function text($text)
97
    {
98
        return $this->content($text);
99
    }
100
101
    /**
102
     * Set the file path.
103
     *
104
     * @param  string  $path
105
     * @return $this
106
     */
107
    public function path($path)
108
    {
109
        if (empty($this->filename)) {
0 ignored issues
show
Bug introduced by
The property filename does not seem to exist. Did you mean fileName?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
110
            $this->fileName(basename($path));
111
        }
112
113
        if (empty($this->fileType)) {
114
            $this->fileType(mime_content_type($path));
115
        }
116
117
        $this->fileContent(fopen($path, 'r'));
118
119
        return $this;
120
    }
121
122
    /**
123
     * Explicitly set the content of the file.
124
     *
125
     * @param $content
126
     * @return $this
127
     */
128
    public function fileContent($content)
129
    {
130
        if (is_resource($this->fileContent)) {
131
            fclose($this->fileContent);
132
        }
133
134
        $this->fileContent = $content;
135
136
        if (is_resource($content) && empty($this->fileType)) {
137
            $this->fileType($this->getTypeFromResource($content));
138
        }
139
140
        return $this;
141
    }
142
    
143
    /**
144
     * Set the new name of the file.
145
     *
146
     * @param  string  $fileName
147
     * @return $this
148
     */
149
    public function fileName($fileName)
150
    {
151
        $this->fileName = $fileName;
152
        return $this;
153
    }
154
155
    /**
156
     * Set the mime type of the file content.
157
     *
158
     * @param  string  $fileType
159
     * @return $this
160
     */
161
    public function fileType($fileType)
162
    {
163
        $this->fileType= $fileType;
164
        return $this;
165
    }
166
167
    /**
168
     * Get array representation.
169
     *
170
     * @return array
171
     */
172
    public function toArray()
173
    {
174
        return [
175
            'content' => $this->fileContent,
176
            'filename' => $this->fileName,
177
            'file_type' => $this->fileType,
178
            'message' => $this->content,
179
        ];
180
    }
181
182
    /**
183
     * Get the media type from a resource.
184
     *
185
     * @param $resource
186
     * @return string
187
     */
188
    protected function getTypeFromResource($resource)
189
    {
190
        $meta = stream_get_meta_data($resource);
191
        return isset($meta['mediatype']) ? $meta['mediatype'] : null;
192
    }
193
}