Completed
Push — master ( 0cf24a...1a553a )
by Guillaume
04:54
created

AbstractPusher::createClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 20
rs 9.4285
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\Pusher;
17
18
use Concat\Http\Middleware\Logger;
19
use GuzzleHttp\HandlerStack;
20
use GuzzleHttp\MessageFormatter;
21
use Hogosha\Monitor\Model\Result;
22
use Hogosha\Sdk\Api\Client as SdkClient;
23
use Symfony\Component\Console\Logger\ConsoleLogger;
24
use Webmozart\Console\Adapter\IOOutput;
25
use Webmozart\Console\Api\IO\IO;
26
27
/**
28
 * Class AbstractPusher.
29
 */
30
abstract class AbstractPusher implements PusherInterface
31
{
32
    /**
33
     * $client.
34
     *
35
     * @var SdkClient
36
     */
37
    protected $client;
38
39
    /**
40
     * $options.
41
     *
42
     * @var array
43
     */
44
    protected $options;
45
46
    /**
47
     * $io.
48
     *
49
     * @var IO
50
     */
51
    protected $io;
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param array  $options
57
     * @param IO     $io
58
     * @param Client $client
59
     */
60
    public function __construct(array $options, IO $io, SdkClient $client = null)
61
    {
62
        $this->io = $io;
63
        $this->options = $options;
64
        $this->client = null !== $client ? $client : $this->createClient();
65
    }
66
67
    /**
68
     * getDefaultFailedIncidentMessage.
69
     *
70
     * @return string Default message used to create a incident when a failure as occured
71
     */
72
    public function getDefaultFailedIncidentMessage()
73
    {
74
        return $this->options['default_failed_incident_message'];
75
    }
76
77
    /**
78
     * getDefaultResolvedIncidentMessage.
79
     *
80
     * @return string Default message used to create a incident when a service is resolved as occured
81
     */
82
    public function getDefaultResolvedIncidentMessage()
83
    {
84
        return $this->options['default_resolved_incident_message'];
85
    }
86
87
    /**
88
     * getClient.
89
     *
90
     * @return SdkClient
91
     */
92
    protected function createClient()
93
    {
94
        $stack = HandlerStack::create();
95
96
        $middleware = (new Logger((new ConsoleLogger(new IOOutput($this->io)))));
0 ignored issues
show
Documentation introduced by
new \Symfony\Component\C...er\IOOutput($this->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...
97
        $middleware->setRequestLoggingEnabled(true);
98
        $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";
99
        $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...
100
101
        $stack->push($middleware, 'logger');
102
103
        return new SdkClient(
104
            [
105
                'username' => $this->options['username'],
106
                'password' => $this->options['password'],
107
                'base_uri' => $this->options['base_uri'],
108
                'handler' => $stack,
109
            ]
110
        );
111
    }
112
113
    /**
114
     * push.
115
     *
116
     * @param Result $result
117
     *
118
     * @throws \Exception
119
     *
120
     * @return mixed
121
     */
122
    abstract public function push(Result $result);
123
}
124