Passed
Push — develop ( dbd74f...a60ca0 )
by Stan
01:58
created

PushGateway::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Krenor\Prometheus;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Request;
7
use function GuzzleHttp\Psr7\stream_for;
8
use Krenor\Prometheus\Renderer\TextRenderer;
9
10
class PushGateway
11
{
12
    /**
13
     * @var Client
14
     */
15
    protected $client;
16
17
    /**
18
     * @var CollectorRegistry
19
     */
20
    protected $registry;
21
22
    /**
23
     * PushGateway constructor.
24
     *
25
     * @param Client $client
26
     * @param CollectorRegistry $registry
27
     */
28 3
    public function __construct(Client $client, CollectorRegistry $registry)
29
    {
30 3
        $this->client = $client;
31 3
        $this->registry = $registry;
32 3
    }
33
34
    /**
35
     * @param string $job
36
     * @param string|null $instance
37
     *
38
     * @throws \GuzzleHttp\Exception\GuzzleException
39
     *
40
     * @return bool
41
     */
42 1
    public function add(string $job, ?string $instance = null): bool
43
    {
44 1
        return $this->request('POST', ...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $job of Krenor\Prometheus\PushGateway::request() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        return $this->request('POST', /** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
45
    }
46
47
    /**
48
     * @param string $job
49
     * @param string|null $instance
50
     *
51
     * @throws \GuzzleHttp\Exception\GuzzleException
52
     *
53
     * @return bool
54
     */
55 1
    public function replace(string $job, ?string $instance = null): bool
56
    {
57 1
        return $this->request('PUT', ...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $job of Krenor\Prometheus\PushGateway::request() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        return $this->request('PUT', /** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
58
    }
59
60
    /**
61
     * @param string $job
62
     * @param string|null $instance
63
     *
64
     * @throws \GuzzleHttp\Exception\GuzzleException
65
     *
66
     * @return bool
67
     */
68 1
    public function delete(string $job, ?string $instance = null): bool
69
    {
70 1
        return $this->request('DELETE', ...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $job of Krenor\Prometheus\PushGateway::request() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        return $this->request('DELETE', /** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
71
    }
72
73
    /**
74
     * @param string $method
75
     * @param string $job
76
     * @param string|null $instance
77
     *
78
     * @throws \GuzzleHttp\Exception\GuzzleException
79
     *
80
     * @return bool
81
     */
82 3
    protected function request(string $method, string $job, ?string $instance = null): bool
83
    {
84 3
        $request = new Request($method, $instance === null ? "job/{$job}" : "job/{$job}/instance/{$instance}");
85
        $options = [
86
            'headers' => [
87 3
                'Content-Type' => TextRenderer::CONTENT_TYPE,
88
            ],
89
        ];
90
91 3
        if ($method !== 'DELETE') {
92 2
            $options['body'] = stream_for(
93 2
                (new TextRenderer)
94 2
                    ->render($this->registry->collect())
95
            );
96
        }
97
98
        return $this
99 3
                ->client
100 3
                ->send($request, $options)
101 3
                ->getStatusCode() === 202;
102
    }
103
}
104