Passed
Push — master ( 8bbcc4...cbfdba )
by Бабичев
09:25
created

BulkWrite::bulkHandling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Bavix\Entry\Commands;
4
5
use Bavix\LaravelClickHouse\Database\Eloquent\Model as Entry;
6
use Bavix\Entry\Services\BulkService;
7
use Bavix\Entry\Jobs\BulkWriter;
8
use Illuminate\Console\Command;
9
use Illuminate\Contracts\Cache\LockTimeoutException;
10
use Illuminate\Support\Facades\Cache;
11
12
class BulkWrite extends Command
13
{
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'entry:bulk';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Pull out from redis and throws in the queue for recording';
28
29
    /**
30
     * @return void
31
     * @throws
32
     */
33
    public function handle(): void
34
    {
35
        $lock = Cache::lock(__CLASS__, 120);
36
        try {
37
            $lock->block(1);
38
            // Lock acquired after waiting maximum of second...
39
            $batchSize = \config('entry.batchSize', 10000);
40
            $queueName = \config('entry.queueName', 'default');
41
            $keys = app(BulkService::class)->keys();
42
            foreach ($keys as $key) {
43
                [$bulkName, $class] = \explode(':', $key, 2);
44
                $chunkIterator = app(BulkService::class)
45
                    ->chunkIterator($batchSize, $key);
46
47
                foreach ($chunkIterator as $bulkData) {
48
                    foreach ($bulkData as $itemKey => $itemValue) {
49
                        $bulkData[$itemKey] = \json_decode($itemValue, true);
50
                    }
51
52
                    $modelEntry = new $class;
53
                    $bulkData = $this->bulkHandling($modelEntry, $bulkData);
54
                    if ($bulkData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $bulkData of type 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...
55
                        $job = new BulkWriter($modelEntry, $bulkData);
56
                        $job->onQueue($queueName);
57
                        \dispatch($job);
58
                    }
59
                }
60
            }
61
        } catch (LockTimeoutException $timeoutException) {
62
            // Unable to acquire lock...
63
        } finally {
64
            optional($lock)->release();
65
        }
66
    }
67
68
    /**
69
     * The process of processing data before sending it to the queue.
70
     * Here we analyze the data and Supplement it with information from the heap.
71
     *
72
     * @param Entry $entry
73
     * @param array $bulk
74
     * @return array
75
     */
76
    protected function bulkHandling(Entry $entry, array $bulk): array
0 ignored issues
show
Unused Code introduced by
The parameter $entry is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

76
    protected function bulkHandling(/** @scrutinizer ignore-unused */ Entry $entry, array $bulk): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        return $bulk;
79
    }
80
81
}
82