GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

JobMonitor   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 216
ccs 63
cts 63
cp 1
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A start() 0 5 1
A stop() 0 9 2
A setExitCode() 0 4 1
A getExitCode() 0 4 1
A isCompleted() 0 4 1
A isSuccessful() 0 4 2
A setDuration() 0 4 1
A getDuration() 0 4 1
A setLog() 0 4 1
A clearLog() 0 4 1
A appendLog() 0 4 1
A getLog() 0 4 1
A getRequestBody() 0 20 4
A getRequestUri() 0 4 1
A getRequest() 0 4 1
1
<?php
2
3
namespace Softius\JenkinsJobMonitor;
4
5
use GuzzleHttp\Psr7\Request;
6
7
/**
8
 * Class JobMonitor
9
 * @package Softius\JenkinsJobMonitor
10
 */
11
class JobMonitor
12
{
13
    /**
14
     * Job name as configured in Jenkins
15
     * @var string
16
     */
17
    private $jobName;
18
19
    /**
20
     * The name to be displayed rather than the build number
21
     * @var string
22
     */
23
    private $displayName;
24
25
    /**
26
     * Long description of the build
27
     * @var string
28
     */
29
    private $description;
30
31
    /**
32
     * When the job was started running, in milliseconds
33
     * @var integer
34
     */
35
    private $startedOn;
36
37
    /**
38
     * Returns total duration of job execution, in milliseconds
39
     * @var int
40
     */
41
    private $duration;
42
43
    /**
44
     * Integer indicating the error code. 0 is success and everything else is failure
45
     * @var integer
46
     */
47
    private $exitCode;
48
49
    /**
50
     * Build log
51
     * @var string
52
     */
53
    private $log;
54
55
    /**
56
     * JobMonitor constructor.
57
     * @param string $jobName
58
     * @param string $displayName
59
     * @param string $description
60
     */
61 18
    public function __construct($jobName, $displayName = null, $description = null)
62
    {
63 18
        $this->jobName = $jobName;
64 18
        $this->displayName = $displayName;
65 18
        $this->description = $description;
66
67 18
        $this->exitCode = $this->startedOn = $this->duration = null;
68 18
        $this->clearLog();
69 18
    }
70
71
    /**
72
     * Starts job monitoring
73
     */
74 15
    public function start()
75
    {
76 15
        $this->startedOn = round(microtime(true) * 1000);
0 ignored issues
show
Documentation Bug introduced by
The property $startedOn was declared of type integer, but round(microtime(true) * 1000) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
77 15
        $this->duration = null;
78 15
    }
79
80
    /**
81
     * Stops job monitoring
82
     * @param string $exitCode
83
     * @param string $log if log is provided, existing log data will be overwritten
84
     * @see setLog
85
     * @see appendLog
86
     */
87 15
    public function stop($exitCode, $log = null)
88
    {
89 15
        $completedOn = round(microtime(true) * 1000);
90 15
        $this->setDuration(($completedOn - $this->startedOn));
91 15
        $this->setExitCode($exitCode);
92 15
        if ($log) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $log of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
93 6
            $this->setLog($log);
94 6
        }
95 15
    }
96
97
    /**
98
     * @param int $exitCode
99
     */
100 15
    public function setExitCode($exitCode)
101
    {
102 15
        $this->exitCode = $exitCode;
103 15
    }
104
105
    /**
106
     * Returns exit code
107
     * @return int
108
     */
109 3
    public function getExitCode()
110
    {
111 3
        return $this->exitCode;
112
    }
113
114
    /**
115
     * Returns true only and only this job is completed
116
     * @return boolean
117
     */
118 12
    public function isCompleted()
119
    {
120 12
        return $this->duration !== null;
121
    }
122
123
    /**
124
     * Returns true only and only this job is completed and successfull
125
     * @return boolean
126
     */
127 3
    public function isSuccessful()
128
    {
129 3
        return $this->isCompleted() && $this->exitCode == 0;
130
    }
131
132
    /**
133
     * @param integer $duration Duration in milliseconds
134
     */
135 15
    public function setDuration($duration)
136
    {
137 15
        $this->duration = $duration;
138 15
    }
139
140
    /**
141
     * Returns total duration of job execution, in milliseconds
142
     * @return integer
143
     */
144 9
    public function getDuration()
145
    {
146 9
        return $this->duration;
147
    }
148
149
    /**
150
     * @param $log
151
     */
152 9
    public function setLog($log)
153
    {
154 9
        $this->log = $log;
155 9
    }
156
157
    /**
158
     * Resets logs to empty string
159
     */
160 18
    public function clearLog()
161
    {
162 18
        $this->log = null;
163 18
    }
164
165
    /**
166
     * Appends provided log information to existing log
167
     * @param $log
168
     */
169 3
    public function appendLog($log)
170
    {
171 3
        $this->log = $this->log.$log;
172 3
    }
173
174
    /**
175
     * Returns the log of this job
176
     * @return string
177
     */
178 3
    public function getLog()
179
    {
180 3
        return $this->log;
181
    }
182
183
    /**
184
     * @return string
185
     */
186 9
    private function getRequestBody()
187
    {
188 9
        $xmlTemplate = '<run><log encoding="hexBinary">%s</log><result>%d</result>%s</run>';
189 9
        $encodedLog = current(unpack('H*', $this->log));
190 9
        $xmlElements = null;
191 9
        if ($this->isCompleted()) {
192 6
            $xmlElements .= sprintf('<duration>%d</duration>', $this->getDuration());
193 6
        }
194
195 9
        if ($this->displayName !== null) {
196 3
            $xmlElements .= sprintf('<displayName>%s</displayName>', $this->displayName);
197 3
        }
198
199 9
        if ($this->description !== null) {
200 3
            $xmlElements .= sprintf('<description>%s</description>', $this->description);
201 3
        }
202
203 9
        $xmlRequest = sprintf($xmlTemplate, $encodedLog, $this->exitCode, $xmlElements);
204 9
        return $xmlRequest;
205
    }
206
207
    /**
208
     * Constructs and returns the URI, where the monitor data must sent
209
     *
210
     * @return string
211
     */
212 9
    private function getRequestUri()
213
    {
214 9
        return sprintf('/job/%s/postBuildResult', $this->jobName);
215
    }
216
217
    /**
218
     * Constructs and returns the Monitor request to be to Jenkins
219
     *
220
     * @return Psr\Http\Message\RequestInterface
221
     */
222 9
    public function getRequest()
223
    {
224 9
        return new Request('POST', $this->getRequestUri(), [], $this->getRequestBody());
225
    }
226
}
227