LocationsPull::handle()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 5
nop 0
dl 0
loc 20
ccs 0
cts 17
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace IproSync\Jobs\Settings;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Foundation\Bus\Dispatchable;
8
use Illuminate\Queue\InteractsWithQueue;
9
use Illuminate\Queue\SerializesModels;
10
use IproSync\Models\Location;
11
use LaravelIproSoftwareApi\IproSoftwareFacade;
12
13
class LocationsPull implements ShouldQueue
14
{
15
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by IproSync\Jobs\Settings\LocationsPull: $collectionClass, $id, $relations, $class, $keyBy
Loading history...
16
17
    public function handle()
18
    {
19
        $response = IproSoftwareFacade::getLocationsList()->onlySuccessful();
20
21
        $groups = $response->json();
22
        if (is_array($groups) && !empty($groups)) {
23
            foreach ($groups as $items) {
24
                foreach ($items['Children'] ?? [] as $item) {
25
                    if (!isset($item['Id'])) {
26
                        continue;
27
                    }
28
                    Location::firstOrNew(['id' => $item['Id']], )
29
                            ->fill([
30
                                'name'     => $item['Name'],
31
                                'type_id'  => $items['Id'],
32
                                'type'     => $items['Name'],
33
                                'children' => $item['Children'] ?? [],
34
                            ])
35
                            ->fillPulled()
36
                            ->save();
37
                }
38
            }
39
        }
40
    }
41
}
42