Rucaptcha::getClient()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace gladyshev\yii\rucaptcha;
4
5
use Psr\Log\LoggerInterface;
6
use Rucaptcha\Client as RucaptchaClient;
7
use Yii;
8
use yii\base\Component;
9
10
/**
11
 * Class Rucaptcha
12
 * @method void setOptions(array $options)
13
 * @method void setHttpClient(\GuzzleHttp\ClientInterface $client);
14
 * @method void setLogger(\Psr\Log\LoggerInterface $logger);
15
 * @method string recognize(string $content, array $extra = [])
16
 * @method string recognizeFile(string $path, array $extra = [])
17
 * @method int sendCaptcha(string $content, array $extra = [])
18
 * @method string getCaptchaResult(int $captchaId)
19
 * @method array getCaptchaResultBulk(array $captchaIds)
20
 * @method bool addPingback(string $uri)
21
 * @method array getPingbacks()
22
 * @method bool deletePingback(string $uri)
23
 * @method bool deleteAllPingbacks()
24
 * @method int sendRecapthaV2($googleKey, $pageUrl, $extra = [])
25
 * @method string recognizeRecapthaV2($googleKey, $pageUrl, $extra = [])
26
 * @method string getLastCaptchaId()
27
 * @method string getBalance()
28
 * @method bool badCaptcha(string $captchaId)
29
 * @method array getLoad(array $paramsList = [])
30
 * @method \SimpleXmlElement getLoadXml()
31
 *
32
 * @author Dmitry Gladyshev <[email protected]>
33
 */
34
class Rucaptcha extends Component
35
{
36
    /**
37
     * Rucaptcha client api key.
38
     *
39
     * @var string
40
     */
41
    public $apiKey;
42
43
    /**
44
     * Rucaptcha client options.
45
     *
46
     * @var array
47
     */
48
    public $options = [];
49
50
    /**
51
     * Logger class.
52
     *
53
     * @var string|array
54
     */
55
    public $logger = '\gladyshev\yii\rucaptcha\Logger';
56
57
    /**
58
     * Rucaptcha client.
59
     *
60
     * @var RucaptchaClient
61
     */
62
    protected $client;
63
64
    /**
65
     * @param string $name
66
     * @param array $params
67
     * @return mixed
68
     */
69
    public function __call($name, $params)
70
    {
71
        return call_user_func_array([$this->getClient(), $name], $params);
72
    }
73
74
    /**
75
     * @return RucaptchaClient
76
     */
77
    public function getClient()
78
    {
79
        if ($this->client === null) {
80
            $this->client = new RucaptchaClient($this->apiKey, $this->options);
81
            if (isset($this->options['verbose']) && $this->options['verbose']) {
82
                $logger = Yii::createObject($this->logger);
83
                if ($logger instanceof LoggerInterface) {
84
                    $this->client->setLogger($logger);
85
                }
86
            }
87
        }
88
        return $this->client;
89
    }
90
}
91