Completed
Push — master ( 85818a...53d0e1 )
by Mahmoud
03:23
created

AgentAuthentication   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 1
cbo 5
dl 0
loc 76
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C handle() 0 30 7
1
<?php
2
3
namespace App\Containers\APIAuthentication\Middlewares;
4
5
use App\Containers\ApiAuthentication\Exceptions\AuthenticationFailedException;
6
use App\Containers\ApiAuthentication\Exceptions\MissingAgentIdException;
7
use App\Containers\User\Actions\RegisterAgentUserAction;
8
use Closure;
9
use Illuminate\Foundation\Application;
10
use Jenssegers\Agent\Agent;
11
12
/**
13
 * Class AgentAuthentication
14
 *
15
 * @author  Mahmoud Zalt  <[email protected]>
16
 */
17
class AgentAuthentication
18
{
19
20
    /**
21
     * @var  \Jenssegers\Agent\Agent
22
     */
23
    private $agent;
24
25
    /**
26
     * @var  \Illuminate\Foundation\Application
27
     */
28
    private $app;
29
30
    /**
31
     * @var  \App\Containers\User\Actions\RegisterAgentUserAction
32
     */
33
    private $RegisterAgentUserAction;
34
35
    /**
36
     * AgentAuthentication constructor.
37
     *
38
     * @param \Illuminate\Foundation\Application                   $app
39
     * @param \Jenssegers\Agent\Agent                              $agent
40
     * @param \App\Containers\User\Actions\RegisterAgentUserAction $RegisterAgentUserAction
41
     */
42
    public function __construct(
43
        Application $app,
44
        Agent $agent,
45
        RegisterAgentUserAction $RegisterAgentUserAction
46
    ) {
47
        $this->app = $app;
48
        $this->agent = $agent;
49
        $this->RegisterAgentUserAction = $RegisterAgentUserAction;
50
    }
51
52
53
    /**
54
     * Whenever the request doesn't have an Authorization header (token)
55
     * it must have a an Agent-Id header.
56
     *
57
     * @param  \Illuminate\Http\Request $request
58
     * @param  \Closure                 $next
59
     *
60
     * @return mixed
61
     */
62
    public function handle($request, Closure $next)
63
    {
64
        if ($this->agent->isMobile() || $this->agent->isPhone() || $this->agent->isTablet()) {
65
66
            $token = $request->header('Authorization');
67
68
            if (!$token) {
69
                // read the agent ID header (set by the API users)
70
                $agentId = $request->header('Agent-Id');
71
72
                if (!$agentId) {
73
                    throw new MissingAgentIdException();
74
                }
75
76
                $device = $this->agent->device();
77
                $platform = $this->agent->platform();
78
79
                $user = $this->RegisterAgentUserAction->run($agentId, $device, $platform);
80
81
                if (!$user) {
82
                    throw new AuthenticationFailedException(
83
                        'Something went wrong while trying to create user from the Agent ID: ' . $agentId
84
                    );
85
                }
86
            }
87
        }
88
89
        // return the response
90
        return $next($request);
91
    }
92
}
93