Completed
Push — master ( 857394...4794c5 )
by Mahmoud
03:25
created

GenerateEmailConfirmationUrlTask::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace App\Containers\Email\Tasks;
4
5
use App\Containers\Email\Exceptions\UserEmailNotFoundException;
6
use App\Containers\User\Models\User;
7
use App\Port\Task\Abstracts\Task;
8
use Illuminate\Support\Facades\Cache;
9
use Illuminate\Support\Facades\URL;
10
11
/**
12
 * Class GenerateEmailConfirmationUrlTask.
13
 *
14
 * @author Mahmoud Zalt <[email protected]>
15
 */
16
class GenerateEmailConfirmationUrlTask extends Task
17
{
18
19
    /**
20
     * How long to keep the validation code on the memory (valid)
21
     *
22
     * default: 48 hours (2880 minutes)
23
     */
24
    const CONFIRMATION_CODE_VALIDATE_TIME = 2880;
25
26
    /**
27
     * @param $email
28
     * @param $password
29
     *
30
     * @return mixed
31
     */
32
    public function run(User $user)
33
    {
34
        // check if email exist on the user
35
        if (!$user->email) {
36
            throw new UserEmailNotFoundException;
37
        }
38
39
        // generate random unique token
40
        $confirmationCode = sha1(time() . $user->id);
41
42
        // 3. set token next to user id in the memory (redis)
43
        Cache::put('user:email-confirmation-code:' . $user->id, $confirmationCode,
44
            self::CONFIRMATION_CODE_VALIDATE_TIME);
45
46
        // set user status not confirmed (in case the user is updating his email)
47
        $user->confirmed = false;
48
        $user->save();
49
50
        // build the url email confirmation URL with the confirmation code and user id
51
        $confirmationUrl = URL::to('/') . '/users/' . $user->id . '/email/confirmation/' . $confirmationCode;
52
53
        return $confirmationUrl;
54
    }
55
}
56