Completed
Push — master ( 73a5aa...d5d862 )
by Ryosuke
03:31
created

ResponseYielder   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 15
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 61
ccs 40
cts 40
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A syncExecDecoded() 0 9 3
A asyncExecDecoded() 0 10 3
B syncExec() 0 18 5
A asyncExec() 0 16 4
1
<?php
2
3
namespace mpyw\Cowitter\Helpers;
4
5
use mpyw\Co\CURLException;
6
use mpyw\Co\CoInterface;
7
use mpyw\Cowitter\Response;
8
use mpyw\Cowitter\Helpers\ResponseBodyDecoder;
9
10
class ResponseYielder
11
{
12 16
    public static function syncExec($ch)
13 16
    {
14 16
        if (false === $buffer = curl_exec($ch)) {
15 1
            throw new CURLException(curl_error($ch), curl_errno($ch), $ch);
16
        }
17 15
        $response = new Response($buffer, $ch);
18 14
        if ($response->getStatusCode() >= 300 &&
19 14
            $response->getStatusCode() < 400 &&
20 14
            '' !== $url = $response->getHeaderLine('Location')) {
21 1
            curl_setopt_array($ch, [
22 1
                CURLOPT_URL => $url,
23 1
                CURLOPT_HTTPGET => true,
24 1
                CURLOPT_REFERER => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
25
            ]);
26 1
            return static::syncExec($ch);
27
        }
28 14
        return $response;
29
    }
30
31 11
    public static function asyncExec($ch)
32 11
    {
33 11
        $response = new Response((yield $ch), $ch);
34 11
        if ($response->getStatusCode() >= 300 &&
35 11
            $response->getStatusCode() < 400 &&
36 11
            '' !== $url = $response->getHeaderLine('Location')) {
37 1
            curl_setopt_array($ch, [
38 1
                CURLOPT_URL => $url,
39 1
                CURLOPT_HTTPGET => true,
40 1
                CURLOPT_REFERER => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL),
41
            ]);
42 1
            yield CoInterface::RETURN_WITH => static::syncExec($ch);
43
        }
44 10
        yield CoInterface::RETURN_WITH => $response;
45
        // @codeCoverageIgnoreStart
46
    }
47
    // @codeCoverageIgnoreEnd
48
49 13
    public static function syncExecDecoded($ch, $return_response_object = false)
50 13
    {
51 13
        $response = static::syncExec($ch);
52 12
        $response = ResponseBodyDecoder::getDecodedResponse($response, $ch);
53 4
        if (!$return_response_object) {
54 4
            $response = $response->hasContent() ? $response->getContent() : null;
55
        }
56 4
        return $response;
57
    }
58
59 10
    public static function asyncExecDecoded($ch, $return_response_object = false)
60 10
    {
61 10
        $response = (yield static::asyncExec($ch));
62 10
        $response = ResponseBodyDecoder::getDecodedResponse($response, $ch);
63 10
        if (!$return_response_object) {
64 10
            $response = $response->hasContent() ? $response->getContent() : null;
65
        }
66 10
        yield CoInterface::RETURN_WITH => $response;
67
        // @codeCoverageIgnoreStart
68
    }
69
    // @codeCoverageIgnoreEnd
70
}
71