Issues (38)

src/Jobs/Bookings/BlockoutsPull.php (2 issues)

Severity
1
<?php
2
3
namespace IproSync\Jobs\Bookings;
4
5
use Carbon\Carbon;
6
use Illuminate\Bus\Queueable;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Foundation\Bus\Dispatchable;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Queue\SerializesModels;
11
use IproSync\Models\Blockout;
12
use LaravelIproSoftwareApi\IproSoftwareFacade;
13
14
class BlockoutsPull implements ShouldQueue
15
{
16
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by IproSync\Jobs\Bookings\BlockoutsPull: $collectionClass, $id, $relations, $class, $keyBy
Loading history...
17
18
    protected int $propertyId;
19
    protected ?Carbon $checkIn  = null;
20
    protected ?Carbon $checkOut = null;
21
    protected array $requestParams;
22
23
    public function __construct(
24
        int     $propertyId,
25
        ?Carbon $checkIn = null,
26
        ?Carbon $checkOut = null,
27
        array   $requestParams = []
28
    ) {
29
        $this->propertyId    = $propertyId;
30
        $this->checkIn       = $checkIn;
31
        $this->checkOut      = $checkOut;
32
        $this->requestParams = $requestParams;
33
    }
34
35
36
    public function handle()
37
    {
38
        $response = IproSoftwareFacade::getPropertyBlockouts(
39
            $this->propertyId,
40
            [
41
                'query' => array_merge(
42
                    array_filter([
43
                        'checkin'  => $this->checkIn?->format('Y-m-d'),
44
                        'checkout' => $this->checkOut?->format('Y-m-d'),
45
                    ]),
46
                    $this->requestParams,
47
                ),
48
            ]
49
        )->onlySuccessful();
50
51
        $items = $response->json();
52
        if (!is_array($items)) {
0 ignored issues
show
The condition is_array($items) is always true.
Loading history...
53
            return;
54
        }
55
56
        foreach ($items as $item) {
57
            if (!empty($item['ID'])) {
58
                static::createOrUpdateBlockout($item);
59
            }
60
        }
61
    }
62
63
    public static function createOrUpdateBlockout(array $item): ?Blockout
64
    {
65
        if (isset($item['ID'])) {
66
            $model = Blockout::firstOrNew(['id' => $item['ID']], )
67
                               ->fill([
68
                                   'property_id'           => !empty($item['PropertyID']) ? (int) $item['PropertyID'] : null,
69
                                   'check_in'              => !empty($item['CheckIn']) ? Carbon::createFromFormat('Y-m-d', (string) $item['CheckIn'])->format('Y-m-d') : null,
70
                                   'check_out'             => !empty($item['CheckOut']) ? Carbon::createFromFormat('Y-m-d', (string) $item['CheckOut'])->format('Y-m-d') : null,
71
                                   'comments'              => !empty($item['Comments']) ? (string) $item['Comments'] : null,
72
                                   'imported_from_ical'    => !empty($item['ImportedFromiCal']) ? (string) $item['ImportedFromiCal'] : null,
73
                                   'imported_from_channel' => !empty($item['ImportedFromChannel']) ? (string) $item['ImportedFromChannel'] : null,
74
                               ])
75
                               ->fillPulled();
76
77
            $model->save();
78
79
            return $model;
80
        }
81
82
        return null;
83
    }
84
}
85