CheckCollection::startNextCheck()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\ServerMonitor;
4
5
use Countable;
6
use Illuminate\Support\Collection;
7
use Spatie\ServerMonitor\Helpers\ConsoleOutput;
8
use Spatie\ServerMonitor\Models\Check;
9
10
class CheckCollection implements Countable
11
{
12
    /** @var \Illuminate\Support\Collection */
13
    protected $pendingChecks;
14
15
    /** @var \Illuminate\Support\Collection */
16
    protected $runningChecks;
17
18
    public function __construct(Collection $checks)
19
    {
20
        $this->pendingChecks = $checks;
21
22
        $this->runningChecks = collect();
23
    }
24
25
    public function runAll()
26
    {
27
        while ($this->pendingChecks->isNotEmpty() || $this->runningChecks->isNotEmpty()) {
28
            if ($this->runningChecks->count() < config('server-monitor.concurrent_ssh_connections')) {
29
                $this->startNextCheck();
30
            }
31
32
            $this->handleFinishedChecks();
33
        }
34
    }
35
36
    protected function startNextCheck()
37
    {
38
        if ($this->pendingChecks->isEmpty()) {
39
            return;
40
        }
41
42
        $check = $this->pendingChecks->shift();
43
44
        ConsoleOutput::comment($check->host->name.": performing check `{$check->type}`...");
45
46
        $check->getProcess()->start();
47
48
        $this->runningChecks->push($check);
49
    }
50
51
    protected function handleFinishedChecks()
52
    {
53
        [$this->runningChecks, $finishedChecks] = $this->runningChecks->partition(function (Check $check) {
0 ignored issues
show
Bug introduced by
The variable $finishedChecks does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
54
            return $check->getProcess()->isRunning();
55
        });
56
57
        $finishedChecks->each->handleFinishedProcess();
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function count()
64
    {
65
        return count($this->pendingChecks);
66
    }
67
}
68