mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-20 01:09:56 +03:00
Fixed phpstan errors in ListKeysCommand
This commit is contained in:
parent
e024ba5d94
commit
4f3995ea80
2 changed files with 34 additions and 36 deletions
|
@ -50,74 +50,72 @@ class ListKeysCommand extends Command
|
||||||
$list = $this->apiKeyService->listKeys($enabledOnly);
|
$list = $this->apiKeyService->listKeys($enabledOnly);
|
||||||
|
|
||||||
$table = new Table($output);
|
$table = new Table($output);
|
||||||
if ($enabledOnly) {
|
$table->setHeaders(array_filter([
|
||||||
$table->setHeaders([
|
$this->translator->translate('Key'),
|
||||||
$this->translator->translate('Key'),
|
! $enabledOnly ? $this->translator->translate('Is enabled') : null,
|
||||||
$this->translator->translate('Expiration date'),
|
$this->translator->translate('Expiration date'),
|
||||||
]);
|
]));
|
||||||
} else {
|
|
||||||
$table->setHeaders([
|
|
||||||
$this->translator->translate('Key'),
|
|
||||||
$this->translator->translate('Is enabled'),
|
|
||||||
$this->translator->translate('Expiration date'),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @var ApiKey $row */
|
/** @var ApiKey $row */
|
||||||
foreach ($list as $row) {
|
foreach ($list as $row) {
|
||||||
$key = $row->getKey();
|
$key = $row->getKey();
|
||||||
$expiration = $row->getExpirationDate();
|
$expiration = $row->getExpirationDate();
|
||||||
$rowData = [];
|
$rowData = [];
|
||||||
$formatMethod = ! $row->isEnabled()
|
$formatMethod = $this->determineFormatMethod($row);
|
||||||
? 'getErrorString'
|
|
||||||
: ($row->isExpired() ? 'getWarningString' : 'getSuccessString');
|
|
||||||
|
|
||||||
if ($enabledOnly) {
|
$rowData[] = $formatMethod($key);
|
||||||
$rowData[] = $this->{$formatMethod}($key);
|
if (! $enabledOnly) {
|
||||||
} else {
|
$rowData[] = $formatMethod($this->getEnabledSymbol($row));
|
||||||
$rowData[] = $this->{$formatMethod}($key);
|
|
||||||
$rowData[] = $this->{$formatMethod}($this->getEnabledSymbol($row));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$rowData[] = isset($expiration) ? $expiration->format(\DateTime::ATOM) : '-';
|
$rowData[] = $expiration !== null ? $expiration->format(\DateTime::ATOM) : '-';
|
||||||
$table->addRow($rowData);
|
$table->addRow($rowData);
|
||||||
}
|
}
|
||||||
|
|
||||||
$table->render();
|
$table->render();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private function determineFormatMethod(ApiKey $apiKey): callable
|
||||||
* @param string $string
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function getErrorString($string)
|
|
||||||
{
|
{
|
||||||
return sprintf('<fg=red>%s</>', $string);
|
if (! $apiKey->isEnabled()) {
|
||||||
|
return [$this, 'getErrorString'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $apiKey->isExpired() ? [$this, 'getWarningString'] : [$this, 'getSuccessString'];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $string
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getSuccessString($string)
|
private function getErrorString(string $value): string
|
||||||
{
|
{
|
||||||
return sprintf('<info>%s</info>', $string);
|
return sprintf('<fg=red>%s</>', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $string
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getWarningString($string)
|
private function getSuccessString(string $value): string
|
||||||
{
|
{
|
||||||
return sprintf('<comment>%s</comment>', $string);
|
return sprintf('<info>%s</info>', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $value
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getWarningString(string $value): string
|
||||||
|
{
|
||||||
|
return sprintf('<comment>%s</comment>', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ApiKey $apiKey
|
* @param ApiKey $apiKey
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getEnabledSymbol(ApiKey $apiKey)
|
private function getEnabledSymbol(ApiKey $apiKey): string
|
||||||
{
|
{
|
||||||
return ! $apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++';
|
return ! $apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++';
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ class ApiKey extends AbstractEntity
|
||||||
*/
|
*/
|
||||||
protected $key;
|
protected $key;
|
||||||
/**
|
/**
|
||||||
* @var \DateTime
|
* @var \DateTime|null
|
||||||
* @ORM\Column(name="expiration_date", nullable=true, type="datetime")
|
* @ORM\Column(name="expiration_date", nullable=true, type="datetime")
|
||||||
*/
|
*/
|
||||||
protected $expirationDate;
|
protected $expirationDate;
|
||||||
|
@ -60,7 +60,7 @@ class ApiKey extends AbstractEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \DateTime
|
* @return \DateTime|null
|
||||||
*/
|
*/
|
||||||
public function getExpirationDate()
|
public function getExpirationDate()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue