BulkWriter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A handle() 0 8 2
1
<?php
2
3
namespace Bavix\Entry\Jobs;
4
5
use Bavix\LaravelClickHouse\Database\Eloquent\Model as Entry;
6
use Illuminate\Contracts\Queue\ShouldQueue;
7
use Illuminate\Foundation\Bus\Dispatchable;
8
use Illuminate\Queue\InteractsWithQueue;
9
use Illuminate\Queue\SerializesModels;
10
use Illuminate\Bus\Queueable;
11
12
class BulkWriter implements ShouldQueue
13
{
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 Bavix\Entry\Jobs\BulkWriter: $id, $relations, $class, $keyBy
Loading history...
16
17
    /**
18
     * @var Entry
19
     */
20
    protected $entry;
21
22
    /**
23
     * @var array[]
24
     */
25
    protected $data;
26
27
    /**
28
     * Create a new job instance.
29
     *
30
     * @param Entry $entry
31
     * @param array[] $data
32
     * @return void
33
     */
34
    public function __construct(Entry $entry, ?array $data = null)
35
    {
36
        if ($data === null) {
37
            $data = $entry->toArray();
38
        }
39
40
        $this->entry = $entry;
41
        $this->data = $data;
42
    }
43
44
    /**
45
     * Execute the job.
46
     *
47
     * @return void
48
     */
49
    public function handle(): void
50
    {
51
        if ($this->data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->data of type array<mixed,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...
52
            \array_walk_recursive($this->data, static function (&$value) {
53
                $value = $value ?? raw('NULL');
54
            });
55
56
            $this->entry::insert($this->data);
57
        }
58
    }
59
60
}
61