Passed
Branch master (ffb5df)
by Michele
03:52
created

WebArtisanEnabled::handle()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 12
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 21
rs 8.4444
1
<?php
2
3
namespace Micheledamo\LaravelWebArtisan\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Micheledamo\LaravelWebArtisan\LaravelWebArtisan;
9
10
class WebArtisanEnabled
11
{
12
    /**
13
     * The WebArtisan instance
14
     *
15
     * @var LaravelWebArtisan
16
     */
17
    protected $webartisan;
18
19
    /**
20
     * Create a new middleware instance.
21
     *
22
     * @param  LaravelWebArtisan $webartisan
23
     */
24
    public function __construct(LaravelWebArtisan $webartisan)
25
    {
26
        $this->webartisan = $webartisan;
27
    }
28
29
    /**
30
     * Handle an incoming request.
31
     *
32
     * @param $request
33
     * @param Closure $next
34
     * @return Response|mixed
35
     */
36
    public function handle($request, Closure $next)
37
    {
38
        $response = $next($request);
39
        if ($response instanceof Response and $this->webartisan->isEnabled() and $request->url() != asset(config('webartisan.route_prefix').'/run')) {
40
            try {
41
                $response->getContent();
42
            }
43
            catch (\ErrorException $e) {
44
                return $response;
45
            }
46
47
            if(!$this->webartisan->useAuthentication()
48
                or ($this->webartisan->useAuthentication()
49
                    and $request->session()->has('webartisan__authenticated'))) {
50
51
                $this->webartisan->setAuthenticated();
52
            }
53
54
            $this->webartisan->render($response);
55
        }
56
        return $response;
57
    }
58
}