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); |
|
|
|
|
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); |
|
|
|
|
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; |
|
|
|
|
82
|
2 |
|
$userProfile->name = $data['name']; |
83
|
2 |
|
$userProfile->surname = $data['surname']; |
84
|
2 |
|
$userProfile->country_id = $data['country_id']; |
|
|
|
|
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) { |
|
|
|
|
124
|
|
|
$userProfile->phone_verified_at = Carbon::now(); |
|
|
|
|
125
|
|
|
$userProfile->update(); |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|