Completed
Push — master ( 3cf178...59e65f )
by Ryosuke
03:33
created

RequestorTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 66.67%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 20
c 4
b 2
f 0
lcom 1
cbo 3
dl 0
loc 93
ccs 38
cts 57
cp 0.6667
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
getInternalCurl() 0 1 ?
A getAsync() 0 4 1
A postAsync() 0 4 1
A postMultipartAsync() 0 4 1
A get() 0 4 1
A post() 0 4 1
A postMultipart() 0 4 1
A getOutAsync() 0 4 1
A postOutAsync() 0 4 1
A postMultipartOutAsync() 0 4 1
A getOut() 0 4 1
A postOut() 0 4 1
A postMultipartOut() 0 4 1
A streamingAsync() 0 15 4
A streaming() 0 12 4
1
<?php
2
3
namespace mpyw\Cowitter\Traits;
4
5
use mpyw\Co\CURLException;
6
use mpyw\Cowitter\Components\StreamHandler;
7
use mpyw\Cowitter\Helpers\ResponseYielder;
8
9
trait RequestorTrait
10
{
11
    abstract protected function getInternalCurl();
12
13 1
    public function getAsync($endpoint, array $params = [], $return_response_object = false)
14 1
    {
15 1
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->get($endpoint, $params), $return_response_object);
16
    }
17
18 1
    public function postAsync($endpoint, array $params = [], $return_response_object = false)
19 1
    {
20 1
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->post($endpoint, $params), $return_response_object);
21
    }
22
23 1
    public function postMultipartAsync($endpoint, array $params = [], $return_response_object = false)
24 1
    {
25 1
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->postMultipart($endpoint, $params), $return_response_object);
26
    }
27
28 6
    public function get($endpoint, array $params = [], $return_response_object = false)
29 6
    {
30 6
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->get($endpoint, $params), $return_response_object);
31
    }
32
33 5
    public function post($endpoint, array $params = [], $return_response_object = false)
34 5
    {
35 5
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->post($endpoint, $params), $return_response_object);
36
    }
37
38 1
    public function postMultipart($endpoint, array $params = [], $return_response_object = false)
39 1
    {
40 1
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->postMultipart($endpoint, $params), $return_response_object);
41
    }
42
43 5
    public function streamingAsync($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null)
44 5
    {
45 5
        $handler = new StreamHandler($header_response_handler, $event_handler);
46 5
        $ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler);
47
        try {
48 5
            yield $ch;
49 4
        } catch (CURLException $e) {
50 4
            if (!$handler->isHaltedByUser()) {
51
                throw $e;
52
            }
53
        }
54 5
        if (!$handler->isHaltedByUser()) {
55 1
            throw new \UnexpectedValueException('Streaming stopped unexpectedly.');
56
        }
57 4
    }
58
59 6
    public function streaming($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null)
60 6
    {
61 6
        $handler = new StreamHandler($header_response_handler, $event_handler);
62 6
        $ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler);
63 6
        $result = curl_exec($ch);
64 4
        if (!$handler->isHaltedByUser() && $result === false) {
65 1
            throw new CURLException(curl_error($ch), curl_errno($ch), $ch);
66
        }
67 3
        if (!$handler->isHaltedByUser()) {
68 1
            throw new \UnexpectedValueException('Streaming stopped unexpectedly.');
69
        }
70 2
    }
71
72
    public function getOutAsync($endpoint, array $params = [], $return_response_object = false)
73
    {
74
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object);
75
    }
76
77
    public function postOutAsync($endpoint, array $params = [], $return_response_object = false)
78
    {
79
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object);
80
    }
81
82
    public function postMultipartOutAsync($endpoint, array $params = [], $return_response_object = false)
83
    {
84
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object);
85
    }
86
87
    public function getOut($endpoint, array $params = [], $return_response_object = false)
88
    {
89
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object);
90
    }
91
92
    public function postOut($endpoint, array $params = [], $return_response_object = false)
93
    {
94
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object);
95
    }
96
97
    public function postMultipartOut($endpoint, array $params = [], $return_response_object = false)
98
    {
99
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object);
100
    }
101
}
102