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