Completed
Push — master ( 71feda...776f21 )
by claudio
06:53
created

AuthController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 38.71%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 97
ccs 12
cts 31
cp 0.3871
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A postRegister() 0 6 1
A postLogin() 0 17 2
A validator() 0 8 1
A create() 0 8 1
1
<?php
2
3
namespace plunner\Http\Controllers\Employees\Auth;
4
5
use Illuminate\Http\Request;
6
use Log;
7
use plunner\Company;
8
use plunner\employee;
9
use plunner\Http\Controllers\Controller;
10
use Tymon\JWTAuth\Support\auth\AuthenticatesAndRegistersUsers;
11
use Tymon\JWTAuth\Support\auth\ThrottlesLogins;
12
use Validator;
13
14
/**
15
 * Class AuthController
16
 * @package plunner\Http\Controllers\Employees\Auth
17
 * @author Claudio Cardinale <[email protected]>
18
 * @copyright 2015 Claudio Cardinale
19
 * @version 1.0.0
20
 */
21
class AuthController extends Controller
22
{
23
    /*
24
    |--------------------------------------------------------------------------
25
    | Registration & Login Controller
26
    |--------------------------------------------------------------------------
27
    |
28
    | This controller handles the registration of new users, as well as the
29
    | authentication of existing users. By default, this controller uses
30
    | a simple trait to add these behaviors. Why don't you explore it?
31
    |
32
    */
33
34
    use AuthenticatesAndRegistersUsers {
35
        postRegister as postRegisterOriginal;
36
        postLogin as postLoginOriginal;
37
    }
38
    use ThrottlesLogins;
39
40
    protected $redirectPath = "/";
41
42
    /**
43
     * en = employee normal
44
     * @var array
45
     */
46
    protected $custom = ['mode' => 'en'];
47
48
    /**
49
     * @var company
50
     */
51
    private $company = null;
52
53
    /**
54
     * Create a new authentication controller instance.
55
     *
56
     */
57 10
    public function __construct()
58
    {
59 10
        config(['auth.model' => \plunner\Employee::class]);
60 10
        config(['jwt.user' => \plunner\Employee::class]);
61 10
    }
62
63
    public function postRegister(Request $request)
64
    {
65
        $this->validate($request, ['company' => 'required|exists:companies,name']);
66
        $this->company = Company::whereName($request->input('company'))->firstOrFail();
67
        return $this->postRegisterOriginal($request);
68
    }
69
70 10
    public function postLogin(Request $request)
71
    {
72
        //get company ID and impiled it in the request
73 10
        $this->validate($request, ['company' => 'required|exists:companies,name']);
74 10
        $this->company = Company::whereName($request->input('company'))->firstOrFail();
75 10
        $request->merge(['company_id' => $this->company->id]);
76
77
        //remember me
78 10
        $this->validate($request, ['remember' => 'boolean']);//TODO insert required
79 10
        if ($request->input('remember', false)) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
            config(['jwt.ttl' => '43200']); //30 days
81
            $this->custom = array_merge($this->custom, ['remember' => 'true']);
82
        } else
83 10
            $this->custom = array_merge($this->custom, ['remember' => 'false']);
84
85 10
        return $this->postLoginOriginal($request);
86
    }
87
88
    /**
89
     * Get a validator for an incoming registration request.
90
     *
91
     * @param  array $data
92
     * @return \Illuminate\Contracts\Validation\Validator
93
     */
94
    protected function validator(array $data)
95
    {
96
        return Validator::make($data, [
97
            'name' => 'required|min:1|max:255',
98
            'email' => 'required|email|max:255|unique:employees,email,NULL,id,company_id,' . $this->company->id,
99
            'password' => 'required|confirmed|min:6',
100
        ]);
101
    }
102
103
    /**
104
     * Create a new user instance after a valid registration.
105
     *
106
     * @param  array $data
107
     * @return Company
108
     */
109
    protected function create(array $data)
110
    {
111
        return $this->company->save(new employee([
112
            'name' => $data['name'],
113
            'email' => $data['email'],
114
            'password' => bcrypt($data['password']),
115
        ]));
116
    }
117
}
118