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\Repositories\Contracts\SearchRepositoryInterface; |
6
|
|
|
use JobApis\JobsToMail\Repositories\Contracts\UserRepositoryInterface; |
7
|
|
|
|
8
|
|
|
class CreateUserAndSearch |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array $data |
12
|
|
|
*/ |
13
|
|
|
protected $data; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Create a new job instance. |
17
|
|
|
*/ |
18
|
4 |
|
public function __construct($data = []) |
19
|
|
|
{ |
20
|
4 |
|
$this->data = $data; |
21
|
4 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a Search for new or existing User |
25
|
|
|
* |
26
|
|
|
* @param UserRepositoryInterface $users |
27
|
|
|
* |
28
|
|
|
* @return FlashMessage |
29
|
|
|
*/ |
30
|
4 |
|
public function handle( |
31
|
|
|
UserRepositoryInterface $users, |
32
|
|
|
SearchRepositoryInterface $searches |
33
|
|
|
) { |
34
|
|
|
try { |
35
|
|
|
// Get or create the user |
36
|
4 |
|
$user = $users->firstOrCreate($this->data); |
37
|
|
|
|
38
|
|
|
// Make sure the user isn't over their maximum |
39
|
3 |
|
$maxSearches = config('app.max_searches'); |
40
|
3 |
|
$adminEmail = env('ADMIN_EMAIL'); |
41
|
|
|
|
42
|
3 |
|
if ($user->existed === true && $user->searches()->count() >= $maxSearches) { |
43
|
1 |
|
return new FlashMessage( |
44
|
1 |
|
'alert-danger', |
45
|
1 |
|
"You have reached your maximum number of {$maxSearches} |
46
|
|
|
job searches. Unsubscribe from a search or contact |
47
|
1 |
|
{$adminEmail} to upgrade your account." |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Create a new search for this user |
52
|
2 |
|
$searches->create($user->id, $this->data); |
53
|
|
|
|
54
|
|
|
// User already existed |
55
|
2 |
|
if ($user->existed === true) { |
56
|
|
|
// User is new to our system |
57
|
1 |
|
return new FlashMessage( |
58
|
1 |
|
'alert-success', |
59
|
|
|
'A new search has been created for your account. |
60
|
1 |
|
you should start receiving jobs within 24 hours.' |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
// User is new to our system |
64
|
1 |
|
return new FlashMessage( |
65
|
1 |
|
'alert-success', |
66
|
|
|
'A confirmation email has been sent. |
67
|
1 |
|
Once confirmed, you should start receiving jobs within 24 hours.' |
68
|
|
|
); |
69
|
1 |
|
} catch (\Exception $e) { |
70
|
|
|
// Log the error and let the user know something went wrong |
71
|
1 |
|
Log::error($e->getMessage()); |
72
|
1 |
|
return new FlashMessage( |
73
|
1 |
|
'alert-danger', |
74
|
|
|
'Something went wrong and your job search was not saved. |
75
|
1 |
|
Please try again.' |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|