| Conditions | 33 |
| Paths | 5876 |
| Total Lines | 137 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 74 | public function beginExport($params) |
||
| 75 | { |
||
| 76 | $gzip = false; |
||
| 77 | if ($params[4] === true) { |
||
| 78 | $gzip = true; |
||
| 79 | } |
||
| 80 | |||
| 81 | $fromDate = $toDate = ''; |
||
| 82 | $path = $params[0]; |
||
| 83 | |||
| 84 | // Check if the path ends with dir separator. |
||
| 85 | if (substr($path, -1) !== DS) { |
||
|
|
|||
| 86 | $path .= DS; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Check if it's a directory. |
||
| 90 | if (! is_dir($path)) { |
||
| 91 | $this->echoOut('Folder does not exist: '.$path); |
||
| 92 | |||
| 93 | return $this->returnValue(); |
||
| 94 | } |
||
| 95 | |||
| 96 | // Check if we can write to it. |
||
| 97 | if (! is_writable($path)) { |
||
| 98 | $this->echoOut('Folder is not writable: '.$path); |
||
| 99 | |||
| 100 | return $this->returnValue(); |
||
| 101 | } |
||
| 102 | |||
| 103 | // Check if the from date is the proper format. |
||
| 104 | if (isset($params[1]) && $params[1] !== '') { |
||
| 105 | if (! $this->checkDate($params[1])) { |
||
| 106 | return $this->returnValue(); |
||
| 107 | } |
||
| 108 | $fromDate = $params[1]; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Check if the to date is the proper format. |
||
| 112 | if (isset($params[2]) && $params[2] !== '') { |
||
| 113 | if (! $this->checkDate($params[2])) { |
||
| 114 | return $this->returnValue(); |
||
| 115 | } |
||
| 116 | $toDate = $params[2]; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Check if the group_id exists. |
||
| 120 | if (isset($params[3]) && $params[3] !== 0) { |
||
| 121 | if (! is_numeric($params[3])) { |
||
| 122 | $this->echoOut('The group ID is not a number: '.$params[3]); |
||
| 123 | |||
| 124 | return $this->returnValue(); |
||
| 125 | } |
||
| 126 | $groups = $this->pdo->query('SELECT id, name FROM groups WHERE id = '.$params[3]); |
||
| 127 | if (count($groups) === 0) { |
||
| 128 | $this->echoOut('The group ID is not in the DB: '.$params[3]); |
||
| 129 | |||
| 130 | return $this->returnValue(); |
||
| 131 | } |
||
| 132 | } else { |
||
| 133 | $groups = $this->pdo->query('SELECT id, name FROM groups'); |
||
| 134 | } |
||
| 135 | |||
| 136 | $exported = 0; |
||
| 137 | // Loop over groups to take less RAM. |
||
| 138 | foreach ($groups as $group) { |
||
| 139 | $currentExport = 0; |
||
| 140 | // Get all the releases based on the parameters. |
||
| 141 | $releases = $this->releases->getForExport($fromDate, $toDate, $group['id']); |
||
| 142 | $totalFound = count($releases); |
||
| 143 | if ($totalFound === 0) { |
||
| 144 | if ($this->echoCLI) { |
||
| 145 | echo 'No releases found to export for group: '.$group['name'].PHP_EOL; |
||
| 146 | } |
||
| 147 | continue; |
||
| 148 | } |
||
| 149 | if ($this->echoCLI) { |
||
| 150 | echo 'Found '.$totalFound.' releases to export for group: '.$group['name'].PHP_EOL; |
||
| 151 | } |
||
| 152 | |||
| 153 | // Create a path to store the new NZB files. |
||
| 154 | $currentPath = $path.$this->safeFilename($group['name']).DS; |
||
| 155 | if (! is_dir($currentPath)) { |
||
| 156 | mkdir($currentPath); |
||
| 157 | } |
||
| 158 | foreach ($releases as $release) { |
||
| 159 | |||
| 160 | // Get path to the NZB file. |
||
| 161 | $nzbFile = $this->nzb->NZBPath($release['guid']); |
||
| 162 | // Check if it exists. |
||
| 163 | if ($nzbFile === false) { |
||
| 164 | if ($this->echoCLI) { |
||
| 165 | echo 'Unable to find NZB for release with GUID: '.$release['guid']; |
||
| 166 | } |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | // Create path to current file. |
||
| 171 | $currentFile = $currentPath.$this->safeFilename($release['searchname']); |
||
| 172 | |||
| 173 | // Check if the user wants them in gzip, copy it if so. |
||
| 174 | if ($gzip) { |
||
| 175 | if (! copy($nzbFile, $currentFile.'.nzb.gz')) { |
||
| 176 | if ($this->echoCLI) { |
||
| 177 | echo 'Unable to export NZB with GUID: '.$release['guid']; |
||
| 178 | } |
||
| 179 | continue; |
||
| 180 | } |
||
| 181 | // If not, decompress it and create a file to store it in. |
||
| 182 | } else { |
||
| 183 | $nzbContents = Utility::unzipGzipFile($nzbFile); |
||
| 184 | if (! $nzbContents) { |
||
| 185 | if ($this->echoCLI) { |
||
| 186 | echo 'Unable to export NZB with GUID: '.$release['guid']; |
||
| 187 | } |
||
| 188 | continue; |
||
| 189 | } |
||
| 190 | $fh = fopen($currentFile.'.nzb', 'w'); |
||
| 191 | fwrite($fh, $nzbContents); |
||
| 192 | fclose($fh); |
||
| 193 | } |
||
| 194 | |||
| 195 | $currentExport++; |
||
| 196 | |||
| 197 | if ($this->echoCLI && $currentExport % 10 === 0) { |
||
| 198 | echo 'Exported '.$currentExport.' of '.$totalFound.' nzbs for group: '.$group['name']."\r"; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | if ($this->echoCLI && $currentExport > 0) { |
||
| 202 | echo 'Exported '.$currentExport.' of '.$totalFound.' nzbs for group: '.$group['name'].PHP_EOL; |
||
| 203 | } |
||
| 204 | $exported += $currentExport; |
||
| 205 | } |
||
| 206 | if ($exported > 0) { |
||
| 207 | $this->echoOut('Exported total of '.$exported.' NZB files to '.$path); |
||
| 208 | } |
||
| 209 | |||
| 210 | return $this->returnValue(); |
||
| 211 | } |
||
| 266 |