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

Github::getRepoUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Morphatic\AutoDeploy\Origins;
4
5
class Github extends AbstractOrigin implements OriginInterface
6
{
7
    /**
8
     * The name of the origin.
9
     *
10
     * @var string
11
     */
12
    public $name = 'Github';
13
14
    /**
15
     * Determines whether or not the Request originated from Github.
16
     *
17
     * @param Illuminate\Http\Request $this->request The Request object
0 ignored issues
show
Bug introduced by
There is no parameter named $this->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...
18
     *
19
     * @return bool Returns true if the request originated from Github. False otherwise.
20
     */
21 18
    public function originated()
22
    {
23
        // Correct IP range for Github maintained here:
24
        // https://help.github.com/articles/what-ip-addresses-does-github-use-that-i-should-whitelist/
25 18
        $has_github_header = false !== strpos($this->request->header('User-Agent'), 'GitHub-Hookshot');
0 ignored issues
show
Coding Style introduced by
$has_github_header does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
26 18
        $has_github_ip = $this->ipInRange($this->request->server('REMOTE_ADDR'), '192.30.252.0', 22);
0 ignored issues
show
Coding Style introduced by
$has_github_ip does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27 18
        if ($has_github_header && $has_github_ip) {
0 ignored issues
show
Coding Style introduced by
$has_github_header does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $has_github_header && $has_github_ip;.
Loading history...
28 4
            return true;
29
        }
30
31 14
        return false;
32
    }
33
34
    /**
35
     * Verifies the authenticity of a webhook request from Github.
36
     *
37
     * Follows the procedure described here: https://developer.github.com/webhooks/securing/
38
     *
39
     * @param Illuminate\Http\Request $this->request The Request object
0 ignored issues
show
Bug introduced by
There is no parameter named $this->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...
40
     *
41
     * @return bool Returns true if the request is authentic. False otherwise.
1 ignored issue
show
Documentation introduced by
Should the return type not be null|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
42
     */
43 4
    public function verify()
44
    {
45
        // get the Github signature
46 4
        $xhub = $this->request->header('X-Hub-Signature') ?: 'nothing';
47
48
        // reconstruct the hash on this side
49 4
        $hash = 'sha1='.hash_hmac('sha1', $this->request->getContent(), config('auto-deploy.secret'));
50
51
        // securely compare them
52 4
        return hash_equals($xhub, $hash);
53
    }
54
55
    /**
56
     * Gets the event the triggered the webhook request.
57
     *
58
     * @param Illuminate\Http\Request $this->request The Request object
0 ignored issues
show
Bug introduced by
There is no parameter named $this->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...
59
     *
60
     * @return string The name of the event, e.g. push, release, create, etc.
61
     */
62 2
    public function event()
63
    {
64 2
        return $this->request->header('X-GitHub-Event');
65
    }
66
67
    /**
68
     * Gets the URL to be cloned from.
69
     *
70
     * @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...
71
     *
72
     * @return string The URL of the repo.
73
     */
74 36
    public function getRepoUrl()
75
    {
76 36
        return $this->request->json('repository.clone_url');
77
    }
78
79
    /**
80
     * Gets the ID of the commit that is to be cloned.
81
     *
82
     * @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...
83
     *
84
     * @return string The commit ID.
85
     */
86 36
    public function getCommitId()
87
    {
88 36
        return $this->request->json('after');
89
    }
90
}
91