CopyPeopleFromTenancyToGenealogy::handle()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
nc 1
nop 0
dl 0
loc 38
rs 9.2568
c 1
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands\Synchronize;
4
5
use App\Models\Person;
6
use App\Models\Tenant;
7
use App\Models\TenantPerson;
8
use Illuminate\Console\Command;
9
10
class CopyPeopleFromTenancyToGenealogy extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'synchronize:people';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Copy people records from tenancy databases into main genealogy database';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @return int
30
     */
31
    public function handle()
32
    {
33
        $recordsSince = TenantPerson::orderBy('updated_at', 'desc')->first()?->updated_at;
0 ignored issues
show
Bug introduced by
The property updated_at does not seem to exist on App\Models\TenantPerson. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property updated_at does not seem to exist on Illuminate\Database\Eloq...Relations\HasOneThrough.
Loading history...
Bug introduced by
The property updated_at does not seem to exist on Illuminate\Database\Eloq...elations\HasManyThrough.
Loading history...
34
        $this->info('Tenant people records syncronization since: '.($recordsSince ?: 'start'));
35
36
        tenancy()->query()->cursor()->each(function (Tenant $tenant) use ($recordsSince) {
37
            try {
38
                tenancy()->initialize($tenant);
39
40
                $tenantPersonQuery = Person::query();
41
42
                if ($recordsSince) {
43
                    $tenantPersonQuery->where('updated_at', '>=', $recordsSince);
44
                }
45
46
                $this->info('Processing tenant #'.$tenant->id.' records: '.(clone $tenantPersonQuery)->count());
0 ignored issues
show
Bug introduced by
Are you sure clone $tenantPersonQuery->count() of type Illuminate\Database\Eloquent\Builder|integer|mixed can be used in concatenation? ( Ignorable by Annotation )

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

46
                $this->info('Processing tenant #'.$tenant->id.' records: './** @scrutinizer ignore-type */ (clone $tenantPersonQuery)->count());
Loading history...
47
                $tenantPersonQuery->chunk(100, function ($people) use ($tenant) {
48
                    // clear old records and push updated ones
49
                    TenantPerson::where('tenant_id', $tenant->id)
50
                        ->whereIn('tenant_person_id', $people->pluck('id'))
51
                        ->delete();
52
53
                    foreach ($people as $person) {
54
                        $personData = $person->toArray();
55
                        unset($personData['id']);
56
                        $personData['tenant_id'] = $tenant->id;
57
                        $personData['tenant_person_id'] = $person->id;
58
59
                        (new TenantPerson($personData))->save();
60
                    }
61
                });
62
63
                tenancy()->end();
64
            } catch (\Exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
65
            }
66
        });
67
68
        return Command::SUCCESS;
69
    }
70
}
71