Family   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 38
c 2
b 1
f 0
dl 0
loc 105
rs 10
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getList() 0 9 2
A addEvent() 0 20 3
A events() 0 3 1
A title() 0 5 3
A getWifeName() 0 6 2
A husband() 0 3 1
A children() 0 3 1
A getHusbandName() 0 6 2
A wife() 0 3 1
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use LaravelEnso\People\Models\Person;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Person. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
// use App\Person;
8
use LaravelEnso\Tables\Traits\TableCache;
9
10
class Family extends Model
11
{
12
    use TableCache;
13
    // public function __construct(Array $attributes = [])
14
    // {
15
    //     parent::__construct($attributes);
16
    //     $db = \Session::get('db');
17
    //     error_log('+++Family++++++++++++++++++++++++++++++++'.$db);
18
    //     if(empty($db)) {
19
    //         $db = env('DB_DATABASE', 'enso');
20
    //     }
21
    //     if($db === env('DB_DATABASE')) {
22
    //         $key = 'database.connections.mysql.database';
23
    //         config([$key => $db]);
24
    //     } else {
25
    //         $key = 'database.connections.mysql.database';
26
    //         config([$key => $db]);
27
    //     }
28
    //     \DB::purge('mysql');
29
    //     \DB::reconnect('mysql');
30
    //     $this->setConnection('mysql');
31
    //     error_log('-----------------------------------'.$this->getConnection()->getDatabaseName());
32
    // }
33
    protected $fillable = ['description', 'is_active', 'husband_id', 'wife_id', 'type_id', 'chan', 'nchi', 'rin'];
34
35
    protected $attributes = ['is_active' => false];
36
37
    protected $casts = ['is_active' => 'boolean'];
38
39
    public function events()
40
    {
41
        return $this->hasMany(FamilyEvent::class);
42
    }
43
44
    public function children()
45
    {
46
        return $this->hasMany(Person::class, 'child_in_family_id');
47
    }
48
49
    public function husband()
50
    {
51
        return $this->hasOne(Person::class, 'id', 'husband_id');
52
    }
53
54
    public function wife()
55
    {
56
        return $this->hasOne(Person::class, 'id', 'wife_id');
57
    }
58
59
    public function title()
60
    {
61
        return (($this->husband) ? $this->husband->fullname() : '?').
62
            ' + '.
63
            (($this->wife) ? $this->wife->fullname() : '?');
64
    }
65
66
    public static function getList()
67
    {
68
        $families = self::get();
69
        $result = [];
70
        foreach ($families as $family) {
71
            $result[$family->id] = $family->title();
72
        }
73
74
        return collect($result);
75
    }
76
77
    public function addEvent($title, $date, $place, $description = '')
78
    {
79
        $place_id = Place::getIdByTitle($place);
80
        $event = FamilyEvent::updateOrCreate(
81
            [
82
                'family_id' => $this->id,
83
                'title' => $title,
84
            ],
85
            [
86
                'family_id' => $this->id,
87
                'title' => $title,
88
                'description' => $description,
89
            ]);
90
        if ($date) {
91
            $event->date = $date;
92
            $event->save();
93
        }
94
        if ($place) {
95
            $event->places_id = $place_id;
96
            $event->save();
97
        }
98
    }
99
100
    public function getWifeName()
101
    {
102
        if ($this->wife) {
103
            return $this->wife->fullname();
104
        } else {
105
            return 'unknown woman';
106
        }
107
    }
108
109
    public function getHusbandName()
110
    {
111
        if ($this->husband) {
112
            return $this->husband->fullname();
113
        } else {
114
            return 'unknown man';
115
        }
116
    }
117
}
118