Files
w4rpservices/app/Jobs/Commands/MiningTaxes/FetchMiningTaxesObserversJob.php
T

126 lines
3.6 KiB
PHP

<?php
namespace App\Jobs\Commands\MiningTaxes;
//Internal Library
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
//App Library
use Seat\Eseye\Exceptions\RequestFailedException;
use App\Library\Esi\Esi;
use App\Library\Lookups\LookupHelper;
use App\Library\Structures\StructureHelper;
//App Models
use App\Models\MiningTax\Observer;
class FetchMiningTaxesObserversJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 3600;
/**
* Number of job retries
*
* @var int
*/
public $tries = 3;
/**
* Job Variables
*/
private $charId;
private $corpId;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($charId, $corpId)
{
$this->charId = $charId;
$this->corpId = $corpId;
//Set the connection for the job
$this->connection = 'redis';
}
/**
* Execute the job.
* The job's duty is to get all of the corporation's moon mining observers,
* then store them in the database.
*
* @return void
*/
public function handle()
{
//Declare variables
$sHelper = new StructureHelper($this->charId, $this->corpId);
$lookup = new LookupHelper;
$esiHelper = new Esi;
//Get the configuration from the main site
$config = config('esi');
//Check for the esi scope
if(!$esiHelper->HaveEsiScope($this->charId, 'esi-industry.read_corporation_mining.v1') || !$esiHelper->HaveEsiScope($this->charId, 'esi-universe.read_structures.v1')) {
Log::critical('Esi scopes were not found for FetchMiningTaxesObserversJob.');
return;
}
//Get the refresh token for the character
$refreshToken = $esiHelper->GetRefreshToken($this->charId);
//Get the esi variable
$esi = $esiHelper->SetupEsiAuthentication($refreshToken);
try {
$response = $esi->invoke('get', '/corporations/{corporation_id}/mining/observers', [
'corporation_id' => $this->corpId,
]);
} catch(RequestFailedException $e) {
Log::critical("Failed to get moon observers in FetchMiningTaxesObservers");
}
//Run through the mining observers, and add them to the database
foreach($response as $observer) {
//Count how many observers have the observer_id stated.
//May change this to a more efficient function later
$count = Observer::where([
'observer_id' => $observer->observer_id,
])->count();
if($count == 0) {
$obs = new Observer;
$obs->last_updated = $observer->last_updated;
$obs->observer_id = $observer->observer_id;
$obs->observer_type = $observer->observer_type;
$obs->save();
} else {
Observer::where([
'observer_id' => $observer->observer_id,
])->update([
'last_updated' => $observer->last_updated,
]);
}
}
/**
* Cleanup stale data that hasn't been updated in at least 1 week.
*/
$date = Carbon::now()->subDay(7);
Observer::where('updated_at', '<', $date)->delete();
}
}