Completed
Push — master ( 5e51ce...883933 )
by Freek
01:51
created

DemoMode::hasDemoAccess()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace Spatie\DemoMode;
4
5
use Closure;
6
use Illuminate\Config\Repository;
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Http\Request;
9
10
class DemoMode
11
{
12
    /**
13
     * @var array
14
     */
15
    private $config;
16
17
    public function __construct(Repository $config)
18
    {
19
        $this->config = $config->get('laravel-demo-mode');
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('laravel-demo-mode') of type * is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
20
    }
21
22
    /**
23
     * Handle an incoming request.
24
     *
25
     * @param \Illuminate\Http\Request $request
26
     * @param \Closure                 $next
27
     *
28
     * @return mixed
29
     */
30
    public function handle($request, Closure $next)
31
    {
32
        if ($this->protectedByDemoMode($request)) {
33
            if (!$this->hasDemoAccess($request)) {
34
                return new RedirectResponse($this->config['redirect_unauthorized_users_to_url']);
35
            }
36
        }
37
38
        return $next($request);
39
    }
40
41
    protected function protectedByDemoMode(Request $request) : bool
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        return true;
44
    }
45
46
    protected function hasDemoAccess(Request $request) : bool
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
        if (session()->has('demo_access_granted')) {
49
            return true;
50
        }
51
52
        if (auth()->user()) {
0 ignored issues
show
Bug introduced by
The method user() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            return true;
54
        }
55
56
        return false;
57
    }
58
}
59