Completed
Pull Request — master (#20)
by Michael
08:11 queued 06:37
created

HipChatFile::fileContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

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