DurationRule::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Http;
4
5
use phm\HttpWebdriverClient\Http\Response\DurationAwareResponse;
6
use Psr\Http\Message\ResponseInterface;
7
use whm\Smoke\Rules\CheckResult;
8
use whm\Smoke\Rules\Rule;
9
10
/**
11
 * This rule can validate if a http request takes longer than a given max duration.
12
 * A website that is slower than one second is considered as slow.
13
 */
14
class DurationRule implements Rule
15
{
16
    private $maxDuration;
17
18
    /**
19
     * @param int $maxDuration The maximum duration a http call is allowed to take (time to first byte)
20
     */
21
    public function init($maxDuration = 1000)
22
    {
23
        $this->maxDuration = $maxDuration;
24
    }
25
26
    public function validate(ResponseInterface $response)
27
    {
28
        if ($response instanceof DurationAwareResponse) {
29
            if ($response->getDuration() > $this->maxDuration) {
30
                return new CheckResult(
31
                    CheckResult::STATUS_FAILURE,
32
                    'The http request took ' . (int)$response->getDuration() . ' milliseconds (limit was ' . $this->maxDuration . 'ms).',
33
                    (int)$response->getDuration());
34
            }
35
36
            return new CheckResult(
37
                CheckResult::STATUS_SUCCESS,
38
                'The http request took ' . (int)$response->getDuration() . ' milliseconds (limit was ' . $this->maxDuration . 'ms).',
39
                (int)$response->getDuration());
40
        }
41
    }
42
}
43