| Conditions | 17 |
| Paths | 148 |
| Total Lines | 84 |
| Code Lines | 53 |
| 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 |
||
| 54 | public function run($request) |
||
| 55 | { |
||
| 56 | // Definitions |
||
| 57 | $default_source_id = Defaults::config()->source_id; |
||
| 58 | $query = []; |
||
| 59 | $consumer = Consumer::get()->find('Title', 'OrderUpdate'); // to get modifiedSince |
||
| 60 | |||
| 61 | if ($consumer) { |
||
|
|
|||
| 62 | $date = new DateTime($consumer->ExternalLastEdited); |
||
| 63 | $query['modifiedSince'] = substr($date->format('Y-m-d\TH:i:s.u'), 0, 23); |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($default_source_id) { |
||
| 67 | $query['sourceId'] = $default_source_id; |
||
| 68 | } |
||
| 69 | |||
| 70 | $response = UnleashedAPI::sendCall( |
||
| 71 | 'GET', |
||
| 72 | 'https://api.unleashedsoftware.com/SalesOrders', |
||
| 73 | ['query' => $query] |
||
| 74 | ); |
||
| 75 | |||
| 76 | if ($response->getStatusCode() == '200') { |
||
| 77 | $apidata_array = json_decode($response->getBody()->getContents(), true); |
||
| 78 | $apidata = $apidata_array['Items']; |
||
| 79 | $pagination = $apidata_array['Pagination']; |
||
| 80 | $numberofpages = (int) $pagination['NumberOfPages']; |
||
| 81 | |||
| 82 | if ($numberofpages > 1) { |
||
| 83 | for ($i = 2; $i <= $numberofpages; $i++) { |
||
| 84 | $response = UnleashedAPI::sendCall( |
||
| 85 | 'GET', |
||
| 86 | 'https://api.unleashedsoftware.com/SalesOrders/' . $i, |
||
| 87 | ['query' => $query] |
||
| 88 | ); |
||
| 89 | if ($response->getStatusCode() == '200') { |
||
| 90 | $apidata_array = json_decode($response->getBody()->getContents(), true); |
||
| 91 | $apidata = array_merge($apidata, $apidata_array['Items']); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $this->log('<h3>Update the OrderStatus in Silvershop</h3>'); |
||
| 97 | $loader = OrderBulkLoader::create('SilverShop\Model\Order'); |
||
| 98 | $loader->transforms = [ |
||
| 99 | 'Status' => [ |
||
| 100 | 'callback' => function ($value) { |
||
| 101 | // convert from Unleashed Sales Order status to Silvershop |
||
| 102 | return $this->order_status_map[$value]; |
||
| 103 | } |
||
| 104 | ] |
||
| 105 | ]; |
||
| 106 | $results = $loader->updateRecords($apidata, $this->preview); |
||
| 107 | |||
| 108 | if ($results->UpdatedCount()) { |
||
| 109 | $this->log(Debug::text($results->getData())); |
||
| 110 | } |
||
| 111 | $this->log("Done"); |
||
| 112 | |||
| 113 | // Send email |
||
| 114 | if ($results->Count() && $this->email_subject && !$this->preview && Email::config()->admin_email) { |
||
| 115 | $data = $results->getData(); |
||
| 116 | $email = Email::create( |
||
| 117 | ShopConfigExtension::config()->email_from ? ShopConfigExtension::config()->email_from : Email::config()->admin_email, |
||
| 118 | Email::config()->admin_email, |
||
| 119 | $this->email_subject, |
||
| 120 | Debug::text($data) |
||
| 121 | ); |
||
| 122 | $dispatched = $email->send(); |
||
| 123 | if ($dispatched) { |
||
| 124 | $this->log("Email sent"); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | // Create/update Consumer |
||
| 129 | if (!$this->preview && $apidata) { |
||
| 130 | if (!$consumer) { |
||
| 131 | $consumer = Consumer::create([ |
||
| 132 | 'Title' => 'OrderUpdate', |
||
| 133 | 'ExternalLastEditedKey' => 'LastModifiedOn' |
||
| 134 | ]); |
||
| 135 | } |
||
| 136 | $consumer->setMaxExternalLastEdited($apidata); |
||
| 137 | $consumer->write(); |
||
| 138 | } |
||
| 142 |