Completed
Push — master ( d66ddd...35a6f1 )
by Mahmoud
06:35
created

VisitorsAuthentication   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B handle() 0 28 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\Http\Request;
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  \App\Containers\User\Actions\RegisterVisitorUserAction
27
     */
28
    private $registerVisitorUserAction;
29
30
    /**
31
     * VisitorsAuthentication constructor.
32
     *
33
     * @param \Illuminate\Foundation\Application                     $app
0 ignored issues
show
Bug introduced by
There is no parameter named $app. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
34
     * @param \Jenssegers\Agent\Agent                                $agent
35
     * @param \App\Containers\User\Actions\RegisterVisitorUserAction $registerVisitorUserAction
36
     */
37
    public function __construct(
38
        Agent $agent,
39
        RegisterVisitorUserAction $registerVisitorUserAction
40
    ) {
41
        $this->agent = $agent;
42
        $this->registerVisitorUserAction = $registerVisitorUserAction;
43
    }
44
45
    /**
46
     * Whenever the request doesn't have an Authorization header (token)
47
     * it must have a an Visitor-Id header.
48
     *
49
     * @param  \Illuminate\Http\Request $request
50
     * @param  \Closure                 $next
51
     *
52
     * @return mixed
53
     */
54
    public function handle(Request $request, Closure $next)
55
    {
56
57
        $token = $request->header('Authorization');
58
59
        if (!$token) {
60
            // read the visitor ID header (set by the API users)
61
            $visitorId = $request->header('Visitor-Id');
62
63
            if (!$visitorId) {
64
                throw new MissingVisitorIdException();
65
            }
66
67
            $device = $this->agent->device();
68
            $platform = $this->agent->platform();
69
70
            $user = $this->registerVisitorUserAction->run($visitorId, $device, $platform);
71
72
            if (!$user) {
73
                throw new AuthenticationFailedException(
74
                    'Something went wrong while trying to create user from the "Visitor-Id": ' . $visitorId
75
                );
76
            }
77
        }
78
79
        // return the response
80
        return $next($request);
81
    }
82
}
83