Completed
Push — develop ( 335c93...e4583a )
by Mohamed
12:58
created

CheckForMaintenanceMode::handle()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6.5625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 6
cts 8
cp 0.75
rs 8.8571
cc 6
eloc 8
nc 3
nop 2
crap 6.5625
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\Middleware;
13
14
use Closure;
15
use Illuminate\Contracts\Auth\Guard;
16
use Illuminate\Contracts\Foundation\Application;
17
use Symfony\Component\HttpKernel\Exception\HttpException;
18
19
/**
20
 * @author Mohamed Alsharaf <[email protected]>
21
 */
22
class CheckForMaintenanceMode
23
{
24
    /**
25
     * The Guard implementation.
26
     *
27
     * @var Guard
28
     */
29
    protected $auth;
30
31
    /**
32
     * The application implementation.
33
     *
34
     * @var \Illuminate\Contracts\Foundation\Application
35
     */
36
    protected $app;
37
38
    /**
39
     * Create a new filter instance.
40
     *
41
     * @param Guard                                        $auth
42
     * @param \Illuminate\Contracts\Foundation\Application $app
43
     */
44 61
    public function __construct(Guard $auth, Application $app)
45
    {
46 61
        $this->auth = $auth;
47 61
        $this->app  = $app;
48 61
    }
49
50
    /**
51
     * Handle an incoming request.
52
     *
53
     * @param \Illuminate\Http\Request $request
54
     * @param \Closure                 $next
55
     *
56
     * @return mixed
57
     *
58
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
59
     */
60 61
    public function handle($request, Closure $next)
61
    {
62 61
        $siteDown = $this->app->isDownForMaintenance();
63 61
        $isLogin  = $request->is('/', 'logout', 'signin');
64
65
        // Allow admin & login page to always view the site event in maintenance mode
66 61
        if ($siteDown && !$isLogin && ($this->auth->guest() || !$this->auth->user()->isAdmin())) {
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 isAdmin() 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...
67
            throw new HttpException(503);
68
        }
69
70
        // Show message to administrator
71 61
        if ($siteDown) {
72
            $this->app['session']->flash('notice-error', trans('tinyissue.site_maintenance_message'));
73
        }
74
75 61
        return $next($request);
76
    }
77
}
78