/** * Run the Update * * @return mixed|void */ public function call() { $pheal = $this->setScope('corp')->setCorporationID()->getPheal(); // Looking at the AssetsList update, we see that // the table is cleaned up beforehand as the // itemID's may change for assets. For this call // to the Locations endpoint, we will also have // to cleanup as we will be referencing the same // itemID's LocationsModel::where('corporationID', $this->corporationID)->delete(); // We get an array of items ID's that is keyed // by the itemID's so that we can later use this // same array to lookup the locationID for the // nearest_celestial lookup. $item_ids = AssetListModel::where('corporationID', $this->corporationID)->get()->keyBy('itemID')->toArray(); // Chunk the requests to the API as the ids field // could get too long with all of the bigInts inside // of the query string. foreach (array_chunk($item_ids, 100) as $items) { // Apply array_column so that we only pass the // itemID's in the query string to the API $result = $pheal->Locations(['ids' => implode(',', array_column($items, 'itemID'))]); foreach ($result->locations as $location) { $nearest_celestial = $this->find_nearest_celestial($item_ids[$location->itemID]['locationID'], $location->x, $location->y, $location->z); LocationsModel::create(['corporationID' => $this->corporationID, 'itemID' => $location->itemID, 'itemName' => $location->itemName, 'x' => $location->x, 'y' => $location->y, 'z' => $location->z, 'mapID' => $nearest_celestial['mapID'], 'mapName' => $nearest_celestial['mapName']]); } // Foreach Location } return; }
/** * Run the Update * * @return mixed|void */ public function call() { $pheal = $this->setScope('corp')->setCorporationID()->getPheal(); $result = $pheal->AssetList(); // The caveat of this API call as can be seen here [1] is // that the itemID's may change for a number of reasons. // Due to this we need to clear out the assets that we // have for this character and repopulate them. // // [1] https://neweden-dev.com/Corporation/Asset_List AssetListModel::where('corporationID', $this->corporationID)->delete(); AssetListContents::where('corporationID', $this->corporationID)->delete(); // We take the resuls and chunk it up into parts of 1000 // entries. For every 1000 entries we bulk insert the // assets and the asset contents into the database. foreach (array_chunk((array) $result->assets, 1000) as $asset_chunk) { // Take the chunked array and map the fields to our // asset_list variable. This variable is filtered // for empty value so that we can prevent a case // where a bulk insert fail because of an empty // data array variable. $asset_list = array_filter(array_map(function ($entry) { return ['corporationID' => $this->corporationID, 'itemID' => $entry->itemID, 'locationID' => $entry->locationID, 'typeID' => $entry->typeID, 'quantity' => $entry->quantity, 'flag' => $entry->flag, 'singleton' => $entry->singleton, 'rawQuantity' => isset($entry->rawQuantity) ? $entry->rawQuantity : 0, 'created_at' => Carbon::now()->toDateTimeString(), 'updated_at' => Carbon::now()->toDateTimeString()]; }, $asset_chunk)); // If there were any assets derived form the array_map // then we can bulk insert it into the table. if (count($asset_list) > 0) { AssetListModel::insert($asset_list); } // Next we process the assets contents for this chunk // of assets data. We iterate over each of the assets // to check if there is some contents that we can add // to the database. foreach ($asset_chunk as $asset) { if (isset($asset->contents)) { $this->add_asset_content($asset->contents, $this->corporationID, $asset->itemID); } } // End foreach $asset_chunk } // End array_chunk return; }