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

ValidateConfirmationCodeTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\Email\Tasks;
4
5
use App\Containers\Email\Exceptions\InvalidConfirmationCodeException;
6
use App\Port\Task\Abstracts\Task;
7
use Illuminate\Cache\Repository as Cache;
8
9
/**
10
 * Class ValidateConfirmationCodeTask.
11
 *
12
 * @author Mahmoud Zalt <[email protected]>
13
 */
14
class ValidateConfirmationCodeTask extends Task
15
{
16
17
    /**
18
     * @var  \Illuminate\Cache\Repository
19
     */
20
    private $cache;
21
22
    /**
23
     * ValidateConfirmationCodeTask constructor.
24
     *
25
     * @param \Illuminate\Cache\Repository $cache
26
     */
27
    public function __construct(Cache $cache)
28
    {
29
        $this->cache = $cache;
30
    }
31
32
    /**
33
     * @param $userId
34
     * @param $code
35
     *
36
     * @return  bool
37
     */
38
    public function run($userId, $code)
39
    {
40
        // find the confirmation code of this user from the cache
41
        $codeFromCache = $this->cache->get('user:email-confirmation-code:' . $userId);
42
43
        // if code is valid
44
        if (!$codeFromCache && $codeFromCache != $code) {
45
            throw new InvalidConfirmationCodeException;
46
        }
47
48
        // remove the confirmation code from the cache
49
        $this->cache->forget('user:email-confirmation-code:' . $userId);
50
51
        return true;
52
    }
53
}
54