-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK SIO-4352 Add info about traffic
- Loading branch information
Dmitrii Romashov
committed
Jul 27, 2022
1 parent
8b08e02
commit 75d97c4
Showing
6 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace WHMCS\Module\Server\SolusIoVps\Helpers; | ||
|
||
use InvalidArgumentException; | ||
|
||
class Unit | ||
{ | ||
public const KiB = 'KiB'; | ||
public const MiB = 'MiB'; | ||
public const GiB = 'GiB'; | ||
public const TiB = 'TiB'; | ||
public const PiB = 'PiB'; | ||
|
||
private const MULTIPLIERS = [ | ||
self::KiB => 1024, | ||
self::MiB => 1024*1024, | ||
self::GiB => 1024*1024*1024, | ||
self::TiB => 1024*1024*1024*1024, | ||
self::PiB => 1024*1024*1024*1024*1024, | ||
]; | ||
|
||
public static function convert(int $bytes, string $unit, int $decimal = 2): float | ||
{ | ||
if (!array_key_exists($unit, self::MULTIPLIERS)) { | ||
throw new InvalidArgumentException(sprintf('Unknown unit: "%s"', $unit)); | ||
} | ||
|
||
$result = $bytes / self::MULTIPLIERS[$unit]; | ||
|
||
if ($decimal === 0) { | ||
return floor($result); | ||
} | ||
|
||
$k = 10**$decimal; | ||
|
||
return floor($result*$k)/$k; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Tests\lib\Helpers; | ||
|
||
use InvalidArgumentException; | ||
use Tests\AbstractModuleTest; | ||
use WHMCS\Module\Server\SolusIoVps\Helpers\Unit; | ||
|
||
class UnitTest extends AbstractModuleTest | ||
{ | ||
/** | ||
* @dataProvider convertDataProvider | ||
*/ | ||
public function testConvert(int $bytes, string $unit, float $expected): void | ||
{ | ||
self::assertEquals($expected, Unit::convert($bytes, $unit)); | ||
} | ||
|
||
public function convertDataProvider(): array | ||
{ | ||
return [ | ||
[1024, Unit::KiB, 1], | ||
[100*1024*1024, Unit::MiB, 100], | ||
[30*1024*1024, Unit::KiB, 30*1024], | ||
[512*1024*1024, Unit::GiB, 0.5], | ||
[(int)(0.5*1024*1024*1024), Unit::MiB, 512], | ||
[1023*1024*1024, Unit::GiB, 0.99], | ||
[1025*1024*1024, Unit::GiB, 1], | ||
[1988*1024*1024, Unit::GiB, 1.94], | ||
]; | ||
} | ||
|
||
public function testConvertUnknownUnit(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Unknown unit: "fake"'); | ||
Unit::convert(42, 'fake'); | ||
} | ||
} |