Completed
Push — countdown ( 5f4254...b0d53e )
by Craig
55:40 queued 20:35
created

Countdown   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A from() 0 5 1
A run() 0 30 5
1
<?php
2
3
namespace League\CLImate\TerminalObject\Dynamic;
4
5
use function sleep;
6
7
final class Countdown extends DynamicTerminalObject
8
{
9
    /** @var string */
10
    private $label = "";
11
12
    /** @var int */
13
    private $from = 5;
14
15
16
    /**
17
     * @param string $label
18
     */
19
    public function __construct(string $label = "Starting in... ")
20
    {
21
        $this->label = $label;
22
    }
23
24
25
    /**
26
     * @param int $from
27
     *
28
     * @return self
29
     */
30
    public function from(int $from): self
31
    {
32
        $this->from = $from;
33
        return $this;
34
    }
35
36
37
    /**
38
     * @return void
39
     */
40
    public function run(): void
41
    {
42
        $firstLine = true;
43
        $i = $this->from;
44
        while (true) {
45
46
            $content = "";
47
48
            if ($firstLine) {
49
                $firstLine = false;
50
            } else {
51
                $content .= $this->util->cursor->up(1);
52
                $content .= $this->util->cursor->startOfCurrentLine();
53
                $content .= $this->util->cursor->deleteCurrentLine();
54
            }
55
56
            $content .= $this->label;
57
            if ($i > 0) {
58
                $content .= $i;
59
            }
60
61
            $this->output->write($this->parser->apply($content));
62
            if ($i < 1) {
63
                break;
64
            }
65
66
            sleep(1);
67
            --$i;
68
        }
69
    }
70
}
71