diff --git a/src/Console/Command/UsersProfileCommand.php b/src/Console/Command/UsersProfileCommand.php new file mode 100644 index 0000000..24060f8 --- /dev/null +++ b/src/Console/Command/UsersProfileCommand.php @@ -0,0 +1,93 @@ +setName('users:profile') + ->setDescription('') + ->addOption('id', 'i', InputOption::VALUE_REQUIRED, '') + ->setHelp("%command.name% +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]; + } +}