Completed
Push — master ( 25f226...cc0a72 )
by Morgan
03:23
created

VerifyDeployRequest::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
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 22
    public function handle($request, Closure $next)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
21
    {
22 22
        if ($request->path() === config('auto-deploy.route')) {
23 20
            if (!config('auto-deploy.require-ssl') || $request->secure()) {
24 18
                $origin = $this->determineOrigin();
25 18
                if (null !== $origin) {
26 16
                    if ($origin->verify()) {
27
                        // set the origin type in the controller
28 12
                        $request->offsetSet('origin', $origin);
29
30 12
                        return $next($request);
31
                    } else {
32 4
                        abort(403, 'Forbidden. Could not verify the origin of the request.');
33
                    }
34
                } else {
35 2
                    abort(403, 'Forbidden. Could not determine the origin of the request.');
36
                }
37
            } else {
38 2
                abort(403, 'Forbidden. Webhook requests must be sent using SSL.');
39
            }
40
        }
41
        // Passthrough if it's not our specific route
42 2
        return $next($request);
43
    }
44
45
    /**
46
     * Determine the origin of a deploy request.
47
     *
48
     * @param Illuminate\Http\Request $request The Request object.
0 ignored issues
show
Bug introduced by
There is no parameter named $request. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
49
     *
50
     * @return \Morphatic\AutoDeploy\Origins\OriginInterface An object corresponding to the origin type or null.
51
     */
52 18
    private function determineOrigin()
53
    {
54 18
        foreach ($this->origins as $origin) {
55 18
            if ($origin->originated()) {
56 17
                return $origin;
57
            }
58 7
        }
59 2
    }
60
}
61