Conditions | 16 |
Paths | 579 |
Total Lines | 121 |
Code Lines | 82 |
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 |
||
78 | function process() { |
||
79 | if (!$this->valid_cfg){ |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | global $order, $insert_id; |
||
84 | |||
85 | $OSCOM_Db = Registry::get('Db'); |
||
86 | |||
87 | $orderId = $insert_id; // just to make it obvious. |
||
88 | |||
89 | $debug_email = ''; |
||
90 | |||
91 | if ($this->debug){ |
||
92 | $debug_email .= '------------[New Order ' . $orderId . ']-----------------' . "\n" . |
||
93 | '$order =' . "\n" . |
||
94 | print_r($order, true) . |
||
95 | '$_COOKIE =' . "\n" . |
||
96 | print_r($_COOKIE, true); |
||
97 | } |
||
98 | |||
99 | if (!isset($_COOKIE['mailchimp_campaign_id']) || !isset($_COOKIE['mailchimp_email_id'])){ |
||
100 | return; |
||
101 | } |
||
102 | |||
103 | if ($this->debug){ |
||
104 | $debug_email .= date('Y-m-d H:i:s') . ' current ids:' . "\n" . |
||
105 | date('Y-m-d H:i:s') . ' eid =' . $_COOKIE['mailchimp_email_id'] . "\n" . |
||
106 | date('Y-m-d H:i:s') . ' cid =' . $_COOKIE['mailchimp_campaign_id'] . "\n"; |
||
107 | } |
||
108 | |||
109 | $Qorder = $OSCOM_Db->get('orders', 'orders_id', ['customers_id' => $_SESSION['customer_id']], 'date_purchased desc', 1); |
||
110 | |||
111 | $totals_array = array(); |
||
112 | $Qtotals = $OSCOM_Db->get('orders_total', ['value', 'class'], ['orders_id' => $Qorder->valueInt('orders_id')]); |
||
113 | while ($Qtotals->fetch()) { |
||
114 | $totals_array[$Qtotals->value('class')] = $Qtotals->value('value'); |
||
115 | } |
||
116 | |||
117 | $products_array = array(); |
||
118 | $Qproducts = $OSCOM_Db->get('orders_products', ['products_id', 'products_model', 'products_name', 'products_tax', 'products_quantity', 'final_price'], ['orders_id' => $Qorder->valueInt('orders_id')]); |
||
119 | while ($Qproducts->fetch()) { |
||
120 | $products_array[] = array('id' => $Qproducts->valueInt('products_id'), |
||
121 | 'name' => $Qproducts->value('products_name'), |
||
122 | 'model' => $Qproducts->value('products_model'), |
||
123 | 'qty' => $Qproducts->value('products_quantity'), |
||
124 | 'final_price' => $Qproducts->value('final_price'), |
||
125 | ); |
||
126 | $totals_array['ot_tax'] += $Qproducts->value('product_tax'); |
||
127 | } |
||
128 | |||
129 | $mcorder = array( |
||
130 | 'id' => $Qorder->valueInt('orders_id'), |
||
131 | 'total'=>$totals_array['ot_total'], |
||
132 | 'shipping'=>$totals_array['ot_shipping'], |
||
133 | 'tax' =>$totals_array['ot_tax'], |
||
134 | 'items'=>array(), |
||
135 | 'store_id'=>$this->store_id, |
||
136 | 'store_name' => $_SERVER['SERVER_NAME'], |
||
137 | 'campaign_id'=>$_COOKIE['mailchimp_campaign_id'], |
||
138 | 'email_id'=>$_COOKIE['mailchimp_email_id'], |
||
139 | 'plugin_id'=>1216 |
||
140 | ); |
||
141 | |||
142 | foreach($products_array as $product){ |
||
143 | $item = array(); |
||
144 | $item['line_num'] = $line; |
||
145 | $item['product_id'] = $product['id']; |
||
146 | $item['product_name'] = $product['name']; |
||
147 | $item['sku'] = $product['model']; |
||
148 | $item['qty'] = $product['qty']; |
||
149 | $item['cost'] = $product['final_price']; |
||
150 | |||
151 | //All this to get a silly category name from here |
||
152 | $Qcat = $OSCOM_Db->get('products_to_categories', 'categories_id', ['products_id' => $product['id']], null, 1); |
||
153 | |||
154 | $cat_id = $Qcat->valueInt('categories_id'); |
||
155 | |||
156 | $item['category_id'] = $cat_id; |
||
157 | $cat_name == ''; |
||
158 | $continue = true; |
||
159 | while($continue){ |
||
160 | //now recurse up the categories tree... |
||
161 | $Qcat = $OSCOM_Db->prepare('select c.categories_id, c.parent_id, cd.categories_name from :table_categories c inner join :table_categories_description cd on c.categories_id = cd.categories_id where c.categories_id = :categories_id'); |
||
162 | $Qcat->bindInt(':categories_id', $cat_id); |
||
163 | $Qcat->execute(); |
||
164 | |||
165 | if ($cat_name == ''){ |
||
166 | $cat_name = $Qcat->value('categories_name'); |
||
167 | } else { |
||
168 | $cat_name = $Qcat->value('categories_name') .' - '.$cat_name; |
||
169 | } |
||
170 | $cat_id = $Qcat->valueInt('parent_id'); |
||
171 | if ($cat_id==0){ |
||
172 | $continue = false; |
||
173 | } |
||
174 | } |
||
175 | $item['category_name'] = $cat_name; |
||
176 | |||
177 | $mcorder['items'][] = $item; |
||
178 | } |
||
179 | |||
180 | $GLOBALS["mc_api_key"] = $this->apikey; |
||
181 | $api = new MCAPI('notused','notused'); |
||
182 | $res = $api->campaignEcommAddOrder($mcorder); |
||
183 | if ($api->errorMessage!=''){ |
||
184 | if ($this->debug) { |
||
185 | $debug_email .= 'Error:' . "\n" . |
||
186 | $api->errorMessage . "\n"; |
||
187 | } |
||
188 | } else { |
||
189 | //nothing |
||
190 | } |
||
191 | // send!() |
||
192 | |||
193 | if ($this->debug && !empty($debug_email)) { |
||
194 | $debugEmail = new Mail(MODULE_HEADER_TAGS_MAILCHIMP_360_DEBUG_EMAIL, null, STORE_OWNER_EMAIL_ADDRESS, STORE_OWNER, 'MailChimp Debug E-Mail'); |
||
195 | $debugEmail->setBody($debug_email); |
||
196 | $debugEmail->send(); |
||
197 | } |
||
198 | }//update |
||
199 | }//mc360 class |
||
202 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: