Conditions | 12 |
Paths | 78 |
Total Lines | 73 |
Code Lines | 43 |
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 |
||
69 | public function downloadMapZipAndCover(array $p_URLs, string $targetDir): ResponseDownload |
||
70 | { |
||
71 | $response = new ResponseDownload(); |
||
72 | |||
73 | $anyError = []; |
||
74 | $allFailed = null; |
||
75 | |||
76 | foreach ($p_URLs as $hash => $l_URLs) { |
||
77 | echo $hash . ": "; |
||
78 | |||
79 | $error = false; |
||
80 | |||
81 | foreach ($l_URLs as $type => $l_URL) { |
||
82 | echo $type . ": "; |
||
83 | |||
84 | $extension = $type === "map" ? '.zip' : '.jpg'; |
||
85 | |||
86 | if (!file_exists($targetDir)) { |
||
87 | mkdir($targetDir, 0777, true); |
||
88 | } |
||
89 | |||
90 | if(substr($targetDir, -1) !== "/") |
||
91 | $targetDir .= "/"; |
||
92 | |||
93 | //The path & filename to save to. |
||
94 | $saveTo = $targetDir . $hash . $extension; |
||
95 | |||
96 | $ch = curl_init($l_URL); |
||
97 | curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent); |
||
98 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
99 | curl_setopt($ch, CURLOPT_ENCODING, ""); |
||
100 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); |
||
101 | |||
102 | $result = curl_exec($ch); |
||
103 | |||
104 | if(curl_errno($ch) === 0) { |
||
105 | $allFailed = false; |
||
106 | echo "Ok "; |
||
107 | } else { |
||
108 | $anyError[] = $hash; |
||
109 | $error = true; |
||
110 | if(is_null($allFailed)) $allFailed = true; |
||
111 | echo "Error "; |
||
112 | } |
||
113 | |||
114 | file_put_contents($saveTo, $result); |
||
115 | |||
116 | curl_close($ch); |
||
117 | } |
||
118 | |||
119 | echo "save: " . ($error ? "No" : "Yes") . "\n"; |
||
120 | } |
||
121 | |||
122 | $status = ""; |
||
123 | |||
124 | if($allFailed) { |
||
125 | $status = "All failed"; |
||
126 | $response->setErrorStatus(true)->setErrorMessage("Can't download all/some maps"); |
||
127 | } else { |
||
128 | if(count($anyError) !== 0) { |
||
129 | $response->setErrorStatus(true)->setErrorMessage("Can't download all/some maps"); |
||
130 | |||
131 | foreach ($anyError as $hash) { |
||
132 | $status .= $hash . ", "; |
||
133 | } |
||
134 | |||
135 | $status .= "Failed"; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | $response->setDownloadStatus($status); |
||
140 | |||
141 | return $response; |
||
142 | } |
||
157 | } |