| 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 |
||
| 101 | public function actionProgress($filename) |
||
| 102 | { |
||
| 103 | $result = []; |
||
| 104 | $progressFile = sys_get_temp_dir().DIRECTORY_SEPARATOR.$filename.'.progress'; |
||
| 105 | if (file_exists($progressFile)) { |
||
| 106 | $content = @file_get_contents($progressFile); |
||
| 107 | if ($content) { |
||
| 108 | // get duration of source |
||
| 109 | preg_match('/Duration: (.*?), start:/', $content, $matches); |
||
| 110 | if (\count($matches) > 0) { |
||
| 111 | $rawDuration = $matches[1]; |
||
| 112 | |||
| 113 | // rawDuration is in 00:00:00.00 format. This converts it to seconds. |
||
| 114 | $ar = array_reverse(explode(':', $rawDuration)); |
||
| 115 | $duration = (float)$ar[0]; |
||
| 116 | if (!empty($ar[1])) { |
||
| 117 | $duration += (int)$ar[1] * 60; |
||
| 118 | } |
||
| 119 | if (!empty($ar[2])) { |
||
| 120 | $duration += (int)$ar[2] * 60 * 60; |
||
| 121 | } |
||
| 122 | } else { |
||
| 123 | $duration = 'unknown'; // with GIF as input, duration is unknown |
||
| 124 | } |
||
| 125 | |||
| 126 | // Get the time in the file that is already encoded |
||
| 127 | preg_match_all('/time=(.*?) bitrate/', $content, $matches); |
||
| 128 | $rawTime = array_pop($matches); |
||
| 129 | |||
| 130 | // this is needed if there is more than one match |
||
| 131 | if (\is_array($rawTime)) { |
||
| 132 | $rawTime = array_pop($rawTime); |
||
| 133 | } |
||
| 134 | |||
| 135 | //rawTime is in 00:00:00.00 format. This converts it to seconds. |
||
| 136 | $ar = array_reverse(explode(':', $rawTime)); |
||
| 137 | $time = (float)$ar[0]; |
||
| 138 | if (!empty($ar[1])) { |
||
| 139 | $time += (int)$ar[1] * 60; |
||
| 140 | } |
||
| 141 | if (!empty($ar[2])) { |
||
| 142 | $time += (int)$ar[2] * 60 * 60; |
||
| 143 | } |
||
| 144 | |||
| 145 | //calculate the progress |
||
| 146 | if ($duration !== 'unknown') { |
||
| 147 | $progress = round(($time / $duration) * 100); |
||
| 148 | } else { |
||
| 149 | $progress = 'unknown'; |
||
| 150 | } |
||
| 151 | |||
| 152 | // return results |
||
| 153 | if ($progress !== 'unknown' && $progress < 100) { |
||
| 154 | $result = [ |
||
| 155 | 'filename' => $filename, |
||
| 156 | 'duration' => $duration, |
||
| 157 | 'time' => $time, |
||
| 158 | 'progress' => $progress, |
||
| 159 | ]; |
||
| 160 | } elseif ($progress === 'unknown') { |
||
| 161 | $result = [ |
||
| 162 | 'filename' => $filename, |
||
| 163 | 'duration' => 'unknown', |
||
| 164 | 'time' => $time, |
||
| 165 | 'progress' => 'unknown', |
||
| 166 | 'message' => 'encoding GIF, can\'t determine duration', |
||
| 167 | ]; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | return JsonHelper::encode($result); |
||
| 173 | } |
||
| 175 |