GedcomParser::parse()   F
last analyzed

Complexity

Conditions 38
Paths 0

Size

Total Lines 230
Code Lines 148

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 38
eloc 148
c 3
b 0
f 0
nc 0
nop 5
dl 0
loc 230
rs 3.3333

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 DB;
0 ignored issues
show
Bug introduced by
The type DB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use FamilyTree365\LaravelGedcom\Events\GedComProgressSent;
7
use FamilyTree365\LaravelGedcom\Models\PersonAlia;
8
use FamilyTree365\LaravelGedcom\Models\PersonAsso;
9
use FamilyTree365\LaravelGedcom\Utils\Importer\Note;
10
use FamilyTree365\LaravelGedcom\Utils\Importer\Obje;
11
use FamilyTree365\LaravelGedcom\Utils\Importer\Repo;
12
use FamilyTree365\LaravelGedcom\Utils\Importer\Sour;
13
use FamilyTree365\LaravelGedcom\Utils\Importer\Subm;
14
use FamilyTree365\LaravelGedcom\Utils\Importer\Subn;
15
use Gedcom\Parser;
16
use Illuminate\Console\OutputStyle;
0 ignored issues
show
Bug introduced by
The type Illuminate\Console\OutputStyle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Illuminate\Support\Facades\Log;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Symfony\Component\Console\Input\StringInput;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Input\StringInput was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Symfony\Component\Console\Output\StreamOutput;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Console\Output\StreamOutput was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
class GedcomParser
22
{
23
    /**
24
     * Array of persons ID
25
     * key - old GEDCOM ID
26
     * value - new autoincrement ID.
27
     *
28
     * @var string
29
     */
30
    protected $persons_id = [];
31
    protected $subm_ids = [];
32
    protected $sour_ids = [];
33
    protected $obje_ids = [];
34
    protected $note_ids = [];
35
    protected $repo_ids = [];
36
    protected $conn = '';
37
38
    public function parse(
39
        $conn,
40
        string $filename,
41
        string $slug,
42
        bool $progressBar = null,
43
        $channel = ['name' => 'gedcom-progress1', 'eventName' => 'newMessage']
44
    ) {
45
        DB::disableQueryLog();
46
        //start calculating the time
47
        $time_start = microtime(true);
48
        $this->conn = $conn;
49
        //start calculating the memory - https://www.php.net/manual/en/function.memory-get-usage.php
50
        $startMemoryUse = round(memory_get_usage() / 1048576, 2);
51
52
        error_log("\n Memory Usage: ".$startMemoryUse.' MB');
53
        error_log('PARSE LOG : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'.$conn);
54
        $parser = new Parser();
55
        $gedcom = @$parser->parse($filename);
56
57
        /**
58
         * work.
59
         */
60
        $subn = [];
61
        $subm = [];
62
        $sour = [];
63
        $note = [];
64
        $repo = [];
65
        $obje = [];
66
67
        if ($gedcom->getSubn()) {
68
            $subn = $gedcom->getSubn();
69
        }
70
        if ($gedcom->getSubm()) {
71
            $subm = $gedcom->getSubm();
72
        }
73
        if ($gedcom->getSour()) {
74
            $sour = $gedcom->getSour();
75
        }
76
        if ($gedcom->getNote()) {
77
            $note = $gedcom->getNote();
78
        }
79
        if ($gedcom->getRepo()) {
80
            $repo = $gedcom->getRepo();
81
        }
82
        if ($gedcom->getObje()) {
83
            $obje = $gedcom->getObje();
84
        }
85
86
        /**
87
         * work end.
88
         */
89
        $c_subn = 0;
90
        $c_subm = count($subm);
91
        $c_sour = count($sour);
92
        $c_note = count($note);
93
        $c_repo = count($repo);
94
        $c_obje = count($obje);
95
        if ($subn != null) {
96
            $c_subn = 1;
97
        }
98
        $beforeInsert = round(memory_get_usage() / 1048576, 2);
99
100
        $individuals = $gedcom->getIndi();
101
        $families = $gedcom->getFam();
102
        $indCount = count($individuals);
103
        $famCount = count($families);
104
        $total = $indCount + $famCount + $c_subn + $c_subm + $c_sour + $c_note + $c_repo + $c_obje;
105
        $complete = 0;
106
        if ($progressBar === true) {
107
            $bar = $this->getProgressBar($indCount + $famCount);
108
            event(new GedComProgressSent($slug, $total, $complete, $channel));
0 ignored issues
show
Bug introduced by
The function event 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

108
            /** @scrutinizer ignore-call */ 
109
            event(new GedComProgressSent($slug, $total, $complete, $channel));
Loading history...
109
        }
110
        Log::info('Individual:'.$indCount);
111
        Log::info('Families:'.$famCount);
112
        Log::info('Subn:'.$c_subn);
113
        Log::info('Subm:'.$c_subm);
114
        Log::info('Sour:'.$c_sour);
115
        Log::info('Note:'.$c_note);
116
        Log::info('Repo:'.$c_repo);
117
118
        try {
119
            // store all the media objects that are contained within the GEDCOM file.
120
            foreach ($obje as $item) {
121
                // $this->getObje($item);
122
                if ($item) {
123
                    $_obje_id = $item->getId();
124
                    $obje_id = Obje::read($this->conn, $item);
125
                    if ($obje_id != 0) {
126
                        $this->obje_ids[$_obje_id] = $obje_id;
127
                    }
128
                }
129
                if ($progressBar === true) {
130
                    $bar->advance();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $bar does not seem to be defined for all execution paths leading up to this point.
Loading history...
131
                    $complete++;
132
                    event(new GedComProgressSent($slug, $total, $complete, $channel));
133
                }
134
            }
135
            // store information about all the submitters to the GEDCOM file.
136
            foreach ($subm as $item) {
137
                if ($item) {
138
                    $_subm_id = $item->getSubm();
139
                    $subm_id = Subm::read($this->conn, $item, null, null, $this->obje_ids);
140
                    $this->subm_ids[$_subm_id] = $subm_id;
141
                }
142
                if ($progressBar === true) {
143
                    $bar->advance();
144
                    $complete++;
145
                    event(new GedComProgressSent($slug, $total, $complete, $channel));
146
                }
147
            }
148
149
            if ($subn != null) {
150
                // store the submission information for the GEDCOM file.
151
                // $this->getSubn($subn);
152
                Subn::read($this->conn, $subn, $this->subm_ids);
153
                if ($progressBar === true) {
154
                    $bar->advance();
155
                    $complete++;
156
                    event(new GedComProgressSent($slug, $total, $complete, $channel));
157
                }
158
            }
159
160
            // store all the notes contained within the GEDCOM file that are not inline.
161
            foreach ($note as $item) {
162
                // $this->getNote($item);
163
                if ($item) {
164
                    $note_id = $item->getId();
165
                    $_note_id = Note::read($this->conn, $item);
166
                    $this->note_ids[$note_id] = $_note_id;
167
                }
168
                if ($progressBar === true) {
169
                    $bar->advance();
170
                    $complete++;
171
                    event(new GedComProgressSent($slug, $total, $complete, $channel));
172
                }
173
            }
174
175
            // store all repositories that are contained within the GEDCOM file and referenced by sources.
176
            foreach ($repo as $item) {
177
                // $this->getRepo($item);
178
                if ($item) {
179
                    $repo_id = $item->getRepo();
180
                    $_repo_id = Repo::read($this->conn, $item);
181
                    $this->repo_ids[$repo_id] = $_repo_id;
182
                }
183
                if ($progressBar === true) {
184
                    $bar->advance();
185
                    $complete++;
186
                    event(new GedComProgressSent($slug, $total, $complete, $channel));
187
                }
188
            }
189
190
            // store sources cited throughout the GEDCOM file.
191
            // obje import before sour import
192
            foreach ($sour as $item) {
193
                // $this->getSour($item);
194
                if ($item) {
195
                    $_sour_id = $item->getSour();
196
                    $sour_id = Sour::read($this->conn, $item, $this->obje_ids);
197
                    if ($sour_id != 0) {
198
                        $this->sour_ids[$_sour_id] = $sour_id;
199
                    }
200
                }
201
                if ($progressBar === true) {
202
                    $bar->advance();
203
                    $complete++;
204
                    event(new GedComProgressSent($slug, $total, $complete, $channel));
205
                }
206
            }
207
208
            $parentData = ParentData::getPerson($this->conn, $individuals, $this->obje_ids, $this->sour_ids);
209
210
            foreach ($individuals as $individual) {
211
                if ($progressBar === true) {
212
                    $bar->advance();
213
                    $complete++;
214
                    event(new GedComProgressSent($slug, $total, $complete, $channel));
215
                }
216
            }
217
218
            // complete person-alia and person-asso table with person table
219
            $alia_list = PersonAlia::on($conn)->select('alia')->where('group', 'indi')->where('import_confirm', 0)->get();
220
            foreach ($alia_list as $item) {
221
                $alia = $item->alia;
222
                if (isset($this->person_ids[$alia])) {
0 ignored issues
show
Bug introduced by
The property person_ids does not exist on FamilyTree365\LaravelGedcom\Utils\GedcomParser. Did you mean persons_id?
Loading history...
223
                    $item->alia = $this->person_ids[$alia];
224
                    $item->import_confirm = 1;
225
                    $item->save();
226
                } else {
227
                    $item->delete();
228
                }
229
            }
230
231
            $asso_list = PersonAsso::on($conn)->select('indi')->where('group', 'indi')->where('import_confirm', 0)->get();
232
            foreach ($asso_list as $item) {
233
                $_indi = $item->indi;
234
                if (isset($this->person_ids[$_indi])) {
235
                    $item->indi = $this->person_ids[$_indi];
236
                    $item->import_confirm = 1;
237
                    $item->save();
238
                } else {
239
                    $item->delete();
240
                }
241
            }
242
243
            FamilyData::getFamily($this->conn, $families, $this->obje_ids, $this->sour_ids, $this->persons_id, $this->note_ids, $this->repo_ids, $parentData);
244
245
            foreach ($families as $family) {
246
                FamilyData::getFamily($this->conn, $family, $this->obje_ids);
247
                if ($progressBar === true) {
248
                    $bar->advance();
249
                    $complete++;
250
                    event(new GedComProgressSent($slug, $total, $complete, $channel));
251
                }
252
            }
253
        } catch (\Exception $e) {
254
            $error = $e->getMessage();
255
256
            return \Log::error($error);
257
        }
258
259
        if ($progressBar === true) {
260
            $time_end = microtime(true);
261
            $endMemoryUse = round(memory_get_usage() / 1048576, 2);
262
            $execution_time = ($time_end - $time_start);
263
            $beform_insert_memory = $beforeInsert - $startMemoryUse;
0 ignored issues
show
Unused Code introduced by
The assignment to $beform_insert_memory is dead and can be removed.
Loading history...
264
            $memory_usage = $endMemoryUse - $startMemoryUse;
265
            error_log("\nTotal Execution Time: ".round($execution_time).' Seconds');
266
            error_log("\nMemory Usage: ".$memory_usage.''.' MB');
267
            $bar->finish();
268
        }
269
    }
270
271
    private function getProgressBar(int $max)
272
    {
273
        return (new OutputStyle(
274
            new StringInput(''),
275
            new StreamOutput(fopen('php://stdout', 'w'))
276
        ))->createProgressBar($max);
277
    }
278
}
279