Conditions | 15 |
Paths | 126 |
Total Lines | 90 |
Code Lines | 57 |
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 downloadRemoteDataStore() |
||
75 | { |
||
76 | $connector = HttpFactory::getHttp(); |
||
77 | $package = $this->getRemoteDateStoreDownloadUrl(); |
||
78 | |||
79 | try { |
||
80 | $data = $connector->get($package); |
||
81 | } catch (\Exception $exception) { |
||
82 | $this->logger->error('Failed to fetch remote IP data: '.$exception->getMessage()); |
||
83 | } |
||
84 | |||
85 | $tempTarget = $this->cacheDir.'/'.basename($package); |
||
86 | $tempExt = strtolower(pathinfo($package, PATHINFO_EXTENSION)); |
||
|
|||
87 | $localTarget = $this->getLocalDataStoreFilepath(); |
||
88 | $localTargetExt = strtolower(pathinfo($localTarget, PATHINFO_EXTENSION)); |
||
89 | |||
90 | try { |
||
91 | $success = false; |
||
92 | |||
93 | switch (true) { |
||
94 | case $localTargetExt === $tempExt: |
||
95 | $success = (bool) file_put_contents($localTarget, $data->body); |
||
96 | |||
97 | break; |
||
98 | |||
99 | case $this->endsWith($package, 'tar.gz'): |
||
100 | /** |
||
101 | * If tar.gz it loops whole folder structure and copy the file which has the same basename as |
||
102 | * desired localTarget. |
||
103 | */ |
||
104 | $tempTargetFolder = $this->cacheDir.'/'.self::TAR_CACHE_FOLDER; |
||
105 | $temporaryPhar = $tempTargetFolder.'/'.self::TAR_TEMP_FILE; |
||
106 | if (!is_dir($tempTargetFolder)) { |
||
107 | // dir doesn't exist, make it |
||
108 | mkdir($tempTargetFolder); |
||
109 | } |
||
110 | file_put_contents($temporaryPhar, $data->body); |
||
111 | $pharData = new PharData($temporaryPhar); |
||
112 | foreach (new RecursiveIteratorIterator($pharData) as $file) { |
||
113 | /** @var PharFileInfo $file */ |
||
114 | if ($file->getBasename() === basename($localTarget)) { |
||
115 | $success = copy($file->getPathname(), $localTarget); |
||
116 | } |
||
117 | } |
||
118 | @unlink($temporaryPhar); |
||
119 | |||
120 | break; |
||
121 | |||
122 | case 'gz' == $tempExt: |
||
123 | $memLimit = $this->sizeInByte(ini_get('memory_limit')); |
||
124 | $freeMem = $memLimit - memory_get_peak_usage(); |
||
125 | //check whether there is enough memory to handle large iplookp DB |
||
126 | // or will throw iplookup exception |
||
127 | if (function_exists('gzdecode') && strlen($data->body) < ($freeMem / 3)) { |
||
128 | $success = (bool) file_put_contents($localTarget, gzdecode($data->body)); |
||
129 | } elseif (function_exists('gzopen')) { |
||
130 | if (file_put_contents($tempTarget, $data->body)) { |
||
131 | $bufferSize = 4096; // read 4kb at a time |
||
132 | $file = gzopen($tempTarget, 'rb'); |
||
133 | $outFile = fopen($localTarget, 'wb'); |
||
134 | while (!gzeof($file)) { |
||
135 | fwrite($outFile, gzread($file, $bufferSize)); |
||
136 | } |
||
137 | fclose($outFile); |
||
138 | gzclose($file); |
||
139 | @unlink($tempTarget); |
||
140 | $success = true; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | break; |
||
145 | |||
146 | case 'zip' == $tempExt: |
||
147 | file_put_contents($tempTarget, $data->body); |
||
148 | |||
149 | $zipper = new \ZipArchive(); |
||
150 | |||
151 | $zipper->open($tempTarget); |
||
152 | $success = $zipper->extractTo($localTarget); |
||
153 | $zipper->close(); |
||
154 | @unlink($tempTarget); |
||
155 | break; |
||
156 | } |
||
157 | } catch (\Exception $exception) { |
||
158 | error_log($exception); |
||
159 | |||
160 | $success = false; |
||
161 | } |
||
162 | |||
163 | return $success; |
||
164 | } |
||
216 |