Conditions | 13 |
Paths | 242 |
Total Lines | 72 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
100 | public function actionProgress($filename) |
||
101 | { |
||
102 | $result = []; |
||
103 | $progressFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename . '.progress'; |
||
104 | if (file_exists($progressFile)) { |
||
105 | $content = @file_get_contents($progressFile); |
||
106 | if ($content) { |
||
107 | // get duration of source |
||
108 | preg_match('/Duration: (.*?), start:/', $content, $matches); |
||
109 | if (\count($matches) > 0) { |
||
110 | $rawDuration = $matches[1]; |
||
111 | |||
112 | // rawDuration is in 00:00:00.00 format. This converts it to seconds. |
||
113 | $ar = array_reverse(explode(':', $rawDuration)); |
||
114 | $duration = (float)$ar[0]; |
||
115 | if (!empty($ar[1])) { |
||
116 | $duration += (int)$ar[1] * 60; |
||
117 | } |
||
118 | if (!empty($ar[2])) { |
||
119 | $duration += (int)$ar[2] * 60 * 60; |
||
120 | } |
||
121 | } else { |
||
122 | $duration = 'unknown'; // with GIF as input, duration is unknown |
||
123 | } |
||
124 | |||
125 | // Get the time in the file that is already encoded |
||
126 | preg_match_all('/time=(.*?) bitrate/', $content, $matches); |
||
127 | $rawTime = array_pop($matches); |
||
128 | |||
129 | // this is needed if there is more than one match |
||
130 | if (\is_array($rawTime)) { |
||
131 | $rawTime = array_pop($rawTime); |
||
132 | } |
||
133 | |||
134 | //rawTime is in 00:00:00.00 format. This converts it to seconds. |
||
135 | $ar = array_reverse(explode(':', $rawTime)); |
||
136 | $time = (float)$ar[0]; |
||
137 | if (!empty($ar[1])) { |
||
138 | $time += (int)$ar[1] * 60; |
||
139 | } |
||
140 | if (!empty($ar[2])) { |
||
141 | $time += (int)$ar[2] * 60 * 60; |
||
142 | } |
||
143 | |||
144 | //calculate the progress |
||
145 | if ($duration !== 'unknown') { |
||
146 | $progress = round(($time / $duration) * 100); |
||
147 | } else { |
||
148 | $progress = 'unknown'; |
||
149 | } |
||
150 | |||
151 | // return results |
||
152 | if ($progress !== 'unknown' && $progress < 100) { |
||
153 | $result = [ |
||
154 | 'filename' => $filename, |
||
155 | 'duration' => $duration, |
||
156 | 'time' => $time, |
||
157 | 'progress' => $progress, |
||
158 | ]; |
||
159 | } elseif ($progress === 'unknown') { |
||
160 | $result = [ |
||
161 | 'filename' => $filename, |
||
162 | 'duration' => 'unknown', |
||
163 | 'time' => $time, |
||
164 | 'progress' => 'unknown', |
||
165 | 'message' => 'encoding GIF, can\'t determine duration', |
||
166 | ]; |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | return JsonHelper::encode($result); |
||
172 | } |
||
174 |