| Conditions | 23 |
| Paths | 96 |
| Total Lines | 163 |
| Code Lines | 112 |
| 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 |
||
| 39 | public function run($request) |
||
| 40 | { |
||
| 41 | // Definitions |
||
| 42 | $silvershopInternalItemIDMustBeUnique = Product::get()->column('InternalItemID'); |
||
| 43 | $silvershopTitleMustBeUnique = Product::get()->column('Title'); |
||
| 44 | $consumer = Consumer::get()->find('Title', 'ProductUpdate'); |
||
| 45 | |||
| 46 | // Get Products from Unleashed |
||
| 47 | if (!$consumer) { |
||
|
|
|||
| 48 | $response = UnleashedAPI::sendCall( |
||
| 49 | 'GET', |
||
| 50 | 'https://api.unleashedsoftware.com/Products' |
||
| 51 | ); |
||
| 52 | |||
| 53 | $apidata_array = json_decode($response->getBody()->getContents(), true); |
||
| 54 | $apidata = $apidata_array['Items']; |
||
| 55 | $pagination = $apidata_array['Pagination']; |
||
| 56 | $numberofpages = (int) $pagination['NumberOfPages']; |
||
| 57 | |||
| 58 | if ($numberofpages > 1) { |
||
| 59 | for ($i = 2; $i <= $numberofpages; $i++) { |
||
| 60 | $response = UnleashedAPI::sendCall( |
||
| 61 | 'GET', |
||
| 62 | 'https://api.unleashedsoftware.com/Products/' . $i |
||
| 63 | ); |
||
| 64 | $apidata_array = json_decode($response->getBody()->getContents(), true); |
||
| 65 | $apidata = array_merge($apidata, $apidata_array['Items']); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | $query = []; |
||
| 70 | $date = new DateTime($consumer->ExternalLastEdited); |
||
| 71 | $date->setTimezone(new DateTimeZone("UTC")); // required for Unleashed Products (not for other endpoints) |
||
| 72 | $query['modifiedSince'] = substr($date->format('Y-m-d\TH:i:s.u'), 0, 23); |
||
| 73 | |||
| 74 | $response = UnleashedAPI::sendCall( |
||
| 75 | 'GET', |
||
| 76 | 'https://api.unleashedsoftware.com/Products', |
||
| 77 | ['query' => $query] |
||
| 78 | ); |
||
| 79 | |||
| 80 | $apidata_array = json_decode($response->getBody()->getContents(), true); |
||
| 81 | $apidata = $apidata_array['Items']; |
||
| 82 | $pagination = $apidata_array['Pagination']; |
||
| 83 | $numberofpages = (int) $pagination['NumberOfPages']; |
||
| 84 | |||
| 85 | if ($numberofpages > 1) { |
||
| 86 | for ($i = 2; $i <= $numberofpages; $i++) { |
||
| 87 | $response = UnleashedAPI::sendCall( |
||
| 88 | 'GET', |
||
| 89 | 'https://api.unleashedsoftware.com/Products/' . $i, |
||
| 90 | ['query' => $query] |
||
| 91 | ); |
||
| 92 | $apidata_array = json_decode($response->getBody()->getContents(), true); |
||
| 93 | $apidata = array_merge($apidata, $apidata_array['Items']); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | $this->log('<h2>Preliminary Checks</h2>'); |
||
| 99 | // Check for duplicates in DataList before proceeding further |
||
| 100 | $duplicates = Utilities::getDuplicates($silvershopInternalItemIDMustBeUnique); |
||
| 101 | if (!empty($duplicates)) { |
||
| 102 | echo "<h2>Duplicate check of Product InternalItemID within Silvershop</h2>"; |
||
| 103 | foreach ($duplicates as $duplicate) { |
||
| 104 | $this->log($duplicate); |
||
| 105 | } |
||
| 106 | $this->log('Please remove duplicates from Silvershop before running this Build Task'); |
||
| 107 | $this->log('Exit'); |
||
| 108 | die(); |
||
| 109 | } else { |
||
| 110 | $this->log('No duplicate found'); |
||
| 111 | } |
||
| 112 | |||
| 113 | $duplicates = Utilities::getDuplicates($silvershopTitleMustBeUnique); |
||
| 114 | if (!empty($duplicates)) { |
||
| 115 | echo "<h2>Duplicate check of Product Titles within Silvershop</h2>"; |
||
| 116 | foreach ($duplicates as $duplicate) { |
||
| 117 | $this->log($duplicate); |
||
| 118 | } |
||
| 119 | $this->log('Please remove duplicates from Silvershop before running this Build Task'); |
||
| 120 | $this->log('Exit'); |
||
| 121 | die(); |
||
| 122 | } else { |
||
| 123 | $this->log('No duplicate found'); |
||
| 124 | } |
||
| 125 | |||
| 126 | // Check for duplicates in apidata before proceeding further |
||
| 127 | $duplicates = Utilities::getDuplicates(array_column($apidata, 'ProductCode')); |
||
| 128 | if (!empty($duplicates)) { |
||
| 129 | echo "<h2>Duplicate check of ProductCode within Unleashed</h2>"; |
||
| 130 | foreach ($duplicates as $duplicate) { |
||
| 131 | $this->log(htmlspecialchars($duplicate, ENT_QUOTES, 'utf-8')); |
||
| 132 | } |
||
| 133 | $this->log( |
||
| 134 | 'Please remove duplicates from Unleashed before running this Build Task' |
||
| 135 | ); |
||
| 136 | $this->log('Exit'); |
||
| 137 | die(); |
||
| 138 | } else { |
||
| 139 | $this->log('No duplicate found'); |
||
| 140 | } |
||
| 141 | |||
| 142 | // Update |
||
| 143 | $this->log('<h3>Update Product records in Silvershop</h3>'); |
||
| 144 | $loader = ProductBulkLoader::create('SilverShop\Page\Product'); |
||
| 145 | $loader->transforms = [ |
||
| 146 | 'Parent' => [ |
||
| 147 | 'callback' => function ($value) { |
||
| 148 | $obj = ProductCategory::get()->find('Guid', $value['Guid']); |
||
| 149 | if ($obj) { |
||
| 150 | return $obj; |
||
| 151 | } else { |
||
| 152 | return ProductCategory::get()->find('Title', $value['GroupName']); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | ], |
||
| 156 | 'BasePrice' => [ |
||
| 157 | 'callback' => function ($value) { |
||
| 158 | return (float)$value; |
||
| 159 | } |
||
| 160 | ], |
||
| 161 | 'Title' => [ |
||
| 162 | 'callback' => function ($value, &$placeholder) { |
||
| 163 | $placeholder->URLSegment = Convert::raw2url($value); |
||
| 164 | return $value; |
||
| 165 | } |
||
| 166 | ] |
||
| 167 | ]; |
||
| 168 | $results = $loader->updateRecords($apidata, $this->preview); |
||
| 169 | |||
| 170 | if ($results->UpdatedCount()) { |
||
| 171 | $this->log(Debug::text($results->getData())); |
||
| 172 | } |
||
| 173 | $this->log("Done"); |
||
| 174 | Debug::show($results->Count()); |
||
| 175 | Debug::show(!$this->preview); |
||
| 176 | Debug::show(Email::config()->admin_email); |
||
| 177 | Debug::show($this->email_subject); |
||
| 178 | // Send email |
||
| 179 | if ($results->Count() && !$this->preview && Email::config()->admin_email && $this->email_subject) { |
||
| 180 | // send email |
||
| 181 | $email = Email::create( |
||
| 182 | ShopConfigExtension::config()->email_from ? ShopConfigExtension::config()->email_from : Email::config()->admin_email, |
||
| 183 | Email::config()->admin_email, |
||
| 184 | $this->email_subject, |
||
| 185 | Debug::text($results->getData()) |
||
| 186 | ); |
||
| 187 | $dispatched = $email->send(); |
||
| 188 | if ($dispatched) { |
||
| 189 | $this->log('Email sent'); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!$this->preview && $apidata) { |
||
| 194 | if (!$consumer) { |
||
| 195 | $consumer = Consumer::create([ |
||
| 196 | 'Title' => 'ProductUpdate', |
||
| 197 | 'ExternalLastEditedKey' => 'LastModifiedOn' |
||
| 198 | ]); |
||
| 199 | } |
||
| 200 | $consumer->setMaxExternalLastEdited($apidata); |
||
| 201 | $consumer->write(); |
||
| 202 | } |
||
| 205 |