|
1
|
|
|
<?php namespace JobApis\JobsToMail\Jobs\Users; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Log; |
|
4
|
|
|
use JobApis\JobsToMail\Http\Messages\FlashMessage; |
|
5
|
|
|
use JobApis\JobsToMail\Models\User; |
|
6
|
|
|
use JobApis\JobsToMail\Repositories\Contracts\SearchRepositoryInterface; |
|
7
|
|
|
use JobApis\JobsToMail\Repositories\Contracts\UserRepositoryInterface; |
|
8
|
|
|
|
|
9
|
|
|
class CreateUserAndSearch |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array $data |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $data; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Create a new job instance. |
|
18
|
|
|
*/ |
|
19
|
4 |
|
public function __construct($data = []) |
|
20
|
|
|
{ |
|
21
|
4 |
|
$this->data = $data; |
|
22
|
4 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create a Search for new or existing User |
|
26
|
|
|
* |
|
27
|
|
|
* @param UserRepositoryInterface $users |
|
28
|
|
|
* @param SearchRepositoryInterface $searches |
|
29
|
|
|
* |
|
30
|
|
|
* @return FlashMessage |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function handle( |
|
33
|
|
|
UserRepositoryInterface $users, |
|
34
|
|
|
SearchRepositoryInterface $searches |
|
35
|
|
|
): FlashMessage { |
|
36
|
|
|
try { |
|
37
|
4 |
|
return $this->createUserAndSearch($users, $searches); |
|
38
|
1 |
|
} catch (\Exception $e) { |
|
39
|
1 |
|
Log::error($e->getMessage()); |
|
40
|
1 |
|
return $this->flashDanger( |
|
41
|
1 |
|
'Something went wrong and your job search was not saved. Please try again.' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create or get the user and search term |
|
48
|
|
|
* |
|
49
|
|
|
* @param UserRepositoryInterface $users |
|
50
|
|
|
* @param SearchRepositoryInterface $searches |
|
51
|
|
|
* |
|
52
|
|
|
* @return FlashMessage |
|
53
|
|
|
*/ |
|
54
|
4 |
|
private function createUserAndSearch( |
|
55
|
|
|
UserRepositoryInterface $users, |
|
56
|
|
|
SearchRepositoryInterface $searches |
|
57
|
|
|
): FlashMessage { |
|
58
|
4 |
|
$user = $users->firstOrCreate($this->data); |
|
59
|
|
|
|
|
60
|
3 |
|
if (!$this->userAtOrOverMaximum($user)) { |
|
61
|
2 |
|
return $this->createSearchForUser($user, $searches); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$adminEmail = config('mail.from.address'); |
|
65
|
|
|
|
|
66
|
1 |
|
return $this->flashDanger("You have reached your maximum number of |
|
67
|
1 |
|
{$user->max_searches} job searches. Unsubscribe from a search |
|
68
|
1 |
|
or contact {$adminEmail} to upgrade your account."); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Creates a search for a new or existing user |
|
73
|
|
|
* |
|
74
|
|
|
* @param User $user |
|
75
|
|
|
* @param SearchRepositoryInterface $searches |
|
76
|
|
|
* |
|
77
|
|
|
* @return FlashMessage |
|
78
|
|
|
*/ |
|
79
|
2 |
|
private function createSearchForUser( |
|
80
|
|
|
User $user, |
|
81
|
|
|
SearchRepositoryInterface $searches |
|
82
|
|
|
): FlashMessage { |
|
83
|
2 |
|
$searches->create($user->id, $this->data); |
|
84
|
|
|
|
|
85
|
2 |
|
if ($user->existed === true) { |
|
86
|
1 |
|
return $this->flashSuccess('A new search has been created for your |
|
87
|
|
|
account. You should start receiving jobs within 24 hours.'); |
|
88
|
|
|
} |
|
89
|
1 |
|
return $this->flashSuccess('A confirmation email has been sent. Once |
|
90
|
|
|
confirmed, you should start receiving jobs within 24 hours.'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Determine whether or not this user has reached the max number of allowed searches |
|
95
|
|
|
* |
|
96
|
|
|
* @param User $user |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
3 |
|
private function userAtOrOverMaximum(User $user): bool |
|
100
|
|
|
{ |
|
101
|
3 |
|
return $user->existed === true && $user->searches()->count() >= $user->max_searches; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Returns a danger flash message |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $message |
|
108
|
|
|
* @return FlashMessage |
|
109
|
|
|
*/ |
|
110
|
2 |
|
private function flashDanger(string $message): FlashMessage |
|
111
|
|
|
{ |
|
112
|
2 |
|
return new FlashMessage('alert-danger', $message); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns a success flash message |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $message |
|
119
|
|
|
* @return FlashMessage |
|
120
|
|
|
*/ |
|
121
|
2 |
|
private function flashSuccess(string $message): FlashMessage |
|
122
|
|
|
{ |
|
123
|
2 |
|
return new FlashMessage('alert-success', $message); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|