Completed
Push — master ( 063bdc...995895 )
by Mahmoud
04:00
created

VisitorsAuthentication   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B handle() 0 27 4
1
<?php
2
3
namespace App\Containers\APIAuthentication\Middlewares;
4
5
use App\Containers\ApiAuthentication\Exceptions\AuthenticationFailedException;
6
use App\Containers\ApiAuthentication\Exceptions\MissingVisitorIdException;
7
use App\Containers\User\Actions\RegisterVisitorUserAction;
8
use Closure;
9
use Illuminate\Foundation\Application;
10
use Jenssegers\Agent\Agent;
11
12
/**
13
 * Class VisitorsAuthentication
14
 *
15
 * @author  Mahmoud Zalt  <[email protected]>
16
 */
17
class VisitorsAuthentication
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\RegisterVisitorUserAction
32
     */
33
    private $RegisterVisitorUserAction;
34
35
    /**
36
     * VisitorsAuthentication constructor.
37
     *
38
     * @param \Illuminate\Foundation\Application                   $app
39
     * @param \Jenssegers\Agent\Agent                              $agent
40
     * @param \App\Containers\User\Actions\RegisterVisitorUserAction $RegisterVisitorUserAction
41
     */
42
    public function __construct(
43
        Application $app,
44
        Agent $agent,
45
        RegisterVisitorUserAction $RegisterVisitorUserAction
46
    ) {
47
        $this->app = $app;
48
        $this->agent = $agent;
49
        $this->RegisterVisitorUserAction = $RegisterVisitorUserAction;
50
    }
51
52
53
    /**
54
     * Whenever the request doesn't have an Authorization header (token)
55
     * it must have a an Visitor-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
        $token = $request->header('Authorization');
65
66
        if (!$token) {
67
            // read the visitor ID header (set by the API users)
68
            $visitorId = $request->header('Visitor-Id');
69
70
            if (!$visitorId) {
71
                throw new MissingVisitorIdException();
72
            }
73
74
            $device = $this->agent->device();
75
            $platform = $this->agent->platform();
76
77
            $user = $this->RegisterVisitorUserAction->run($visitorId, $device, $platform);
78
79
            if (!$user) {
80
                throw new AuthenticationFailedException(
81
                    'Something went wrong while trying to create user from the Visitor ID: ' . $visitorId
82
                );
83
            }
84
        }
85
86
        // return the response
87
        return $next($request);
88
    }
89
}
90