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