Completed
Push — master ( 93c150...38adbe )
by Ibrahim
03:41
created

Event   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.49%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 66
ccs 32
cts 37
cp 0.8649
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A loadObject() 0 4 1
B discoverOwner() 0 11 5
A validFor() 0 7 2
A package() 0 10 1
A forwardTo() 0 9 2
A capture() 0 8 2
1
<?php
2
namespace Yabacon\Paystack;
3
4
use Yabacon\Paystack\Http\Request;
5
6
class Event
7
{
8
    public $raw = '';
9
    protected $signature = '';
10
    public $obj;
11
    const SIGNATURE_KEY = 'HTTP_X_PAYSTACK_SIGNATURE';
12
13 6
    protected function __construct()
14
    {
15 6
    }
16
17 3
    public static function capture()
0 ignored issues
show
Coding Style introduced by
capture uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
18
    {
19 3
        $evt = new Event();
20 2
        $evt->raw = @file_get_contents('php://input');
21 2
        $evt->signature = ( isset($_SERVER[self::SIGNATURE_KEY]) ? $_SERVER[self::SIGNATURE_KEY] : '' );
22 2
        $evt->loadObject();
23 2
        return $evt;
24
    }
25
26 6
    protected function loadObject()
27
    {
28 6
        $this->obj = json_decode($this->raw);
29 6
    }
30
31 1
    public function discoverOwner(array $keys)
32
    {
33 1
        if (!$this->obj || !property_exists($this->obj, 'data')) {
34
            return;
35
        }
36 1
        foreach ($keys as $index => $key) {
37 1
            if ($this->validFor($key)) {
38 1
                return $index;
39
            }
40 1
        }
41
    }
42
43 2
    public function validFor($key)
44
    {
45 2
        if ($this->signature === hash_hmac('sha512', $this->raw, $key)) {
46 2
            return true;
47
        }
48 1
        return false;
49
    }
50
51 2
    public function package(array $additional_headers = [], $method = 'POST')
52
    {
53 2
        $pack = new Request();
54 2
        $pack->method = $method;
55 2
        $pack->headers = $additional_headers;
56 2
        $pack->headers["X-Paystack-Signature"] = $this->signature;
57 2
        $pack->headers["Content-Type"] = "application/json";
58 2
        $pack->body = $this->raw;
59 2
        return $pack;
60
    }
61
62 1
    public function forwardTo($url, array $additional_headers = [], $method = 'POST')
63
    {
64 1
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
65 1
            return false;
66
        }
67
        $packed = $this->package($additional_headers, $method);
68
        $packed->endpoint = $url;
69
        return $packed->send()->wrapUp();
70
    }
71
}
72