HipChatFile   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 196
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0
wmc 18
lcom 2
cbo 0

11 Methods

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