Completed
Pull Request — master (#11)
by Karl
02:49
created

CreateUserAndSearch::handle()   B

Complexity

Conditions 3
Paths 6

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.8571
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 3
eloc 18
nc 6
nop 2
crap 3
1
<?php namespace JobApis\JobsToMail\Jobs;
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 3
    public function __construct($data = [])
19
    {
20 3
        $this->data = $data;
21 3
    }
22
23
    /**
24
     * Create a Search for new or existing User
25
     *
26
     * @param UserRepositoryInterface $users
27
     *
28
     * @return FlashMessage
29
     */
30 3
    public function handle(
31
        UserRepositoryInterface $users,
32
        SearchRepositoryInterface $searches
33
    ) {
34
        try {
35
            // Get or create the user
36 3
            $user = $users->firstOrCreate($this->data);
37
38
            // Create a new search for this user
39 2
            $searches->create($user->id, $this->data);
40
41
            // User already existed
42 2
            if ($user->existed === true) {
43
                // User is new to our system
44 1
                return new FlashMessage(
45 1
                    'alert-success',
46
                    'A new search has been created for your account.
47 1
                    you should start receiving jobs within 24 hours.'
48
                );
49
            }
50
            // User is new to our system
51 1
            return new FlashMessage(
52 1
                'alert-success',
53
                'A confirmation email has been sent. 
54 1
                        Once confirmed, you should start receiving jobs within 24 hours.'
55
            );
56 1
        } catch (\Exception $e) {
57
            // Log the error and let the user know something went wrong
58 1
            Log::error($e->getMessage());
59 1
            return new FlashMessage(
60 1
                'alert-danger',
61
                'Something went wrong and your job search was not saved.
62 1
                    Please try again.'
63
            );
64
        }
65
    }
66
}
67