Completed
Push — develop ( 35ebd6...f6916d )
by Mohamed
13:11 queued 06:49
created

HomeController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 80%

Importance

Changes 7
Bugs 3 Features 1
Metric Value
wmc 11
c 7
b 3
f 1
lcom 1
cbo 7
dl 0
loc 94
ccs 28
cts 35
cp 0.8
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIssues() 0 8 1
A getIndex() 0 8 2
A getLogout() 0 6 1
A getDashboard() 0 6 1
B postSignin() 0 27 6
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Http\Controllers;
13
14
use Auth as Auth;
15
use Lang;
16
use Tinyissue\Form\Login as LoginForm;
17
use Tinyissue\Http\Requests\FormRequest;
18
use Tinyissue\Model\Project;
19
use Tinyissue\Model\User;
20
21
/**
22
 * HomeController is the controller class for login, logout, dashboard pages.
23
 *
24
 * @author Mohamed Alsharaf <[email protected]>
25
 */
26
class HomeController extends Controller
27
{
28
    /**
29
     * Public issues view.
30
     *
31
     * @param User    $user
32
     * @param Project $project
33
     *
34
     * @return \Illuminate\View\View
35
     */
36 4
    public function getIssues(User $user, Project $project)
37
    {
38 4
        return view('index.issues', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('index.issues', arr...sidebar' => 'public')); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 38 which is incompatible with the return type documented by Tinyissue\Http\Controlle...meController::getIssues of type Illuminate\View\View.
Loading history...
39 4
            'activeUsers' => $user->activeUsers(),
40 4
            'projects'    => $project->projectsWidthIssues(Project::STATUS_OPEN, Project::PRIVATE_NO)->get(),
41 4
            'sidebar'     => 'public',
42 4
        ]);
43
    }
44
45
    /**
46
     * User dashboard.
47
     *
48
     * @return \Illuminate\View\View
49
     */
50 10
    public function getDashboard()
51
    {
52 10
        return view('index.dashboard', [
0 ignored issues
show
Bug Compatibility introduced by
The expression view('index.dashboard', ...:STATUS_OPEN)->get())); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 52 which is incompatible with the return type documented by Tinyissue\Http\Controlle...ontroller::getDashboard of type Illuminate\View\View.
Loading history...
53 10
            'projects' => $this->auth->user()->projectsWidthActivities(Project::STATUS_OPEN)->get(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method projectsWidthActivities() does only exist in the following implementations of said interface: Tinyissue\Model\User.

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...
54 10
        ]);
55
    }
56
57
    /**
58
     * Logout user and redirect to login page.
59
     *
60
     * @return \Illuminate\Http\RedirectResponse
61
     */
62 1
    public function getLogout()
63
    {
64 1
        $this->auth->logout();
65
66 1
        return redirect('/')->with('message', trans('tinyissue.loggedout'));
67
    }
68
69
    /**
70
     * Login page.
71
     *
72
     * @param LoginForm $form
73
     *
74
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
75
     */
76 16
    public function getIndex(LoginForm $form)
77
    {
78 16
        if ($this->auth->user()) {
79 3
            return redirect('dashboard');
80
        }
81
82 13
        return view('user.login', ['form' => $form]);
83
    }
84
85
    /**
86
     * Attempt to log user in or redirect to login page with error.
87
     *
88
     * @param FormRequest\Login $request
89
     *
90
     * @return \Illuminate\Http\RedirectResponse
91
     */
92 8
    public function postSignin(FormRequest\Login $request)
93
    {
94 8
        $credentials = $request->only('email', 'password');
95
96 8
        if ($this->auth->attempt($credentials, $request->has('remember'))
97 8
            && $this->auth->user()->isActive()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method isActive() does only exist in the following implementations of said interface: Tinyissue\Model\User.

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...
98 8
        ) {
99 7
            return redirect()->to('/dashboard');
100
        }
101
102
        // Get error message
103 1
        $errorMessage = 'password_incorrect';
104 1
        if (!$this->auth->guest()) {
105
            if ($this->auth->user()->isInactive()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method isInactive() does only exist in the following implementations of said interface: Tinyissue\Model\User.

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...
106
                $errorMessage = 'user_is_not_active';
107
            } elseif ($this->auth->user()->isBlocked()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Auth\Authenticatable as the method isBlocked() does only exist in the following implementations of said interface: Tinyissue\Model\User.

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...
108
                $errorMessage = 'user_is_blocked';
109
            }
110
111
            // Logged out
112
            $this->auth->logout();
113
        }
114
115 1
        return redirect('/')
116 1
            ->withInput($request->only('email'))
117 1
            ->with('notice-error', trans('tinyissue.' . $errorMessage));
118
    }
119
}
120