firefly-iii/app/Jobs/GetSpectreProviders.php
2017-12-09 12:23:28 +01:00

78 lines
2.5 KiB
PHP

<?php
namespace FireflyIII\Jobs;
use FireflyIII\Models\Configuration;
use FireflyIII\Models\SpectreProvider;
use FireflyIII\Services\Spectre\Request\ListProvidersRequest;
use FireflyIII\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
class GetSpectreProviders implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
/**
* Create a new job instance.
*/
public function __construct(User $user)
{
$this->user = $user;
Log::debug('Constructed job GetSpectreProviders');
}
/**
* Execute the job.
*/
public function handle()
{
/** @var Configuration $configValue */
$configValue = app('fireflyconfig')->get('spectre_provider_download', 0);
$now = time();
if ($now - intval($configValue->data) < 86400) {
Log::debug(sprintf('Difference is %d, so will NOT execute job.', ($now - intval($configValue->data))));
return;
}
Log::debug(sprintf('Difference is %d, so will execute job.', ($now - intval($configValue->data))));
// get user
// fire away!
$request = new ListProvidersRequest($this->user);
$request->call();
// store all providers:
$providers = $request->getProviders();
foreach ($providers as $provider) {
// find provider?
$dbProvider = SpectreProvider::where('spectre_id', $provider['id'])->first();
if (is_null($dbProvider)) {
$dbProvider = new SpectreProvider;
}
// update fields:
$dbProvider->spectre_id = $provider['id'];
$dbProvider->code = $provider['code'];
$dbProvider->mode = $provider['mode'];
$dbProvider->status = $provider['status'];
$dbProvider->interactive = $provider['interactive'] === 1;
$dbProvider->automatic_fetch = $provider['automatic_fetch'] === 1;
$dbProvider->country_code = $provider['country_code'];
$dbProvider->data = $provider;
$dbProvider->save();
Log::debug(sprintf('Stored provider #%d under ID #%d', $provider['id'], $dbProvider->id));
}
app('fireflyconfig')->set('spectre_provider_download', time());
return;
}
}