Passed
Push — master ( 889e59...55e7f1 )
by Curtis
05:56
created

FamilyController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Family;
4
5
use App\Family;
6
use App\Forms\Builders\FamilyForm;
7
use App\Http\Controllers\Controller;
8
use App\Http\Requests\ValidateFamilyRequest;
9
use App\Individual;
10
use App\Person;
11
12
class FamilyController extends Controller
13
{
14
    private function attach($family, $father_id, $mother_id, $individual_list)
15
    {
16
        Individual::find($father_id)->families()->attach($family->id, ['type_id' => 1]);
17
        $individuals = Individual::findOrFail($father_id);
18
        $individuals->parents()->attach($individual_list);
19
20
        $individuals = Individual::findOrFail($mother_id);
21
        $individuals->parents()->attach($individual_list);
22
        Individual::find($mother_id)->families()->attach($family->id, ['type_id' => 2]);
23
24
        $father = Individual::select('id', 'first_name', 'last_name')->where('id', '=', $father_id)->first();
25
        $mother = Individual::select('id', 'first_name', 'last_name')->where('id', '=', $mother_id)->first();
26
27
        $father_full_name = $father->first_name.' '.$father->last_name;
28
        $mother_full_name = $mother->first_name.' '.$mother->last_name;
29
30
        $father_person = Person::where('name', '=', $father_full_name)
31
            ->first();
32
        $mother_person = Person::where('name', '=', $mother_full_name)
33
            ->first();
34
35
        foreach ($individual_list as $individual) {
36
            $individual = Individual::find($individual);
37
            $individual_full_name = $individual->first_name.' '.$individual->last_name;
38
            $person = Person::select('id', 'name')->where('name', '=', $individual_full_name)
39
                ->first();
40
            $person->father()->associate($father_person);
41
            $person->save();
42
            $person = Person::select('id', 'name')->where('name', '=', $individual_full_name)
43
                ->first();
44
            $person->mother()->associate($mother_person);
45
            $person->save();
46
        }
47
    }
48
49
    public function create(FamilyForm $form)
50
    {
51
        return ['form' => $form->create()];
52
    }
53
54
    public function store(ValidateFamilyRequest $request, Family $family)
55
    {
56
        $family = $family->storeWithIndividuals(
57
            $request->all(),
58
            $request->get('individualList')
59
        );
60
61
        $individuals = $request->get('individualList');
62
63
        $father_id = $request->get('father_id');
64
        $mother_id = $request->get('mother_id');
65
66
        $this->attach($family, $father_id, $mother_id, $individuals);
67
68
        return [
69
            'message'  => __('The Family was successfully created'),
70
            'redirect' => 'families.edit',
71
            'id'       => $family->id,
72
        ];
73
    }
74
75
    public function show(Family $family)
76
    {
77
        return $family->individuals()->get(['individuals.id', 'individuals.first_name', 'individuals.last_name']);
78
    }
79
80
    public function edit(Family $family, FamilyForm $form)
81
    {
82
        return ['form' => $form->edit($family)];
83
    }
84
85
    public function update(ValidateFamilyRequest $request, Family $family)
86
    {
87
        $family->updateWithIndividuals(
88
            $request->all(),
89
            $request->get('individualList')
90
        );
91
92
        $individuals = $request->get('individualList');
93
94
        $father_id = $request->get('father_id');
95
        $mother_id = $request->get('mother_id');
96
97
        $this->attach($family, $father_id, $mother_id, $individuals);
98
99
        return ['message' => __('The Family was successfully updated')];
100
    }
101
102
    public function destroy(Family $family)
103
    {
104
        $family->delete();
105
106
        return [
107
            'message'  => __('The Family was successfully deleted'),
108
            'redirect' => 'families.index',
109
        ];
110
    }
111
}
112