Passed
Push — master ( a9f88f...a24c15 )
by Curtis
15:49 queued 10:33
created

Family::title()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 3
nc 4
nop 0
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use LaravelEnso\People\App\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 LaravelEnso\Tables\App\Traits\TableCache;
8
9
class Family extends Model
10
{
11
    use TableCache;
12
    protected $fillable = ['description', 'is_active', 'husband_id', 'wife_id', 'type_id'];
13
14
    protected $attributes = ['is_active' => false];
15
16
    protected $casts = ['is_active' => 'boolean'];
17
18
    public function events()
19
    {
20
        return $this->hasMany(FamilyEvent::class);
21
    }
22
23
    public function children()
24
    {
25
        return $this->hasMany(Person::class, 'child_in_family_id');
26
    }
27
28
    public function husband()
29
    {
30
        return $this->hasOne(Person::class, 'id', 'husband_id');
31
    }
32
33
    public function wife()
34
    {
35
        return $this->hasOne(Person::class, 'id', 'wife_id');
36
    }
37
38
    public function title()
39
    {
40
        return (($this->husband) ? $this->husband->fullname() : "?") .
41
            " + " .
42
            (($this->wife) ? $this->wife->fullname() : "?");
43
    }
44
45
    public static function getList()
46
    {
47
        $families = self::get();
48
        $result = array();
49
        foreach ($families as $family) {
50
            $result[$family->id] = $family->title();
51
        }
52
53
        return collect($result);
54
    }
55
56
    public function addEvent($title, $date, $place, $description = '')
57
    {
58
        $place_id = Place::getIdByTitle($place);
59
        $event = FamilyEvent::create(array(
60
            'family_id' => $this->id,
61
            'title' => $title,
62
            'description' => $description,
63
        ));
64
        if ($date) {
65
            $event->date = $date;
66
            $event->save();
67
        }
68
        if ($place) {
69
            $event->places_id = $place_id;
70
            $event->save();
71
        }
72
    }
73
74
75
    public function getWifeName()
76
    {
77
        if ($this->wife) {
78
            return $this->wife->fullname();
79
        } else {
80
            return "unknown woman";
81
        }
82
    }
83
84
    public function getHusbandName()
85
    {
86
        if ($this->husband) {
87
            return $this->husband->fullname();
88
        } else {
89
            return "unknown man";
90
        }
91
    }
92
}
93
94
95