setName('users:profile')
->setDescription('Show a user\'s profile')
->addOption('id', 'i', InputOption::VALUE_REQUIRED, 'The user id')
->setHelp("%command.name%
Show a user's profile (default is the auhentificated user).
Usage: users:profile [OPTIONS]");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = new Client();
$configLoader = new ConfigLoader();
if (!isset($configLoader->getConfig()['auth']['token'])) {
$output->writeln('You must login.');
return;
}
$client->setAuthorization($configLoader->getConfig()['auth']['token']);
try {
$userId = (int) ($input->getOption('id') ? $input->getOption('id') : $configLoader->getConfig()['auth']['uid']);
$response = $client->getUserProfile($userId);
if ($response->hasError()) {
$output->writeln(sprintf(
'%s (%d)',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
$data = $response->getData();
if (isset($data['username'])) {
$output->writeln(sprintf('Username : %s', $data['username']));
}
if (isset($data['gender'])) {
$output->writeln(sprintf('Gender : %s', $data['gender']));
}
if (isset($data['age'])) {
$output->writeln(sprintf('Age : %s', $data['age']));
}
if (isset($data['downloaded'], $data['uploaded'])) {
$output->writeln('');
$ratio = $data['uploaded'] / $data['downloaded'];
$output->writeln(sprintf(
'DOWN %sB UP %sB RATIO %s',
$this->getHumainSize($data['downloaded']),
$this->getHumainSize($data['uploaded']),
sprintf(
$ratio > 1 ? '%.2f' : '%.2f',
$ratio
)
));
}
} catch (ClientException $e) {
$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];
}
}