Issues (11)

src/Progress/RedisProgress.php (2 issues)

1
<?php
2
3
namespace Tequilarapido\Consolify\Progress;
4
5
use Symfony\Component\Console\Helper\Helper;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
8
class RedisProgress implements Progress
9
{
10
    /** @var ProgressBar */
11
    protected $bar;
12
13
    /** @var string */
14
    protected $uid;
15
16
    /** @var SleepModeState */
17
    protected $sleepModeState;
18
19
    public function setProgressBar(ProgressBar $bar)
20
    {
21
        $this->bar = $bar;
22
23
        return $this;
24
    }
25
26
    public function setUid($uid)
27
    {
28
        $this->uid = $uid;
29
30
        return $this;
31
    }
32
33
    public function advance($step = 1)
34
    {
35
        $this->bar->advance($step);
36
        $this->persistProgress();
37
    }
38
39
    public function start($max = null)
40
    {
41
        $this->bar->start($max);
42
        $this->persistProgress();
43
    }
44
45
    public function setSleepModeState(SleepModeState $sleepModeState)
46
    {
47
        $this->sleepModeState = $sleepModeState;
48
49
        // Progress bar is not advanced here as we are sleep
50
        // so we need to persist progress manually, so the UI can keep up with sleep timing.
51
        $this->persistProgress();
52
    }
53
54
    public function summary()
55
    {
56
        return [
57
            'elapsed' => Helper::formatTime($this->elapsed()),
58
            'estimated' => Helper::formatTime($this->estimated()),
59
            'mremory' => Helper::formatMemory(memory_get_usage(true)),
60
            'current' => $this->bar->getProgress(),
61
            'max' => $this->bar->getMaxSteps(),
62
            'percent' => $this->percent(),
63
            'message' => $this->bar->getMessage(),
64
            'sleepMode' => $this->sleepModeState ? $this->sleepModeState->toArray() : null,
65
        ];
66
    }
67
68
    protected function persistProgress()
69
    {
70
        static::redis()->set(
71
            static::prefixedKey($this->uid),
72
            json_encode($this->summary())
73
        );
74
    }
75
76
    public function getPersisted()
77
    {
78
        $result = static::redis()->get(static::prefixedKey($this->uid));
79
80
        return $result ? json_decode($result) : null;
81
    }
82
83
    public function deletePersisted()
84
    {
85
        $pattern = static::prefixedKey($this->uid) . '*';
86
87
        if (!empty($keys = static::redis()->keys($pattern))) {
0 ignored issues
show
The assignment to $keys is dead and can be removed.
Loading history...
88
            static::redis()->del(static::redis()->keys($pattern));
89
        }
90
    }
91
92
    public function __call($name, $arguments)
93
    {
94
        return call_user_func_array([$this->bar, $name], $arguments);
95
    }
96
97
    public function finishProgress()
98
    {
99
        $this->progress->finish();
0 ignored issues
show
Bug Best Practice introduced by
The property progress does not exist on Tequilarapido\Consolify\Progress\RedisProgress. Did you maybe forget to declare it?
Loading history...
100
    }
101
102
    protected static function prefixedKey($key)
103
    {
104
        return config('consolify.progress.redis_prefix') . $key;
105
    }
106
107
    protected function elapsed()
108
    {
109
        return time() - $this->bar->getStartTime();
110
    }
111
112
    protected function estimated()
113
    {
114
        if (!$this->bar->getMaxSteps()) {
115
            return;
116
        }
117
118
        return !$this->bar->getProgress()
119
            ? 0
120
            : round((time() - $this->bar->getStartTime()) / $this->bar->getProgress() * $this->bar->getMaxSteps());
121
    }
122
123
    protected function percent()
124
    {
125
        return floor($this->bar->getProgressPercent() * 100);
126
    }
127
128
    /**
129
     * return Redis connection.
130
     *
131
     * @return \Illuminate\Redis\Connections\Connection
132
     */
133
    protected static function redis()
134
    {
135
        return app('redis')->connection();
136
    }
137
}
138