Completed
Push — master ( 1aff71...ddb479 )
by Dan
02:22
created

GuzzleFactory::createHandlerStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
namespace Nopolabs\Yabot\Guzzle;
3
4
use function GuzzleHttp\choose_handler;
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\CurlMultiHandler;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Middleware;
9
use React\EventLoop\LoopInterface;
10
11
class GuzzleFactory
12
{
13
    public static function newGuzzle(LoopInterface $eventLoop, array $config) : Guzzle
14
    {
15
        $handler = new CurlMultiHandler();
16
17
        $config['handler'] = self::createHandlerStack();
18
19
        $client = new Client($config);
20
21
        return new Guzzle($client, $handler, $eventLoop);
22
    }
23
24
    /**
25
     * Like HandlerStack::create() except no http_errors
26
     * @return HandlerStack
27
     */
28
    public static function createHandlerStack() : HandlerStack
29
    {
30
        /** @var HandlerStack $stack */
31
        $stack = choose_handler();
32
        $stack->push(Middleware::redirect(), 'allow_redirects');
33
        $stack->push(Middleware::cookies(), 'cookies');
34
        $stack->push(Middleware::prepareBody(), 'prepare_body');
35
36
        return $stack;
37
   }
38
}