Conditions | 9 |
Paths | 17 |
Total Lines | 62 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | 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 |
||
119 | public function ajax_rate() { |
||
120 | |||
121 | $id = $this->input->post('cid'); |
||
122 | $type = $this->input->post('type'); |
||
123 | $rating = (int) $this->input->post('val'); |
||
124 | |||
125 | if ($id != null && $type != null && !$this->session->userdata('voted_g' . $id . $type) == true) { |
||
126 | //Check if rating exists |
||
127 | $check = $this->rating_model->get_rating($id, $type); |
||
128 | if ($check != null) { |
||
129 | $this->new_votes = $check->votes + 1; |
||
130 | $this->new_rating = $check->rating + $rating; |
||
131 | $data = [ |
||
132 | 'votes' => $this->new_votes, |
||
133 | 'rating' => $this->new_rating |
||
134 | ]; |
||
135 | $rating_res = $this->new_rating / $this->new_votes * 20; |
||
136 | $votes_res = $this->new_votes; |
||
137 | $this->rating_model->update_rating($id, $type, $data); |
||
138 | } else { |
||
139 | $data = [ |
||
140 | 'id_type' => $id, |
||
141 | 'type' => $type, |
||
142 | 'votes' => 1, |
||
143 | 'rating' => $rating |
||
144 | ]; |
||
145 | $votes_res = 1; |
||
146 | $rating_res = $rating * 20; |
||
147 | $this->rating_model->insert_rating($data); |
||
148 | } |
||
149 | //Change rating for product |
||
150 | if ($type == 'product') { |
||
151 | if (SProductsQuery::create()->findPk($id) !== null) { |
||
152 | $model = SProductsRatingQuery::create()->findPk($id); |
||
153 | if ($model === null) { |
||
154 | $model = new SProductsRating; |
||
155 | $model->setProductId($id); |
||
156 | } |
||
157 | $rating_res = (($model->getRating() + $rating) / ($model->getVotes() + 1)) * 20; |
||
158 | $votes_res = $model->getVotes() + 1; |
||
159 | |||
160 | $model->setVotes($model->getVotes() + 1); |
||
161 | $model->setRating($model->getRating() + $rating); |
||
162 | $model->save(); |
||
163 | } |
||
164 | } |
||
165 | //Save in session user's info |
||
166 | $this->session->set_userdata('voted_g' . $id . $type, true); |
||
167 | |||
168 | //If ajax request than return data for with new rating and votes |
||
169 | if ($this->input->is_ajax_request()) { |
||
170 | return json_encode( |
||
171 | [ |
||
172 | 'rate' => "$rating_res", |
||
173 | 'votes' => "$votes_res" |
||
174 | ] |
||
175 | ); |
||
176 | } |
||
177 | } else { |
||
178 | return json_encode(['rate' => null]); |
||
179 | } |
||
180 | } |
||
181 | |||
230 | } |