Conditions | 9 |
Paths | 76 |
Total Lines | 74 |
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 |
||
94 | public function doGet($request) |
||
95 | { |
||
96 | self::$logger->debug('>>doGet(request=['.var_export($request, true).'])'); |
||
97 | |||
98 | $params = $request->getParams(); |
||
99 | |||
100 | $body = ''; |
||
101 | |||
102 | try { |
||
103 | if (isset($params['ActiveRecordType'])) { |
||
104 | $ActiveRecordType = $params['ActiveRecordType']; |
||
105 | |||
106 | $className = "Alpha\\Model\\$ActiveRecordType"; |
||
107 | if (class_exists($className)) { |
||
108 | $this->record = new $className(); |
||
109 | } else { |
||
110 | throw new IllegalArguementException('No ActiveRecord available to render!'); |
||
111 | } |
||
112 | |||
113 | // the name of the file download |
||
114 | if (isset($params['ActiveRecordID'])) { |
||
115 | $fileName = $this->record->getTableName().'-'.$params['ActiveRecordID']; |
||
116 | } else { |
||
117 | $fileName = $this->record->getTableName(); |
||
118 | } |
||
119 | |||
120 | $response = new Response(200); |
||
121 | |||
122 | // header info for browser |
||
123 | $response->setHeader('Content-Type', 'application/vnd.ms-excel'); |
||
124 | $response->setHeader('Content-Disposition', 'attachment; filename='.$fileName.'.xls'); |
||
125 | $response->setHeader('Pragma', 'no-cache'); |
||
126 | $response->setHeader('Expires', '0'); |
||
127 | |||
128 | // handle a single record |
||
129 | if (isset($params['ActiveRecordID'])) { |
||
130 | $this->record->load($params['ActiveRecordID']); |
||
131 | ActiveRecord::disconnect(); |
||
132 | |||
133 | $convertor = new ActiveRecord2Excel($this->record); |
||
134 | $body .= $convertor->render(); |
||
135 | } else { |
||
136 | // handle all records of this type |
||
137 | $records = $this->record->loadAll(); |
||
138 | ActiveRecord::disconnect(); |
||
139 | |||
140 | $first = true; |
||
141 | |||
142 | foreach ($records as $record) { |
||
143 | $convertor = new ActiveRecord2Excel($record); |
||
144 | if ($first) { |
||
145 | $body .= $convertor->render(true); |
||
146 | $first = false; |
||
147 | } else { |
||
148 | $body .= $convertor->render(false); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | } else { |
||
153 | throw new IllegalArguementException('No ActiveRecordType parameter available for ViewExcel controller!'); |
||
154 | } |
||
155 | } catch (RecordNotFoundException $e) { |
||
156 | self::$logger->error($e->getMessage()); |
||
157 | throw new ResourceNotFoundException($e->getMessage()); |
||
158 | } catch (IllegalArguementException $e) { |
||
159 | self::$logger->error($e->getMessage()); |
||
160 | throw new ResourceNotFoundException($e->getMessage()); |
||
161 | } |
||
162 | |||
163 | self::$logger->debug('<<__doGet'); |
||
164 | $response->setBody($body); |
||
165 | |||
166 | return $response; |
||
167 | } |
||
168 | } |
||
169 |