Completed
Push — master ( 3f8806...162df9 )
by Peter
02:17
created

HipChatFile::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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|int $room
70
     * @return $this
71
     */
72
    public function room($room)
73
    {
74
        $this->room = $room;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Set the content of the message.
81
     *
82
     * @param  string  $content
83
     * @return $this
84
     */
85
    public function content($content)
86
    {
87
        $this->content = trim($content);
88
89
        return $this;
90
    }
91
92
    /**
93
     * Alias for content().
94
     *
95
     * @param  string  $text
96
     * @return $this
97
     */
98
    public function text($text)
99
    {
100
        return $this->content($text);
101
    }
102
103
    /**
104
     * Set the file path.
105
     *
106
     * @param  string  $path
107
     * @return $this
108
     */
109
    public function path($path)
110
    {
111
        if (empty($this->fileName)) {
112
            $this->fileName(basename($path));
113
        }
114
115
        if (empty($this->fileType)) {
116
            $this->fileType(mime_content_type($path));
117
        }
118
119
        $this->fileContent(fopen($path, 'r'));
120
121
        return $this;
122
    }
123
124
    /**
125
     * Explicitly set the content of the file.
126
     *
127
     * @param $content
128
     * @return $this
129
     */
130
    public function fileContent($content)
131
    {
132
        if (is_resource($this->fileContent)) {
133
            fclose($this->fileContent);
134
        }
135
136
        $this->fileContent = $content;
137
138
        if (is_resource($content) && empty($this->fileType)) {
139
            $this->fileType($this->getTypeFromResource($content));
140
        }
141
142
        return $this;
143
    }
144
145
    /**
146
     * Set the new name of the file.
147
     *
148
     * @param  string  $fileName
149
     * @return $this
150
     */
151
    public function fileName($fileName)
152
    {
153
        $this->fileName = $fileName;
154
155
        return $this;
156
    }
157
158
    /**
159
     * Set the mime type of the file content.
160
     *
161
     * @param  string  $fileType
162
     * @return $this
163
     */
164
    public function fileType($fileType)
165
    {
166
        $this->fileType = $fileType;
167
168
        return $this;
169
    }
170
171
    /**
172
     * Get array representation.
173
     *
174
     * @return array
175
     */
176
    public function toArray()
177
    {
178
        return [
179
            'content' => $this->fileContent,
180
            'filename' => $this->fileName,
181
            'file_type' => $this->fileType,
182
            'message' => $this->content,
183
        ];
184
    }
185
186
    /**
187
     * Get the media type from a resource.
188
     *
189
     * @param $resource
190
     * @return string
191
     */
192
    protected function getTypeFromResource($resource)
193
    {
194
        $meta = stream_get_meta_data($resource);
195
196
        return isset($meta['mediatype']) ? $meta['mediatype'] : null;
197
    }
198
}
199