Conditions | 3 |
Paths | 4 |
Total Lines | 52 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 12 |
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 |
||
88 | public function send($filename, $params = array()) |
||
89 | { |
||
90 | // Set default values for the optional $params argument |
||
91 | if(!isset($params['expiration'])) |
||
92 | $params['expiration'] = '+10 minutes'; |
||
93 | |||
94 | // Initialize the ZipStream object and pass in the file name which |
||
95 | // will be what is sent in the content-disposition header. |
||
96 | // This is the name of the file which will be sent to the client. |
||
97 | $zip = new ZipStream\ZipStream($filename); |
||
98 | |||
99 | // Get a list of objects from the S3 bucket. The iterator is a high |
||
100 | // level abstration that will fetch ALL of the objects without having |
||
101 | // to manually loop over responses. |
||
102 | $result = $this->s3Client->getIterator('ListObjects', $this->params); |
||
103 | |||
104 | // We loop over each object from the ListObjects call. |
||
105 | foreach($result as $file) |
||
106 | { |
||
107 | // We need to use a command to get a request for the S3 object |
||
108 | // and then we can get the presigned URL. |
||
109 | $command = $this->s3Client->getCommand('GetObject', array( |
||
110 | 'Bucket' => $this->params['Bucket'], |
||
111 | 'Key' => $file['Key'] |
||
112 | )); |
||
113 | $signedUrl = $command->createPresignedUrl($params['expiration']); |
||
114 | |||
115 | // Get the file name on S3 so we can save it to the zip file |
||
116 | // using the same name. |
||
117 | $fileName = basename($file['Key']); |
||
118 | |||
119 | // We want to fetch the file to a file pointer so we create it here |
||
120 | // and create a curl request and store the response into the file |
||
121 | // pointer. |
||
122 | // After we've fetched the file we add the file to the zip file using |
||
123 | // the file pointer and then we close the curl request and the file |
||
124 | // pointer. |
||
125 | // Closing the file pointer removes the file. |
||
126 | $fp = tmpfile(); |
||
127 | $ch = curl_init($signedUrl); |
||
128 | curl_setopt($ch, CURLOPT_TIMEOUT, 120); |
||
129 | curl_setopt($ch, CURLOPT_FILE, $fp); |
||
130 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||
131 | curl_exec($ch); |
||
132 | curl_close($ch); |
||
133 | $zip->addFileFromStream($fileName, $fp); |
||
134 | fclose($fp); |
||
135 | } |
||
136 | |||
137 | // Finalize the zip file. |
||
138 | $zip->finish(); |
||
139 | } |
||
140 | } |
||
141 |