Passed
Push — master ( 48ce4d...e85b61 )
by Ryosuke
03:52
created

CurlExecutor::execDecodedAsync()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 3
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 CurlExecutor
11
{
12 27
    public static function exec($ch)
13 27
    {
14 27
        if (false === $buffer = curl_exec($ch)) {
15 1
            throw new CURLException(curl_error($ch), curl_errno($ch), $ch);
16
        }
17 26
        $response = new Response($buffer, $ch);
18 25
        if ($response->getStatusCode() >= 300 &&
19 25
            $response->getStatusCode() < 400 &&
20 25
            false !== $url = curl_getinfo($ch, CURLINFO_REDIRECT_URL)) {
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::exec($ch);
27
        }
28 25
        return $response;
29
    }
30
31 25
    public static function execAsync($ch)
32 25
    {
33 25
        $response = new Response((yield $ch), $ch);
34 25
        if ($response->getStatusCode() >= 300 &&
35 25
            $response->getStatusCode() < 400 &&
36 25
            false !== $url = curl_getinfo($ch, CURLINFO_REDIRECT_URL)) {
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::exec($ch);
43
        }
44 24
        yield CoInterface::RETURN_WITH => $response;
45
        // @codeCoverageIgnoreStart
46
    }
47
    // @codeCoverageIgnoreEnd
48
49 24
    public static function execDecoded($ch, $return_response_object = false)
50 24
    {
51 24
        $response = static::exec($ch);
52 23
        $response = ResponseBodyDecoder::getDecodedResponse($response);
53 15
        if (!$return_response_object) {
54 15
            $response = $response->hasContent() ? $response->getContent() : null;
55
        }
56 15
        return $response;
57
    }
58
59 24
    public static function execDecodedAsync($ch, $return_response_object = false)
60 24
    {
61 24
        $response = (yield static::execAsync($ch));
62 24
        $response = ResponseBodyDecoder::getDecodedResponse($response);
63 24
        if (!$return_response_object) {
64 24
            $response = $response->hasContent() ? $response->getContent() : null;
65
        }
66 24
        yield CoInterface::RETURN_WITH => $response;
67
        // @codeCoverageIgnoreStart
68
    }
69
    // @codeCoverageIgnoreEnd
70
}
71