From 6711f1ae0cffca7df471258d23325814e1abb29b Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 11 Sep 2015 20:45:54 +0200 Subject: [PATCH] Issue 1 patch: users:profile PHP notice --- src/Console/Command/TorrentsDetailsCommand.php | 2 +- src/Console/Command/TorrentsSearchCommand.php | 11 ++--------- src/Console/Command/UsersProfileCommand.php | 13 +++---------- src/Helper/Formater.php | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 src/Helper/Formater.php diff --git a/src/Console/Command/TorrentsDetailsCommand.php b/src/Console/Command/TorrentsDetailsCommand.php index 20ef329..0da8608 100644 --- a/src/Console/Command/TorrentsDetailsCommand.php +++ b/src/Console/Command/TorrentsDetailsCommand.php @@ -55,7 +55,7 @@ Usage: torrents:details TORRENT_ID"); $output->writeln(sprintf('%s', $data['name'])); $output->writeln(''); - $output->writeln(sprintf('Category : %s', $data['categoryname'])); + $output->writeln(sprintf('Category : %s', $data['categoryname'])); foreach ($data['terms'] as $title => $value) { $output->writeln(sprintf('%-16s: %s', $title, $value)); diff --git a/src/Console/Command/TorrentsSearchCommand.php b/src/Console/Command/TorrentsSearchCommand.php index 6360895..9baafdb 100644 --- a/src/Console/Command/TorrentsSearchCommand.php +++ b/src/Console/Command/TorrentsSearchCommand.php @@ -11,6 +11,7 @@ use Api\Client; use Api\ConfigLoader; use Api\ClientResponse; use Api\ClientException; +use Helper\Formater; class TorrentsSearchCommand extends Command { @@ -160,18 +161,10 @@ Usage: torrents:search QUERY [OPTIONS] '[%4d%6d] [%8s] %7d %s', $torrent['seeders'], $torrent['leechers'], - $this->formatBytes((int) $torrent['size']), + Formater::humanSize((int) $torrent['size']), $torrent['id'], $torrent['name'] )); } } - - protected function formatBytes($size, $precision = 2) - { - $base = log($size, 1024); - $suffixes = array('', 'kB', 'MB', 'GB', 'TB'); - - return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)]; - } } diff --git a/src/Console/Command/UsersProfileCommand.php b/src/Console/Command/UsersProfileCommand.php index 996f164..15298b1 100644 --- a/src/Console/Command/UsersProfileCommand.php +++ b/src/Console/Command/UsersProfileCommand.php @@ -9,6 +9,7 @@ use Api\Client; use Api\ConfigLoader; use Symfony\Component\Console\Input\InputOption; use Api\ClientException; +use Helper\Formater; class UsersProfileCommand extends Command { @@ -74,8 +75,8 @@ Usage: users:profile [OPTIONS]"); $output->writeln(sprintf( 'DOWN %sB UP %sB RATIO %s', - $this->getHumainSize($data['downloaded']), - $this->getHumainSize($data['uploaded']), + Formater::humanSize((int) $data['downloaded']), + Formater::humanSize((int) $data['uploaded']), sprintf( $ratio > 1 ? '%.2f' : '%.2f', $ratio @@ -86,12 +87,4 @@ Usage: users:profile [OPTIONS]"); $output->writeln(sprintf('An error occured. %s', $e->getMessage())); } } - - protected function getHumainSize($bytes, $decimals = 2) - { - $sizes = 'BKMGTP'; - $factor = floor((strlen($bytes) - 1) / 3); - - return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)).$sizes[$factor]; - } } diff --git a/src/Helper/Formater.php b/src/Helper/Formater.php new file mode 100644 index 0000000..209f092 --- /dev/null +++ b/src/Helper/Formater.php @@ -0,0 +1,18 @@ + + */ +class Formater +{ + public static function humanSize($size, $precision = 2) + { + $base = log($size, 1024); + $suffixes = array('', 'kB', 'MB', 'GB', 'TB'); + + return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)]; + } +}