elvendor /
laravel-tcmb
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Elvendor\Tcmb\Commands; |
||
| 6 | |||
| 7 | use DateInterval; |
||
| 8 | use DatePeriod; |
||
| 9 | use DateTime; |
||
| 10 | use Illuminate\Console\Command; |
||
| 11 | use Elvendor\Tcmb\Tcmb; |
||
| 12 | use Elvendor\Tcmb\Models\ExchangeRate; |
||
| 13 | |||
| 14 | class SyncRatesCommand extends Command |
||
| 15 | { |
||
| 16 | protected $signature = 'tcmb:syncrates {date?} {endDate?}'; |
||
| 17 | protected $description = 'Get currency rates from the Central Bank of the Republic of Turkey'; |
||
| 18 | private array $rates = []; |
||
| 19 | |||
| 20 | public function handle() : void |
||
| 21 | { |
||
| 22 | $begin = new DateTime($this->argument('date') ?? date('Y-m-d')); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 23 | $end = new DateTime($this->argument('endDate') ?? date('Y-m-d')); |
||
| 24 | |||
| 25 | if($begin != $end) { |
||
| 26 | $period = new DatePeriod($begin, DateInterval::createFromDateString('1 day'), $end); |
||
| 27 | foreach ($period as $dt){ |
||
| 28 | $this->syncRates($dt); |
||
| 29 | } |
||
| 30 | }else{ |
||
| 31 | $this->syncRates($begin); |
||
| 32 | } |
||
| 33 | ExchangeRate::insertOrIgnore($this->rates); |
||
| 34 | } |
||
| 35 | |||
| 36 | private function syncRates(DateTime $dt) : void |
||
| 37 | { |
||
| 38 | $rates = Tcmb::fetchRates($dt); |
||
| 39 | if($rates){ |
||
|
0 ignored issues
–
show
The expression
$rates of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 40 | $this->info($dt->format('d.m.Y') . ' synced'); |
||
| 41 | array_push($this->rates, ['date' => $dt->format('Y-m-d'), 'rates' => json_encode($rates)]); |
||
| 42 | }else{ |
||
| 43 | $this->error($dt->format('d.m.Y') . ' not synced'); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 |