Completed
Push — master ( d1a78a...0cf24a )
by Guillaume
02:47
created

GuzzleClient::createClient()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 1
eloc 16
c 3
b 0
f 2
nc 1
nop 2
dl 0
loc 27
rs 8.8571
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Client;
17
18
use Concat\Http\Middleware\Logger;
19
use GuzzleHttp\Client;
20
use GuzzleHttp\HandlerStack;
21
use GuzzleHttp\MessageFormatter;
22
use GuzzleHttp\Middleware;
23
use Hogosha\Monitor\Middleware\Backoff;
24
use Hogosha\Monitor\Monitor;
25
use Symfony\Component\Console\Logger\ConsoleLogger;
26
use Webmozart\Console\Adapter\IOOutput;
27
use Webmozart\Console\Api\IO\IO;
28
29
/**
30
 * @author Guillaume Cavana <[email protected]>
31
 */
32
class GuzzleClient
33
{
34
    /**
35
     * createClient.
36
     *
37
     * @param IO    $io
38
     * @param array $options
39
     *
40
     * @return Client
41
     */
42
    public static function createClient(IO $io, $options = [])
43
    {
44
        $stack = HandlerStack::create();
45
46
        $middleware = (new Logger((new ConsoleLogger(new IOOutput($io)))));
0 ignored issues
show
Documentation introduced by
new \Symfony\Component\C...\Adapter\IOOutput($io)) is of type object<Symfony\Component...e\Logger\ConsoleLogger>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
        $middleware->setRequestLoggingEnabled(true);
48
        $format = "<fg=white;bg=blue>Request:</>\n <fg=white;bg=default>{host} {req_headers} \nBody: {req_body} </>\n <fg=white;bg=blue>Reponse:</>\n<fg=white;bg=default>{res_body} {res_headers}</>\n";
49
        $middleware->setFormatter((new MessageFormatter($format)));
0 ignored issues
show
Documentation introduced by
new \GuzzleHttp\MessageFormatter($format) is of type object<GuzzleHttp\MessageFormatter>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50
51
        $stack->push($middleware, 'logger');
52
53
        $stack->push(
54
            Middleware::retry(Backoff::decider(), Backoff::delay())
55
        );
56
57
        $defaults = [
58
            'handler' => $stack,
59
            'allow_redirects' => false,
60
            'headers' => [
61
                'User-Agent' => sprintf('Irongate Monitor %s', Monitor::VERSION),
62
            ],
63
        ];
64
65
        $options = array_merge($defaults, $options);
66
67
        return new Client($options);
68
    }
69
}
70