Passed
Push — task/applicant-profile-api ( bae4f4 )
by Yonathan
05:49
created

ApplicantController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 1
b 0
f 0
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateProfile() 0 22 2
A getProfile() 0 4 1
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Requests\UpdateApplicantProfile;
7
use App\Http\Resources\ApplicantProfile as ApplicantProfileResource;
8
use App\Models\Applicant;
9
use Illuminate\Support\Facades\Log;
10
11
class ApplicantController extends Controller
12
{
13
14
    /**
15
     * Retrieve Applicant profile.
16
     *
17
     * @param Applicant $applicant Incoming Applicant object.
18
     *
19
     * @return mixed
20
     */
21
    public function getProfile(Applicant $applicant)
22
    {
23
          $applicant->loadMissing('classifications');
24
          return new ApplicantProfileResource($applicant);
25
    }
26
27
    /**
28
     * Update Applicant profile.
29
     *
30
     * @param UpdateApplicantProfile $request Form Validation casted request object.
31
     * @param Applicant              $applicant Incoming Applicant object.
32
     */
33
    public function updateProfile(UpdateApplicantProfile $request, Applicant $applicant)
34
    {
35
        $data = $request->validated();
36
        // Create associated list of classifications with meta data.
37
        // Then sync new classifications to applicant, which removes all associations from the intermediate table
38
        // that are not in validatedClassifications.
39
        $validatedClassifications = [];
40
        foreach ($data['classifications'] as $classification) {
41
            $validatedClassifications[$classification['id']] = [
42
                'level' => $classification['level'],
43
                'order' => $classification['order'],
44
            ];
45
        }
46
        $applicant->classifications()->sync($validatedClassifications, true);
47
        $applicant->citizenship_declaration_id = $data['citizenship_declaration_id'];
48
        $applicant->veteran_status_id = $data['veteran_status_id'];
49
        $applicant->save();
50
51
        $applicant->fresh();
52
        $applicant->loadMissing('classifications');
53
54
        return new ApplicantProfileResource($applicant);
55
    }
56
}
57