Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/S3Transfer/Progress/ConsoleProgressBar.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\S3\S3Transfer\Progress;
4
5 final class ConsoleProgressBar implements ProgressBarInterface
6 {
7 public const DEFAULT_PROGRESS_BAR_CHAR = '#';
8 public const DEFAULT_PROGRESS_BAR_WIDTH = 50;
9 public const MAX_PROGRESS_BAR_WIDTH = 50;
10
11 /** @var string */
12 private string $progressBarChar;
13
14 /** @var int */
15 private int $progressBarWidth;
16
17 /** @var int */
18 private int $percentCompleted;
19
20 /** @var AbstractProgressBarFormat */
21 private AbstractProgressBarFormat $progressBarFormat;
22
23 /**
24 * @param string $progressBarChar
25 * @param int $progressBarWidth
26 * @param int $percentCompleted
27 * @param AbstractProgressBarFormat $progressBarFormat
28 */
29 public function __construct(
30 string $progressBarChar = self::DEFAULT_PROGRESS_BAR_CHAR,
31 int $progressBarWidth = self::DEFAULT_PROGRESS_BAR_WIDTH,
32 int $percentCompleted = 0,
33 AbstractProgressBarFormat $progressBarFormat = new ColoredTransferProgressBarFormat(),
34 ) {
35 $this->progressBarChar = $progressBarChar;
36 $this->progressBarWidth = min(
37 $progressBarWidth,
38 self::MAX_PROGRESS_BAR_WIDTH
39 );
40 $this->percentCompleted = $percentCompleted;
41 $this->progressBarFormat = $progressBarFormat;
42 }
43
44 /**
45 * @return string
46 */
47 public function getProgressBarChar(): string
48 {
49 return $this->progressBarChar;
50 }
51
52 /**
53 * @return int
54 */
55 public function getProgressBarWidth(): int
56 {
57 return $this->progressBarWidth;
58 }
59
60 /**
61 * @return int
62 */
63 public function getPercentCompleted(): int
64 {
65 return $this->percentCompleted;
66 }
67
68 /**
69 * @return AbstractProgressBarFormat
70 */
71 public function getProgressBarFormat(): AbstractProgressBarFormat
72 {
73 return $this->progressBarFormat;
74 }
75
76 /**
77 * Set current progress percent.
78 *
79 * @param int $percent
80 *
81 * @return void
82 */
83 public function setPercentCompleted(int $percent): void
84 {
85 $this->percentCompleted = max(0, min(100, $percent));
86 }
87
88 /**
89 * @inheritDoc
90 */
91 public function render(): string
92 {
93 $filledWidth = (int) round(($this->progressBarWidth * $this->percentCompleted) / 100);
94 $progressBar = str_repeat($this->progressBarChar, $filledWidth)
95 . str_repeat(' ', $this->progressBarWidth - $filledWidth);
96
97 // Common arguments
98 $this->progressBarFormat->setArg('progress_bar', $progressBar);
99 $this->progressBarFormat->setArg('percent', $this->percentCompleted);
100
101 return $this->progressBarFormat->format();
102 }
103 }