Completed
Push — master ( cc0a72...294b2e )
by Morgan
03:23
created

AbstractOrigin::isIpInRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Morphatic\AutoDeploy\Origins;
4
5
use Illuminate\Http\Request;
6
7
abstract class AbstractOrigin implements OriginInterface
8
{
9
    /**
10
     * The name of the origin.
11
     *
12
     * @var string
13
     */
14
    public $name;
15
16
    /**
17
     * The Request object associated with this webhook.
18
     *
19
     * @var Illuminate\Http\Request
20
     */
21
    public $request;
22
23 36
    public function __construct(Request $request)
1 ignored issue
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
24
    {
25 36
        $this->request = $request;
1 ignored issue
show
Documentation Bug introduced by
It seems like $request of type object<Illuminate\Http\Request> is incompatible with the declared type object<Morphatic\AutoDep...lluminate\Http\Request> of property $request.

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...
26 36
    }
27
28
    /**
29
     * Determines whether or not the Request originated from the webhook origin.
30
     *
31
     * @return bool Returns true if the request originated from this origin. False otherwise.
32
     */
33
    abstract public function isOrigin();
34
35
    /**
36
     * Verifies the authenticity of a webhook request from the origin.
37
     *
38
     * @return bool Returns true if the request is authentic. False otherwise.
39
     */
40
    abstract public function isAuthentic();
41
42
    /**
43
     * Gets the event the triggered the webhook request.
44
     *
45
     * @return string The name of the event, e.g. push, release, create, etc.
46
     */
47
    abstract public function event();
48
49
    /**
50
     * Gets the URL to be cloned from.
51
     *
52
     * @return string The URL of the repo.
53
     */
54
    abstract public function getRepoUrl();
55
56
    /**
57
     * Gets the ID of the commit that is to be cloned.
58
     *
59
     * @return string The commit ID.
60
     */
61
    abstract public function getCommitId();
62
63
    /**
64
     * Determines if a target IP address is within a particular IP address range.
65
     *
66
     * @param string $targetIp The IP address to check
67
     * @param string $rangeIp  The IP address defining the range
68
     * @param int    $cidrMask The CIDR notation net mask defining the range
69
     *
70
     * @return bool True if the target IP falls within the specified range. Otherwise false.
71
     */
72 18
    protected function isIpInRange($targetIp, $rangeIp, $cidrMask)
73
    {
74 18
        $target = sprintf('%032b', ip2long($targetIp));
75 18
        $range = sprintf('%032b', ip2long($rangeIp));
76
77 18
        return 0 === substr_compare($target, $range, 0, $cidrMask);
78
    }
79
}
80