Issue 1 patch: users:profile PHP notice

This commit is contained in:
Simon Vieille 2015-09-11 20:45:54 +02:00
parent 36c4cc837c
commit 6711f1ae0c
4 changed files with 24 additions and 20 deletions

View file

@ -55,7 +55,7 @@ Usage: <comment>torrents:details</comment> <info>TORRENT_ID</info>");
$output->writeln(sprintf('<info>%s</info>', $data['name']));
$output->writeln('');
$output->writeln(sprintf('Category : <comment>%s</comment>', $data['categoryname']));
$output->writeln(sprintf('Category : <comment>%s</comment>', $data['categoryname']));
foreach ($data['terms'] as $title => $value) {
$output->writeln(sprintf('%-16s: <comment>%s</comment>', $title, $value));

View file

@ -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: <comment>torrents:search</comment> <info>QUERY</info> [OPTIONS]
'[<info>%4d</info><comment>%6d</comment>] [%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)];
}
}

View file

@ -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: <comment>users:profile</comment> [OPTIONS]");
$output->writeln(sprintf(
'DOWN <comment>%sB</comment> UP <comment>%sB</comment> RATIO %s',
$this->getHumainSize($data['downloaded']),
$this->getHumainSize($data['uploaded']),
Formater::humanSize((int) $data['downloaded']),
Formater::humanSize((int) $data['uploaded']),
sprintf(
$ratio > 1 ? '<info>%.2f</info>' : '<error>%.2f</error>',
$ratio
@ -86,12 +87,4 @@ Usage: <comment>users:profile</comment> [OPTIONS]");
$output->writeln(sprintf('An error occured. <error>%s</error>', $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];
}
}

18
src/Helper/Formater.php Normal file
View file

@ -0,0 +1,18 @@
<?php
namespace Helper;
/**
* Class Formater
* @author Simon Vieille <simon@deblan.fr>
*/
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)];
}
}