WebhookTester   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 85
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getClient() 0 4 1
A setClient() 0 4 1
A setVersion() 0 5 1
A setEndpoint() 0 5 1
A loadEventData() 0 10 2
A triggerEvent() 0 14 2
1
<?php
2
3
namespace TeamTNT\Stripe;
4
5
use GuzzleHttp\Client;
6
7
class WebhookTester
8
{
9
    /**
10
     * Default API version
11
     */
12
    public $version = "2014-09-08";
13
14
    public $endpoint;
15
16
17
    /**
18
     * @var Client
19
     */
20
    private $client;
21
22
    public function __construct($endpoint = null) {
23
        if ($endpoint) {
24
            $this->endpoint = $endpoint;
25
        }
26
        $this->client = new Client();
27
    }
28
29
    /**
30
     * @return mixed
31
     */
32
    public function getClient()
33
    {
34
        return $this->client;
35
    }
36
37
    /**
38
     * @param mixed $client
39
     */
40
    public function setClient($client)
41
    {
42
        $this->client = $client;
43
    }
44
45
    public function setVersion($version)
46
    {
47
        $this->version = $version;
48
        return $this;
49
    }
50
51
    public function setEndpoint($endpoint)
52
    {
53
        $this->endpoint = $endpoint;
54
        return $this;
55
    }
56
57
    public function loadEventData($name)
58
    {
59
        $file = dirname(__FILE__) . '/../webhooks/'.$this->version.'/'.$name.'.json';
60
61
        if (!file_exists($file)) {
62
            throw new InvalidEventException("Event does not exist in version ".$this->version, 1);
63
        }
64
65
        return file_get_contents($file);
66
    }
67
68
69
    /**
70
     * Triggers a simulated Stripe web hook event
71
     *
72
     * @param null|string $event
73
     * @return \GuzzleHttp\Message\FutureResponse|\GuzzleHttp\Message\ResponseInterface|\GuzzleHttp\Ring\Future\FutureInterface|null
74
     * @throws InvalidEventException
75
     */
76
    public function triggerEvent($event = null)
77
    {
78
79
        if (is_null($event)) {
80
            throw new InvalidEventException("Event name required");
81
        }
82
83
        $response = $this->client->post($this->endpoint, [
84
            'headers' => ['content-type' => 'application/json'],
85
            'body' => $this->loadEventData($event)
86
        ]);
87
88
        return $response;
89
    }
90
91
}
92