SyncRatesCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A syncRates() 0 8 2
A handle() 0 14 3
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
It seems like $this->argument('date') ?? date('Y-m-d') can also be of type array; however, parameter $datetime of DateTime::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        $begin = new DateTime(/** @scrutinizer ignore-type */ $this->argument('date') ?? date('Y-m-d'));
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
Bug Best Practice introduced by
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 empty(..) or ! empty(...) instead.

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