UpdateRelatedIds::handle()   B
last analyzed

Complexity

Conditions 9
Paths 29

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 27
rs 8.0555
c 0
b 0
f 0
cc 9
nc 29
nop 0
1
<?php
2
3
namespace App\Jobs;
4
5
use App\Models\Batch;
6
use App\Models\Concept;
7
use App\Models\ConceptAttribute;
8
use App\Models\Element;
9
use Exception;
10
use Illuminate\Bus\Queueable;
11
use Illuminate\Contracts\Queue\ShouldQueue;
12
use Illuminate\Foundation\Bus\Dispatchable;
13
use Illuminate\Queue\InteractsWithQueue;
14
use Illuminate\Queue\SerializesModels;
15
use Illuminate\Support\Facades\Log;
16
17
class UpdateRelatedIds implements ShouldQueue
18
{
19
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by App\Jobs\UpdateRelatedIds: $id, $class
Loading history...
20
    /**
21
     * @var Batch
22
     */
23
    private $batch;
24
25
    /**
26
     * Create a new job instance.
27
     *
28
     * @param Batch $batch
29
     */
30
    public function __construct(Batch $batch)
31
    {
32
        //
33
        $this->batch = $batch;
34
    }
35
36
    /**
37
     * Execute the job.
38
     *
39
     * @return void
40
     */
41
    public function handle()
42
    {
43
        try {
44
            foreach ($this->batch->imports as $import) {
45
                $statements = $import->conceptResourceStatements()->where('related_concept_id', null);
46
                /** @var ConceptAttribute[] $statements */
47
                if ($statements->count()) {
48
                    foreach ($statements as $statement) {
49
                        $concept = Concept::where('uri', $statement->object)->first();
50
                        if ($concept) {
51
                            $statement->update(['related_concept_id' => $concept->id]);
52
                        }
53
                    }
54
                }
55
                $statements = $import->elementResourceStatements()->where('related_schema_property_id', null);
56
                /** @var ConceptAttribute[] $statements */
57
                if ($statements->count()) {
58
                    foreach ($statements as $statement) {
59
                        $element = Element::where('uri', $statement->object)->first();
60
                        if ($element) {
61
                            $statement->update(['related_schema_property_id' => $element->id]);
62
                        }
63
                    }
64
                }
65
            }
66
        } catch (Exception $e) {
67
            Log::error($e->getMessage());
68
        }
69
    }
70
}
71