BlockoutsPullCommand::handle()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 8
nop 0
dl 0
loc 29
ccs 0
cts 22
cp 0
crap 30
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
namespace IproSync\Console\Commands;
4
5
use Carbon\Carbon;
6
use Illuminate\Console\Command;
7
use IproSync\Console\Commands\Traits\HasRequestParams;
8
use IproSync\Jobs\Bookings\BlockoutsPull;
9
use IproSync\Models\Property;
10
11
class BlockoutsPullCommand extends Command
12
{
13
    use HasRequestParams;
14
15
    protected $signature = 'iprosoftware-sync:blockouts:pull
16
     {--property_id= : Pull blockouts by ipro property id.}
17
     {--from= : Date from, format Y-m-d}
18
     {--to= : Date to, format Y-m-d}
19
     {--request_params= : Send query params (url encoded).}
20
     {--queue= : Queue to dispatch job.}
21
    ';
22
23
    protected $description = 'Pull ipro blockouts';
24
25
    public function handle(): int
26
    {
27
        $from = $this->option('from') ? Carbon::createFromFormat('Y-m-d', $this->option('from')) : null;
28
        $to   = $this->option('to') ? Carbon::createFromFormat('Y-m-d', $this->option('to')) : null;
29
30
        if ($this->option('property_id')) {
31
            BlockoutsPull::dispatch(
32
                $this->option('property_id'),
33
                $from,
34
                $to,
35
                $this->getRequestParams(),
36
            )->onQueue($this->option('queue'));
37
        } else {
38
            Property::query()
39
                    ->chunk(100, function ($properties) use ($from, $to) {
40
                        /** @var Property $property */
41
                        foreach ($properties as $property) {
42
                            BlockoutsPull::dispatch(
43
                                $property->getKey(),
44
                                $from,
45
                                $to,
46
                                $this->getRequestParams()
47
                            )
48
                                ->onQueue($this->option('queue'));
49
                        }
50
                    });
51
        }
52
53
        return 0;
54
    }
55
}
56