1. <?php
  2.  
  3. namespace App\Jobs;
  4.  
  5. use App\Models\Coin;
  6. use App\Utilities\Coinr;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12.  
  13. class UpdateCoinData implements ShouldQueue
  14. {
  15.     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16.  
  17.     protected $coin;
  18.  
  19.     /**
  20.      * Create a new job instance.
  21.      *
  22.      * @param Coin  $coin
  23.      * @return void
  24.      */
  25.     public function __construct(Coin $coin)
  26.     {
  27.         $this->coin = $coin;
  28.     }
  29.  
  30.     /**
  31.      * Execute the job.
  32.      *
  33.      * @return void
  34.      */
  35.     public function handle()
  36.     {
  37.         $coinr = new Coinr;
  38.  
  39.         $data = $coinr->coinData($this->coin->name);
  40.  
  41.         $this->coin->update([
  42.             'name' => array_get($data, 'id'),
  43.             'label' => array_get($data, 'name'),
  44.             'short_name' => array_get($data, 'symbol'),
  45.             'price_eur' => array_get($data, 'price_eur'),
  46.             'price_usd' => array_get($data, 'price_usd'),
  47.             'price_btc' => array_get($data, 'price_btc'),
  48.             'percent_change_1h' => array_get($data, 'percent_change_1h'),
  49.             'percent_change_24h' => array_get($data, 'percent_change_24h'),
  50.             'percent_change_7d' => array_get($data, 'percent_change_7d'),
  51.         ]);
  52.  
  53.         $this->coin->histories()->create([
  54.             'quantity' => $this->coin->quantity,
  55.             'price_eur' => array_get($data, 'price_eur'),
  56.             'price_usd' => array_get($data, 'price_usd'),
  57.             'price_btc' => array_get($data, 'price_btc'),
  58.         ]);
  59.     }
  60. }