ResponseWaitter   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 93
Duplicated Lines 25.81 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 3
dl 24
loc 93
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTimeout() 0 4 1
A setTimeout() 0 4 1
A setProgressBar() 0 4 1
A progressBarStart() 0 4 2
A progressBarSetProgress() 0 4 2
A progressBarFinish() 0 4 2
A updateTimeleft() 0 9 2
A isAvailable() 0 10 2
A waitUntilAvailable() 12 12 3
A waitUntilNotAvailable() 12 12 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace BeubiQA\Application\Lib;
4
5
use BeubiQA\Application\Exception\Timeout;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\ConnectException;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
10
class ResponseWaitter
11
{
12
    /** @var Client */
13
    private $httpClient;
14
    /** @var ProgressBar */
15
    private $progressBar;
16
    private $timeout = 30000000;
17
    private $waitInterval = 25000; // 0.025 seconds
18
19
    public function __construct(Client $httpClient, $timeoutSeconds = 30000000, $waitInterval = 25000)
20
    {
21
        $this->setTimeout($timeoutSeconds);
22
        $this->waitInterval = $waitInterval;
23
        $this->httpClient = $httpClient;
24
    }
25
26
    public function getTimeout()
27
    {
28
        return $this->timeout;
29
    }
30
31
    public function setTimeout($timeoutSeconds)
32
    {
33
        $this->timeout = (int) $timeoutSeconds * 1000000;
34
    }
35
36
    public function setProgressBar(ProgressBar $progressBar)
37
    {
38
        $this->progressBar = $progressBar;
39
    }
40
41
    private function progressBarStart()
42
    {
43
        $this->progressBar ? $this->progressBar->start($this->timeout) : '';
44
    }
45
46
    private function progressBarSetProgress()
47
    {
48
        $this->progressBar ? $this->progressBar->setProgress($this->timeout - $this->timeLeft) : '';
49
    }
50
51
    private function progressBarFinish()
52
    {
53
        $this->progressBar ? $this->progressBar->finish() : '';
54
    }
55
56
    private function updateTimeleft()
57
    {
58
        $this->timeLeft -= $this->waitInterval;
59
        if ($this->timeLeft < 0) {
60
            throw new Timeout('Timeout of '.var_export($this->timeout, true).' seconds.');
61
        }
62
        usleep($this->waitInterval);
63
        $this->progressBarSetProgress();
64
    }
65
66
    public function isAvailable($url, $requestOptions)
67
    {
68
        try {
69
            $this->httpClient->get($url, $requestOptions);
70
        } catch (ConnectException $e) {
71
            return false;
72
        }
73
74
        return true;
75
    }
76
77 View Code Duplication
    public function waitUntilAvailable($url, $requestOptions)
78
    {
79
        $this->progressBarStart();
80
        $this->timeLeft = $this->timeout;
81
        while (true) {
82
            $this->updateTimeleft();
83
            if ($this->isAvailable($url, $requestOptions)) {
84
                break;
85
            }
86
        }
87
        $this->progressBarFinish();
88
    }
89
90 View Code Duplication
    public function waitUntilNotAvailable($url, $requestOptions)
91
    {
92
        $this->progressBarStart();
93
        $this->timeLeft = $this->timeout;
94
        while (true) {
95
            $this->updateTimeleft();
96
            if (!$this->isAvailable($url, $requestOptions)) {
97
                break;
98
            }
99
        }
100
        $this->progressBarFinish();
101
    }
102
}
103