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