Completed
Push — master ( 2028e7...238e6a )
by Dmitry
05:44 queued 01:42
created

Client::getCaptchaResultBulk()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 5
cts 5
cp 1
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * @author Dmitry Gladyshev <[email protected]>
4
 */
5
6
namespace Rucaptcha;
7
8
9
use Rucaptcha\Exception\RuntimeException;
10
11
class Client extends GenericClient
12
{
13
    const STATUS_OK_REPORT_RECORDED = 'OK_REPORT_RECORDED';
14
15
    /**
16
     * @var string
17
     */
18
    protected $serverBaseUri = 'http://rucaptcha.com';
19
20
    /**
21
     * Your application ID in Rucaptcha catalog.
22
     * The value `1013` is ID of this library. Set in false if you want to turn off sending any ID.
23
     * @see https://rucaptcha.com/software/view/php-api-client
24
     * @var string
25
     */
26
    protected $softId = '1013';
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function sendCaptcha($content, array $extra = [])
32
    {
33
        if ($this->softId && !isset($extra[Extra::SOFT_ID])) {
34
            $extra[Extra::SOFT_ID] = $this->softId;
35
        }
36
        return parent::sendCaptcha($content, $extra);
37
    }
38
39
    /**
40
     * Bulk captcha result.
41
     *
42
     * @param int[] $captchaIds     # Captcha task Ids array
43
     * @return string[]             # Array $captchaId => $captchaText or false if is not ready
44
     * @throws RuntimeException
45
     */
46
    public function getCaptchaResultBulk(array $captchaIds)
47
    {
48
        $response = $this->getHttpClient()->request('GET', '/res.php?' . http_build_query([
49
                'key' => $this->apiKey,
50
                'action' => 'get',
51
                'ids' => join(',', $captchaIds)
52
            ]));
53
54
        $captchaTexts = $response->getBody()->__toString();
55
56
        $this->getLogger()->info("Got bulk response: `{$captchaTexts}`.");
57
58
        $captchaTexts = explode("|", $captchaTexts);
59
60
        $result = [];
61
62 23
        foreach ($captchaTexts as $index => $captchaText) {
63
            $captchaText = html_entity_decode(trim($captchaText));
64 23
            $result[$captchaIds[$index]] =
65 1
                ($captchaText == self::STATUS_CAPTCHA_NOT_READY) ? false : $captchaText;
66 1
        }
67
68
        return $result;
69 23
    }
70 1
71 1
    /**
72
     * @return string
73 23
     */
74 23
    public function getBalance()
75
    {
76
        $response = $this->getHttpClient()->request('GET', "/res.php?key={$this->apiKey}&action=getbalance");
77
        return $response->getBody()->__toString();
78
    }
79
80
    /**
81
     * @param $captchaId
82
     * @return bool
83
     * @throws RuntimeException
84
     */
85
    public function badCaptcha($captchaId)
86
    {
87
        $response = $this->getHttpClient()->request('GET', "/res.php?key={$this->apiKey}&action=reportbad&id={$captchaId}");
88
        if ($response->getBody()->__toString() === self::STATUS_OK_REPORT_RECORDED) {
89
            return true;
90
        }
91
        throw new RuntimeException('Report sending trouble: ' . $response->getBody() . '.');
92
    }
93
94
    /**
95
     * @param array $paramsList
96
     * @return array
97
     */
98
    public function getLoad(array $paramsList = ['waiting', 'load', 'minbid', 'averageRecognitionTime'])
99
    {
100
        $response = $this->getHttpClient()->request('GET', "/load.php");
101
        $responseText = $response->getBody()->__toString();
102
        $statusData = [];
103
104
        foreach ($paramsList as $item) {
105
            // Fast parse tags
106
            $value = substr($responseText,
107 22
                strpos($responseText, '<' . $item . '>') + mb_strlen('<' . $item . '>'),
108
                strpos($responseText, '</' . $item . '>') - strpos($responseText, '<' . $item . '>') - mb_strlen('<' . $item . '>')
109 22
            );
110
111 22
            if ($value !== false) {
112 22
                $statusData[$item] = $value;
113 22
            }
114
        }
115 22
        return $statusData;
116 22
    }
117
}
118