UserProfileRepository::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Models\UserProfile;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Carbon;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Date;
10
11
class UserProfileRepository implements UserProfileRepositoryInterface
12
{
13
14
    /**
15
     * Get user profile by id
16
     *
17
     * @param int $id
18
     * @return UserProfile
19
     */
20 1
    public function getById(int $id): UserProfile
21
    {
22 1
        return UserProfile::findOrFail($id);
23
    }
24
25
    /**
26
     * Store UserProfile
27
     *
28
     * @param array $data
29
     * @return UserProfile
30
     */
31 1
    public function store(array $data): UserProfile
32
    {
33 1
        $userProfile = new UserProfile();
34 1
        $testimonial = self::assignDataAttributes($userProfile, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\UserPro...:assignDataAttributes() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        /** @scrutinizer ignore-call */ 
35
        $testimonial = self::assignDataAttributes($userProfile, $data);
Loading history...
Unused Code introduced by
The assignment to $testimonial is dead and can be removed.
Loading history...
35
36 1
        $userProfile->save();
37
38 1
        return $userProfile->fresh();
39
    }
40
41
    /**
42
     * Update UserProfile
43
     *
44
     * @param array $data
45
     * @param int $userProfileId
46
     *
47
     * @return UserProfile
48
     */
49 1
    public function update(array $data, int $userProfileId): UserProfile
50
    {
51 1
        $userProfile = $this->getById($userProfileId);
52 1
        $userProfile = self::assignDataAttributes($userProfile, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method App\Repositories\UserPro...:assignDataAttributes() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        /** @scrutinizer ignore-call */ 
53
        $userProfile = self::assignDataAttributes($userProfile, $data);
Loading history...
53
54 1
        $userProfile->update();
55
        //$this->updateUserStatus($userProfile, $data['status'] ?? null);
56
57 1
        return $userProfile;
58
    }
59
60
    /**
61
     * Delete UserProfile
62
     *
63
     * @param int $id
64
     * @return void
65
     */
66
    public function delete(int $id): void
67
    {
68
        UserProfile::destroy($id);
69
    }
70
71
    /**
72
     * Assign the attributes of the data array to the object
73
     *
74
     * @param \App\Models\UserProfile $userProfile
75
     * @param array $data
76
     *
77
     * @return \App\Models\UserProfile
78
     */
79 2
    public function assignDataAttributes(UserProfile $userProfile, array $data): UserProfile
80
    {
81 2
        $userProfile->user_id = $data['user_id'] ?? $userProfile->user->id;
0 ignored issues
show
Bug introduced by
The property user_id does not exist on App\Models\UserProfile. Did you mean user?
Loading history...
82 2
        $userProfile->name = $data['name'];
83 2
        $userProfile->surname = $data['surname'];
84 2
        $userProfile->country_id = $data['country_id'];
0 ignored issues
show
Bug introduced by
The property country_id does not exist on App\Models\UserProfile. Did you mean country?
Loading history...
85 2
        $userProfile->description = $data['description'] ?? '';
86 2
        $userProfile->accept_terms = ($data['accept_terms'] == 'on') ? 1 : 0;
87
88 2
        return $userProfile;
89
    }
90
91
    /**
92
     * Update the user status
93
     *
94
     * @param UserProfile $userProfile
95
     * @param string|null $status
96
     *
97
     * @return void
98
     */
99
    public function updateUserStatus(UserProfile $userProfile, ?string $status): void
100
    {
101
        // If the status dropdown has been modified change the status according to that
102
        if (isset($status) && $userProfile->user->status != $status) {
103
            $userProfile->user->setStatus($status);
104
        }
105
106
        // Otherwise set the status to update if any field has been modified
107
        elseif ($userProfile->wasChanged()) {
108
            $userProfile->user->setStatus('updated', Auth::id());
109
        }
110
    }
111
112
    /**
113
     * Update the user phone verify at field
114
     *
115
     * @param UserProfile $userProfile
116
     * @param Request $request
117
     *
118
     * @return bool
119
     */
120
    public function updateUserPhoneVerifyAt(UserProfile $userProfile, Request $request): bool
121
    {
122
        if (isset($request->phone_verification_code)) {
123
            if ($userProfile->phone_verification_code == $request->phone_verification_code) {
0 ignored issues
show
Bug introduced by
The property phone_verification_code does not seem to exist on App\Models\UserProfile. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
124
                $userProfile->phone_verified_at = Carbon::now();
0 ignored issues
show
Bug introduced by
The property phone_verified_at does not seem to exist on App\Models\UserProfile. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
125
                $userProfile->update();
126
                return true;
127
            }
128
        }
129
        return false;
130
    }
131
}
132