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) |
||
89 | { |
||
90 | $params = $this->params->getParams(); |
||
91 | |||
92 | $this->doesDirectoryExist($params); |
||
93 | |||
94 | $zip = new ZipStream($filename); |
||
95 | |||
96 | $parts = $this->parts(); |
||
97 | |||
98 | // Add each object from the ListObjects call to the new zip file. |
||
99 | foreach ($parts[$this->part] as $file) { |
||
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 | // The iterator fetches ALL of the objects without having to manually loop over responses. |
||
125 | $files = $this->s3Client->getIterator('ListObjects', $params); |
||
126 | |||
127 | $parts = [0 => []]; |
||
128 | $partSizes = [0 => 0]; |
||
129 | $currentPart = 0; |
||
130 | foreach ($files as $file) { |
||
131 | if ($partSizes[$currentPart] + $file['Size'] > self::MAX_ARCHIVE_SIZE) { |
||
132 | $currentPart++; |
||
133 | $parts[$currentPart] = []; |
||
134 | $partSizes[$currentPart] = 0; |
||
135 | } |
||
136 | $parts[$currentPart][] = $file; |
||
137 | $partSizes[$currentPart] += $file['Size']; |
||
138 | } |
||
139 | |||
140 | return $parts; |
||
174 |