Completed
Push — master ( e93f60...2f787f )
by Mahmoud
08:56 queued 02:41
created

ApiLogoutTask::__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\Authentication\Tasks;
4
5
use App\Containers\Authentication\Adapters\JwtAuthAdapter;
6
use App\Containers\Authentication\Exceptions\MissingTokenException;
7
use App\Port\Task\Abstracts\Task;
8
9
/**
10
 * Class ApiLogoutTask.
11
 *
12
 * @author Mahmoud Zalt <[email protected]>
13
 */
14
class ApiLogoutTask extends Task
15
{
16
17
    /**
18
     * @var \App\Containers\Authentication\Adapters\JwtAuthAdapter
19
     */
20
    private $jwtAuthAdapter;
21
22
    /**
23
     * ApiLogoutTask constructor.
24
     *
25
     * @param \App\Containers\Authentication\Adapters\JwtAuthAdapter $jwtAuthAdapter
26
     */
27
    public function __construct(JwtAuthAdapter $jwtAuthAdapter)
28
    {
29
        $this->jwtAuthAdapter = $jwtAuthAdapter;
30
    }
31
32
    /**
33
     * @param $authorizationHeader
34
     *
35
     * @throws \App\Containers\Authentication\Tasks\MissingTokenException
36
     *
37
     * @return bool
38
     */
39
    public function run($authorizationHeader)
40
    {
41
        // remove the `Bearer` string from the header and keep only the token
42
        $token = str_replace('Bearer', '', $authorizationHeader);
43
44
        $ok = $this->jwtAuthAdapter->invalidate($token);
45
46
        if (!$ok) {
47
            throw new MissingTokenException();
48
        }
49
50
        return true;
51
    }
52
53
}
54