Passed
Pull Request — master (#57)
by
unknown
06:41 queued 04:01
created

ParentData::getPerson()   F

Complexity

Conditions 10
Paths 519

Size

Total Lines 150
Code Lines 122

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 3 Features 1
Metric Value
cc 10
eloc 122
c 9
b 3
f 1
nc 519
nop 4
dl 0
loc 150
rs 3.3344

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace FamilyTree365\LaravelGedcom\Utils;
4
5
use FamilyTree365\LaravelGedcom\Models\Family;
6
use FamilyTree365\LaravelGedcom\Models\Person;
7
8
class ParentData
9
{
10
    /**
11
     * Array of persons ID
12
     * key - old GEDCOM ID
13
     * value - new autoincrement ID.
14
     *
15
     * @var string
16
     */
17
    protected $persons_id = [];
18
    protected $subm_ids = [];
19
    protected $sour_ids = [];
20
    protected $obje_ids = [];
21
    protected $note_ids = [];
22
    protected $repo_ids = [];
23
    protected $conn = '';
24
25
    public static function getPerson($conn, $individuals, $obje_ids = [], $sour_ids = [])
26
    {
27
        $ParentData = [];
28
        $a = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $a is dead and can be removed.
Loading history...
29
30
        try {
31
            foreach ($individuals as $k => $individual) {
32
                $g_id = $individual->getId();
0 ignored issues
show
Unused Code introduced by
The assignment to $g_id is dead and can be removed.
Loading history...
33
                $name = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
34
                $givn = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $givn is dead and can be removed.
Loading history...
35
                $surn = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $surn is dead and can be removed.
Loading history...
36
                $name = '';
37
                $npfx = '';
38
                $givn = '';
39
                $nick = '';
40
                $spfx = '';
41
                $surn = '';
42
                $nsfx = '';
43
                $type = '';
44
                $fone = null; // Gedcom/
0 ignored issues
show
Unused Code introduced by
The assignment to $fone is dead and can be removed.
Loading history...
45
                $romn = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $romn is dead and can be removed.
Loading history...
46
                $names = $individual->getName();
47
                $attr = $individual->getAllAttr();
48
                $events = $individual->getAllEven();
0 ignored issues
show
Unused Code introduced by
The assignment to $events is dead and can be removed.
Loading history...
49
                $note = $individual->getNote();
0 ignored issues
show
Unused Code introduced by
The assignment to $note is dead and can be removed.
Loading history...
50
                $indv_sour = $individual->getSour();
0 ignored issues
show
Unused Code introduced by
The assignment to $indv_sour is dead and can be removed.
Loading history...
51
                $alia = $individual->getAlia(); // string array
0 ignored issues
show
Unused Code introduced by
The assignment to $alia is dead and can be removed.
Loading history...
52
                $asso = $individual->getAsso();
0 ignored issues
show
Unused Code introduced by
The assignment to $asso is dead and can be removed.
Loading history...
53
                $subm = $individual->getSubm();
0 ignored issues
show
Unused Code introduced by
The assignment to $subm is dead and can be removed.
Loading history...
54
                $anci = $individual->getAnci();
0 ignored issues
show
Unused Code introduced by
The assignment to $anci is dead and can be removed.
Loading history...
55
                $refn = $individual->getRefn();
0 ignored issues
show
Unused Code introduced by
The assignment to $refn is dead and can be removed.
Loading history...
56
                $obje = $individual->getObje();
0 ignored issues
show
Unused Code introduced by
The assignment to $obje is dead and can be removed.
Loading history...
57
                // object
58
                $bapl = $individual->getBapl();
0 ignored issues
show
Unused Code introduced by
The assignment to $bapl is dead and can be removed.
Loading history...
59
                $conl = $individual->getConl();
0 ignored issues
show
Unused Code introduced by
The assignment to $conl is dead and can be removed.
Loading history...
60
                $endl = $individual->getEndl();
0 ignored issues
show
Unused Code introduced by
The assignment to $endl is dead and can be removed.
Loading history...
61
                $slgc = $individual->getSlgc();
0 ignored issues
show
Unused Code introduced by
The assignment to $slgc is dead and can be removed.
Loading history...
62
                $chan = $individual->getChan();
63
//                $a = $chan->getDatetime() ? '' : '';
64
                $g_id = $individual->getId();
65
66
                if (!empty($names)) {
67
                    $name = current($names)->getName();
68
                    $npfx = current($names)->getNpfx();
69
                    $givn = current($names)->getGivn();
70
                    $nick = current($names)->getNick();
71
                    $spfx = current($names)->getSpfx();
72
                    $surn = current($names)->getSurn();
73
                    $nsfx = current($names)->getNsfx();
74
                    $type = current($names)->getType();
75
                }
76
77
                // array value
78
                $fams = $individual->getFams();  // self family, leave it now, note would be included in family
79
                $famc = $individual->getFamc();  // parent family , leave it now, note and pedi would be included in family
80
81
                // added to database
82
                // string value
83
                $sex = preg_replace('/[^MF]/', '', $individual->getSex());
84
                $uid = $individual->getUid();
85
                $resn = $individual->getResn();
86
                $rin = $individual->getRin();
87
                $rfn = $individual->getRfn();
88
                $afn = $individual->getAfn();
89
                $titl = $individual->getAttr();
0 ignored issues
show
Unused Code introduced by
The assignment to $titl is dead and can be removed.
Loading history...
90
                
91
                $birt = $individual->getBirt();
92
                $birthday = $birt->dateFormatted ?? null;
93
                $birth_month = $birt->month ?? null;
94
                $birth_year = $birt->year ?? null;
95
                $birthday_dati = $birt->dati ?? null;
96
                $birthday_plac = $birt->plac ?? null;
97
                
98
                $deat = $individual->getDeat();
99
                $deathday = $deat->dateFormatted ?? null;
100
                $death_month = $deat->month ?? null;
101
                $death_year = $deat->year ?? null;
102
                $deathday_dati = $deat->dati ?? null;
103
                $deathday_plac = $deat->plac ?? null;
104
                $deathday_caus = $deat->caus ?? null;
105
106
                $buri = $individual->getBuri();
107
                $burial_day = $buri->dateFormatted ?? null;
108
                $burial_month = $buri->month ?? null;
109
                $burial_year = $buri->year ?? null;
110
                $burial_day_dati = $buri->dati ?? null;
111
                $burial_day_plac = $buri->plac ?? null;
112
113
                $chr = $individual->getChr();
114
                $chr = $chr->dateFormatted ?? null;
115
116
                if ($givn == '') {
117
                    $givn = $name;
118
                }
119
120
                $config = json_encode(config('database.connections.'.$conn));
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

120
                $config = json_encode(/** @scrutinizer ignore-call */ config('database.connections.'.$conn));
Loading history...
Unused Code introduced by
The assignment to $config is dead and can be removed.
Loading history...
121
                $value = [
122
                    'gid' => $g_id,
123
                    'name' => $name,
124
                    'givn' => $givn,
125
                    'surn' => $surn,
126
                    'sex' => $sex,
127
                    'uid' => $uid,
128
                    'rin' => $rin,
129
                    'resn' => $resn,
130
                    'rfn' => $rfn,
131
                    'afn' => $afn,
132
                    'nick' => $nick,
133
                    'type' => $type,
134
                    'chan' => $chan ? $chan->getDatetime() : null,
135
                    'nsfx' => $nsfx,
136
                    'npfx' => $npfx,
137
                    'spfx' => $spfx,
138
                    'birthday' => $birthday,
139
                    'birth_month' => $birth_month,
140
                    'birth_year' => $birth_year,
141
                    'birthday_dati' => $birthday_dati,
142
                    'birthday_plac' => $birthday_plac,
143
                    'deathday' => $deathday,
144
                    'death_month' => $death_month,
145
                    'death_year' => $death_year,
146
                    'deathday_dati' => $deathday_dati,
147
                    'deathday_plac' => $deathday_plac,
148
                    'deathday_caus' => $deathday_caus,
149
                    'burial_day' => $burial_day,
150
                    'burial_month' => $burial_month,
151
                    'burial_year' => $burial_year,
152
                    'burial_day_dati' => $burial_day_dati,
153
                    'burial_day_plac' => $burial_day_plac,
154
                    'titl' => $attr ? $attr['TITL'][0]->getAttr('TITL') : null,
155
                    'famc' => $famc ? $famc[0]->getFamc() : null,
156
                    'fams' => $fams ? $fams[0]->getFams() : null,
157
                    'chr' => $chr
158
                ];
159
160
                $ParentData[] = $value;
161
            }
162
163
            $chunk = array_chunk($ParentData, 1000);
164
165
            foreach ($chunk as $ParentData) {
166
                // it's take only 1 second for 3010 record
167
                Person::on($conn)->upsert($ParentData, ['uid']);
168
            }
169
170
            otherFields::insertOtherFields($conn, $individuals, $obje_ids, $sour_ids);
171
        } catch (\Exception $e) {
172
            $error = $e->getMessage();
173
174
            return \Log::error($error);
175
        }
176
    }
177
}
178