VerifyDeployRequest::handle()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.288

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 24
ccs 12
cts 15
cp 0.8
rs 8.5126
cc 6
eloc 15
nc 5
nop 2
crap 6.288
1
<?php
2
3
namespace Morphatic\AutoDeploy\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class VerifyDeployRequest
9
{
10
    protected $origins = [];
11
12 22
    public function __construct(Request $request)
13
    {
14 22
        foreach (config('auto-deploy.origins') as $origin) {
15 22
            $type = "\Morphatic\AutoDeploy\Origins\\$origin";
16 22
            $this->origins[$origin] = new $type($request);
17 11
        }
18 22
    }
19
20
    /**
21
     * Handles the HTTP request.
22
     *
23
     * @param Illuminate\Http\Request $request The request
24
     * @param Closure                 $next    Mechanism for passing the result down the pipeline to the next piece of middleware
25
     *
26
     * @return Illuminate\Http\Response A Response object that is passed to the next piece of middleware
27
     */
28 22
    public function handle($request, Closure $next)
29
    {
30 22
        if ($request->path() === config('auto-deploy.route')) {
31 20
            if (!config('auto-deploy.require-ssl') || $request->secure()) {
32 18
                $origin = $this->determineOrigin();
33 18
                if (null !== $origin) {
34 16
                    if ($origin->isAuthentic()) {
35
                        // set the origin type in the controller
36 12
                        $request->offsetSet('origin', $origin);
37
38 12
                        return $next($request);
39
                    } else {
40 4
                        abort(403, 'Forbidden. Could not verify the origin of the request.');
41
                    }
42
                } else {
43 2
                    abort(403, 'Forbidden. Could not determine the origin of the request.');
44
                }
45
            } else {
46 2
                abort(403, 'Forbidden. Webhook requests must be sent using SSL.');
47
            }
48
        }
49
        // Passthrough if it's not our specific route
50 2
        return $next($request);
51
    }
52
53
    /**
54
     * Determine the origin of a deploy request.
55
     *
56
     * @return \Morphatic\AutoDeploy\Origins\OriginInterface An object corresponding to the origin type or null.
57
     */
58 18
    private function determineOrigin()
59
    {
60 18
        foreach ($this->origins as $origin) {
61 18
            if ($origin->isOrigin()) {
62 17
                return $origin;
63
            }
64 7
        }
65 2
    }
66
}
67