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