Completed
Push — master ( 458684...f006a2 )
by Ryosuke
02:58
created

RequestorTrait::postMultipartOutAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
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 5
    public function getAsync($endpoint, array $params = [], $return_response_object = false)
14 5
    {
15 5
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->get($endpoint, $params), $return_response_object);
16
    }
17
18 8
    public function postAsync($endpoint, array $params = [], $return_response_object = false)
19 8
    {
20 8
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->post($endpoint, $params), $return_response_object);
21
    }
22
23 8
    public function postMultipartAsync($endpoint, array $params = [], $return_response_object = false)
24 8
    {
25 8
        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 6
    public function streamingAsync($endpoint, callable $event_handler, array $params = [], callable $header_response_handler = null)
44 6
    {
45 6
        $handler = new StreamHandler($header_response_handler, $event_handler);
46 6
        $ch = $this->getInternalCurl()->streaming($endpoint, $params, $handler);
47
        try {
48 6
            yield $ch;
49 5
        } catch (CURLException $e) {
50 5
            if (!$handler->isHaltedByUser()) {
51 1
                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 1
    public function getOutAsync($endpoint, array $params = [], $return_response_object = false)
73 1
    {
74 1
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object);
75
    }
76
77 1
    public function postOutAsync($endpoint, array $params = [], $return_response_object = false)
78 1
    {
79 1
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object);
80
    }
81
82 1
    public function postMultipartOutAsync($endpoint, array $params = [], $return_response_object = false)
83 1
    {
84 1
        return ResponseYielder::asyncExecDecoded($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object);
85
    }
86
87 1
    public function getOut($endpoint, array $params = [], $return_response_object = false)
88 1
    {
89 1
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->getOut($endpoint, $params), $return_response_object);
90
    }
91
92 1
    public function postOut($endpoint, array $params = [], $return_response_object = false)
93 1
    {
94 1
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->postOut($endpoint, $params), $return_response_object);
95
    }
96
97 1
    public function postMultipartOut($endpoint, array $params = [], $return_response_object = false)
98 1
    {
99 1
        return ResponseYielder::syncExecDecoded($this->getInternalCurl()->postMultipartOut($endpoint, $params), $return_response_object);
100
    }
101
}
102