AuthController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 11
dl 0
loc 105
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A index() 0 4 1
A login() 0 14 2
A redirectBackWithError() 0 10 2
A logout() 0 6 1
A loginWithFacebook() 0 5 1
A proceedFacebookLogin() 0 19 2
A proceedLogin() 0 8 2
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2016, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
namespace Angelov\Storgman\Members\Authentication\Http\Controllers;
29
30
use Angelov\Storgman\Core\Http\Controllers\BaseController;
31
use Angelov\Storgman\Members\Member;
32
use Angelov\Storgman\Members\Authentication\Http\Requests\LoginFormRequest;
33
use Angelov\Storgman\Members\SocialProfiles\Repositories\SocialProfilesRepositoryInterface;
34
use Illuminate\Contracts\Auth\Guard;
35
use Illuminate\Contracts\View\View;
36
use Illuminate\Http\RedirectResponse;
37
use Laravel\Socialite\Contracts\Factory as SocialiteFactory;
38
39
class AuthController extends BaseController
40
{
41
    protected $authenticator;
42
43
    public function __construct(Guard $authenticator)
44
    {
45
        $this->authenticator = $authenticator;
46
    }
47
48
    /**
49
     * Display the login form
50
     *
51
     * @return View
52
     */
53
    public function index()
54
    {
55
        return view('auth.index');
56
    }
57
58
    /**
59
     * Check the login data and authenticate the member.
60
     * Thanks to reddit.com/user/baileylo for the suggestions.
61
     *
62
     * @param LoginFormRequest $request
63
     * @return RedirectResponse
64
     */
65
    public function login(LoginFormRequest $request)
66
    {
67
        $credentials = $request->only('email', 'password');
68
        $remember = ($request->get('remember') == 'yes');
69
70
        if (!$this->authenticator->attempt($credentials, $remember)) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Guard as the method attempt() does only exist in the following implementations of said interface: Illuminate\Auth\SessionGuard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
71
            return $this->redirectBackWithError('Wrong email or password.');
72
        }
73
74
        /** @var Member $member */
75
        $member = $this->authenticator->user();
76
77
        return $this->proceedLogin($member);
78
    }
79
80
    /**
81
     * Return the user to the previous page and show an error
82
     *
83
     * @param string $error
84
     * @return RedirectResponse
85
     */
86
    protected function redirectBackWithError($error)
87
    {
88
        if ($this->authenticator->check()) {
89
            $this->authenticator->logout();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Guard as the method logout() does only exist in the following implementations of said interface: Illuminate\Auth\SessionGuard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
90
        }
91
92
        session()->flash('auth-error', $error);
93
94
        return redirect()->back()->withInput();
95
    }
96
97
    /**
98
     * Logout the member
99
     *
100
     * @return RedirectResponse
101
     */
102
    public function logout()
103
    {
104
        $this->authenticator->logout();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Guard as the method logout() does only exist in the following implementations of said interface: Illuminate\Auth\SessionGuard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
105
106
        return redirect()->route('auth');
107
    }
108
109
    public function loginWithFacebook(SocialiteFactory $socialite)
110
    {
111
        $fb = $socialite->driver('facebook');
112
        return $fb->redirect();
113
    }
114
115
    public function proceedFacebookLogin(SocialiteFactory $socialite, SocialProfilesRepositoryInterface $socialProfiles)
116
    {
117
        $fb = $socialite->driver('facebook');
118
119
        $profileId = $fb->user()->getId();
120
121
        $profile = $socialProfiles->getByProfileIdAndProvider($profileId, "facebook");
122
123
        if (!$profile) {
124
            session()->flash()->flash('auth-error', 'Have you connected your account with Facebook?');
125
            return redirect()->route('auth');
126
        }
127
128
        $member = $profile->getMember();
129
130
        $this->authenticator->login($member);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Guard as the method login() does only exist in the following implementations of said interface: Illuminate\Auth\SessionGuard.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
131
132
        return redirect()->route('homepage');
133
    }
134
135
    protected function proceedLogin(Member $member)
136
    {
137
        if (!$member->isApproved()) {
138
            return $this->redirectBackWithError('Your account is not approved yet.');
139
        }
140
141
        return redirect()->route('homepage');
142
    }
143
}
144