ZabbixGraph::width()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace CasperBoone\ZabbixGraph;
4
5
use DateTime;
6
use GuzzleHttp\Client as HttpClient;
7
8
class ZabbixGraph
9
{
10
    protected $httpClient;
11
    protected $oldZabbix;
12
    protected $username;
13
    protected $password;
14
15
    protected $width = 400;
16
    protected $height = 200;
17
    protected $startTime;
18
    protected $endTime;
19
20
    /**
21
     * Construct and initalize ZabbixGraph.
22
     *
23
     * @param  string  $url        Full url of Zabbix location
24
     * @param  string  $username   Zabbix username
25
     * @param  string  $password   Zabbix password
26
     * @param  bool    $oldZabbix  Set to true if using Zabbix 1.8 or older, by default set to false
27
     */
28 7
    public function __construct($url, $username, $password, $oldZabbix = false)
29
    {
30 7
        $this->httpClient = $this->createHttpClient($url);
31 7
        $this->oldZabbix = $oldZabbix;
32 7
        $this->username = $username;
33 7
        $this->password = $password;
34
35 7
        $this->startTime = (new DateTime())->modify('-1 hour');
36 7
        $this->endTime = new DateTime();
37 7
    }
38
39
    /**
40
     * Create HTTP client for requesting the graph. The HTTP client should preserve cookies.
41
     *
42
     * @param  string  $url  Full url of Zabbix location
43
     * @return HttpClient
44
     */
45 1
    protected function createHttpClient($url)
46
    {
47 1
        return new HttpClient([
48 1
            'base_uri' => $url,
49
            'cookies' => true,
50
        ]);
51
    }
52
53
    /**
54
     * Request graph from Zabbix and return a raw image. If an error
55
     * occurred Zabbix will output this as an image.
56
     *
57
     * @param  int  $graphId  ID of the graph to be requested
58
     * @return string
59
     */
60 7
    public function find($graphId)
61
    {
62 7
        $this->login();
63
64 7
        return $this->httpClient->get('chart2.php', [
65
            'query' => [
66 7
                'graphid' => $graphId,
67 7
                'width' => $this->width,
68 7
                'height' => $this->height,
69 7
                'stime' => $this->startTime->getTimestamp(),
70 7
                'period' => $this->endTime->getTimestamp() - $this->startTime->getTimestamp(),
71
            ],
72 7
        ])->getBody()->getContents();
73
    }
74
75
    /**
76
     * Login to Zabbix to acquire the needed session for requesting the graph.
77
     */
78 7
    protected function login()
79
    {
80 7
        $this->httpClient->post('index.php', [
81
            'form_params' => [
82 7
                'name' => $this->username,
83 7
                'password' => $this->password,
84 7
                'enter' => 'Sign in',
85
            ],
86
        ]);
87 7
    }
88
89
    /**
90
     * Specify width of the graph in pixels, by default 400.
91
     *
92
     * @param  int  $width  Width in pixels
93
     * @return $this
94
     */
95 2
    public function width($width)
96
    {
97 2
        $this->width = $width;
98
99 2
        return $this;
100
    }
101
102
    /**
103
     * Specify height of the graph in pixels, by default 400.
104
     *
105
     * @param  int  $height  Height in pixels
106
     * @return $this
107
     */
108 2
    public function height($height)
109
    {
110 2
        $this->height = $height;
111
112 2
        return $this;
113
    }
114
115
    /**
116
     * Specify start date and time of the data displayed in the graph.
117
     *
118
     * @param  DateTime  $startTime  Start date and time
119
     * @return $this
120
     */
121 2
    public function startTime(DateTime $startTime)
122
    {
123 2
        $this->startTime = $startTime;
124
125 2
        return $this;
126
    }
127
128
    /**
129
     * Specify end date and time of the data displayed in the graph.
130
     *
131
     * @param  DateTime  $endTime  End date and time
132
     * @return $this
133
     */
134 1
    public function endTime(DateTime $endTime)
135
    {
136 1
        $this->endTime = $endTime;
137
138 1
        return $this;
139
    }
140
}
141