HttpClient   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 57.14%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 51
ccs 8
cts 14
cp 0.5714
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 4 1
A sendRequest() 0 6 1
A exceptionIsThrown() 0 4 1
A whenEventHappens() 0 6 1
1
<?php
2
3
namespace Imanghafoori\MakeSure;
4
5
/**
6
 * Class HttpClient.
7
 *
8
 * @method IsRespondedWith sendingPostRequest($uri, array $data = [], array $headers = [])
9
 * @method IsRespondedWith sendingJsonPostRequest($uri, array $data = [], array $headers = [])
10
 * @method IsRespondedWith sendingDeleteRequest($uri, array $data = [], array $headers = [])
11
 * @method IsRespondedWith sendingJsonDeleteRequest($uri, array $data = [], array $headers = [])
12
 * @method IsRespondedWith sendingPutRequest($uri, array $data = [], array $headers = [])
13
 * @method IsRespondedWith sendingJsonPutRequest($uri, array $data = [], array $headers = [])
14
 * @method IsRespondedWith sendingPatchRequest($uri, array $data = [], array $headers = [])
15
 * @method IsRespondedWith sendingJsonPatchRequest($uri, array $data = [], array $headers = [])
16
 * @method IsRespondedWith sendingGetRequest($uri, array $headers = [])
17
 * @method IsRespondedWith sendingJsonGetRequest($uri, array $headers = [])
18
 */
19
class HttpClient
20
{
21
    private $chain;
22
23
    private $methods = [
24
        'sendingPostRequest'       => 'post',
25
        'sendingJsonPostRequest'   => 'postJson',
26
        'sendingDeleteRequest'     => 'delete',
27
        'sendingJsonDeleteRequest' => 'deleteJson',
28
        'sendingPutRequest'        => 'put',
29
        'sendingJsonPutRequest'    => 'putJson',
30
        'sendingPatchRequest'      => 'patch',
31
        'sendingJsonPatchRequest'  => 'patchJson',
32
        'sendingGetRequest'        => 'get',
33
        'sendingJsonGetRequest'    => 'getJson',
34
    ];
35
36
    /**
37
     * HttpClient constructor.
38
     *
39
     * @param $phpunit
40
     */
41 11
    public function __construct($phpunit)
42
    {
43 11
        $this->chain = new Chain($phpunit);
44 11
    }
45
46 11
    public function __call($method, $params): IsRespondedWith
47
    {
48 11
        return $this->sendRequest($method, ...$params);
49
    }
50
51 11
    private function sendRequest($method, ...$data): IsRespondedWith
52
    {
53 11
        $this->chain->data['http'] = [$this->methods[$method], $data];
54
55 11
        return new IsRespondedWith($this->chain);
56
    }
57
58
    public function exceptionIsThrown($type)
59
    {
60
        $this->chain->data['exception'] = $type;
61
    }
62
63
    public function whenEventHappens($event): self
64
    {
65
        $this->chain->data['event'] = $event;
66
67
        return $this;
68
    }
69
}
70