Conditions | 3 |
Paths | 4 |
Total Lines | 55 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
76 | * |
||
77 | * @param string $filename - Name for the file to be sent to the client |
||
78 | * $filename will be what is sent in the content-disposition header |
||
79 | * @throws InvalidParameterException |
||
80 | * @internal param array - See the documentation for the List Objects API for valid parameters. |
||
81 | * Only `Bucket` is required. |
||
82 | * |
||
83 | * http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html |
||
84 | */ |
||
85 | public function send($filename) |
||
86 | { |
||
87 | $params = $this->params->getParams(); |
||
88 | |||
89 | $this->doesDirectoryExist($params); |
||
90 | |||
91 | $zip = new ZipStream($filename); |
||
92 | // The iterator fetches ALL of the objects without having to manually loop over responses. |
||
93 | $files = $this->s3Client->getIterator('ListObjects', $params); |
||
94 | |||
95 | $parts = $this->parts(); |
||
96 | |||
97 | // Add each object from the ListObjects call to the new zip file. |
||
98 | foreach ($parts[$this->part] as $file) { |
||
99 | var_dump($file['Size']); |
||
100 | // Get the file name on S3 so we can save it to the zip file using the same name. |
||
101 | $fileName = basename($file['Key']); |
||
102 | |||
103 | if (is_file("s3://{$params['Bucket']}/{$file['Key']}")) { |
||
104 | $context = stream_context_create([ |
||
105 | 's3' => ['seekable' => true] |
||
106 | ]); |
||
107 | // open seekable(!) stream |
||
108 | if ($stream = fopen("s3://{$params['Bucket']}/{$file['Key']}", 'r', false, $context)) { |
||
109 | $zip->addFileFromStream($fileName, $stream); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | // Finalize the zip file. |
||
115 | $zip->finish(); |
||
116 | } |
||
117 | |||
118 | public function parts() |
||
119 | { |
||
120 | $params = $this->params->getParams(); |
||
121 | |||
122 | $this->doesDirectoryExist($params); |
||
123 | |||
124 | $files = $this->s3Client->getIterator('ListObjects', $params); |
||
125 | |||
126 | $parts = [0 => []]; |
||
127 | $partSizes = [0 => 0]; |
||
128 | $currentPart = 0; |
||
129 | foreach ($files as $file) { |
||
130 | if ($partSizes[$currentPart] + $file['Size'] > self::MAX_ARCHIVE_SIZE) { |
||
131 | $currentPart++; |
||
173 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.