Completed
Push — master ( 1b605f...95b7c3 )
by Dan
06:00
created

Guzzle   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getAsync() 0 8 1
A post() 0 4 1
A scheduleProcessing() 0 16 4
1
<?php
2
3
namespace Nopolabs\Yabot\Guzzle;
4
5
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Handler\CurlMultiHandler;
8
use GuzzleHttp\HandlerStack;
9
use GuzzleHttp\Promise\PromiseInterface;
10
use function GuzzleHttp\Promise\queue;
11
use Nopolabs\Yabot\Helpers\ConfigTrait;
12
use Psr\Http\Message\ResponseInterface;
13
use React\EventLoop\LoopInterface;
14
use React\EventLoop\Timer\TimerInterface;
15
16
class Guzzle
17
{
18
    use ConfigTrait;
19
20
    /** @var Client */
21
    private $client;
22
23
    /** @var LoopInterface */
24
    private $eventloop;
25
26
    /** @var CurlMultiHandler */
27
    private $handler;
28
29
    /** @var TimerInterface */
30
    public $timer;
31
32
    public function __construct(LoopInterface $eventLoop, array $config = [])
33
    {
34
        $this->handler = new CurlMultiHandler();
35
        $config['handler'] = HandlerStack::create($this->handler);
36
        $this->client = new Client($config);
37
        $this->eventloop = $eventLoop;
38
        $this->setConfig($config);
39
    }
40
41
    public function getAsync(string $uri, array $options = []) : PromiseInterface
42
    {
43
        $request = $this->client->getAsync($uri, $options);
44
45
        $this->scheduleProcessing();
46
47
        return $request;
48
    }
49
50
    public function post(string $uri, array $options = []) : ResponseInterface
51
    {
52
        return $this->client->post($uri, $options);
53
    }
54
55
    /**
56
     * @see http://stephencoakley.com/2015/06/11/integrating-guzzle-6-asynchronous-requests-with-reactphp
57
     *
58
     * Jiggerery with Closure::bind to get access to CurlMultiHandler::handles private member.
59
     */
60
    private function scheduleProcessing()
61
    {
62
        if ($this->timer === null) {
63
            $self =& $this;
64
            $this->timer = $this->eventloop->addPeriodicTimer(0, \Closure::bind(function () use (&$self) {
65
66
                $this->tick();
1 ignored issue
show
Bug introduced by
The method tick() does not seem to exist on object<Nopolabs\Yabot\Guzzle\Guzzle>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
                // Stop the timer when there are no more requests
69
                if (empty($this->handles) && queue()->isEmpty()) {
1 ignored issue
show
Bug introduced by
The property handles does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
70
                    $self->timer->cancel();
71
                    $self->timer = null;
72
                }
73
            }, $this->handler, $this->handler));
74
        }
75
    }
76
}