PushGateway   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 81
ccs 18
cts 18
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 3 1
A __construct() 0 2 1
A request() 0 20 3
A add() 0 3 1
A replace() 0 3 1
1
<?php
2
3
namespace Krenor\Prometheus;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Utils;
7
use GuzzleHttp\Psr7\Request;
8
use Krenor\Prometheus\Renderer\TextRenderer;
9
10
class PushGateway
11
{
12
    /**
13
     * PushGateway constructor.
14
     *
15
     * @param Client $client
16
     * @param CollectorRegistry $registry
17
     */
18 3
    public function __construct(protected Client $client, protected CollectorRegistry $registry)
19
    {
20
        //
21 3
    }
22
23
    /**
24
     * @param string $job
25
     * @param string|null $instance
26
     *
27
     * @throws \GuzzleHttp\Exception\GuzzleException
28
     *
29
     * @return bool
30
     */
31 1
    public function add(string $job, ?string $instance = null): bool
32
    {
33 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

33
        return $this->request('POST', /** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
34
    }
35
36
    /**
37
     * @param string $job
38
     * @param string|null $instance
39
     *
40
     * @throws \GuzzleHttp\Exception\GuzzleException
41
     *
42
     * @return bool
43
     */
44 1
    public function replace(string $job, ?string $instance = null): bool
45
    {
46 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

46
        return $this->request('PUT', /** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
47
    }
48
49
    /**
50
     * @param string $job
51
     * @param string|null $instance
52
     *
53
     * @throws \GuzzleHttp\Exception\GuzzleException
54
     *
55
     * @return bool
56
     */
57 1
    public function delete(string $job, ?string $instance = null): bool
58
    {
59 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

59
        return $this->request('DELETE', /** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
60
    }
61
62
    /**
63
     * @param string $method
64
     * @param string $job
65
     * @param string|null $instance
66
     *
67
     * @throws \GuzzleHttp\Exception\GuzzleException
68
     *
69
     * @return bool
70
     */
71 3
    protected function request(string $method, string $job, ?string $instance = null): bool
72
    {
73 3
        $request = new Request($method, $instance === null ? "job/{$job}" : "job/{$job}/instance/{$instance}");
74 3
        $options = [
75
            'headers' => [
76
                'Content-Type' => TextRenderer::CONTENT_TYPE,
77
            ],
78
        ];
79
80 3
        if ($method !== 'DELETE') {
81 2
            $options['body'] = Utils::streamFor(
82 2
                (new TextRenderer)
83 2
                    ->render($this->registry->collect())
84
            );
85
        }
86
87
        return $this
88 3
                ->client
89 3
                ->send($request, $options)
90 3
                ->getStatusCode() === 200;
91
    }
92
}
93