-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CacheCommand to facilitate some things
- Loading branch information
Showing
3 changed files
with
68 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace QIT_CLI\Commands; | ||
|
||
use QIT_CLI\App; | ||
use QIT_CLI\Cache; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class CacheCommand extends Command { | ||
protected static $defaultName = 'cache'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase | ||
|
||
protected function configure() { | ||
$this | ||
->addArgument( 'action', InputArgument::REQUIRED, 'The action to perform. (get, set, delete)' ) | ||
->addArgument( 'key', InputArgument::REQUIRED, 'The key to cache.' ) | ||
->addArgument( 'value', InputArgument::OPTIONAL, 'The value when (set).' ) | ||
->addArgument( 'expiration', InputArgument::OPTIONAL, 'The expiration when (set).' ) | ||
->setDescription( 'Manipulates the QIT Cache' ); | ||
} | ||
|
||
protected function execute( InputInterface $input, OutputInterface $output ): int { | ||
$action = $input->getArgument( 'action' ); | ||
$key = $input->getArgument( 'key' ); | ||
$cache = App::make( Cache::class ); | ||
|
||
switch ( $action ) { | ||
case 'get': | ||
$output->writeln( $cache->get( $key ) ); | ||
break; | ||
case 'set': | ||
// Check expiration is set. | ||
if ( ! $input->hasArgument( 'expiration' ) ) { | ||
$output->writeln( 'Expiration is required when setting a cache value.' ); | ||
|
||
return Command::FAILURE; | ||
} | ||
// Validate expiration is an integer. | ||
if ( ! is_numeric( $input->getArgument( 'expiration' ) ) ) { | ||
$output->writeln( 'Expiration must be an integer.' ); | ||
|
||
return Command::FAILURE; | ||
} | ||
// Check value is set. | ||
if ( ! $input->hasArgument( 'value' ) ) { | ||
$output->writeln( 'Value is required when setting a cache value.' ); | ||
|
||
return Command::FAILURE; | ||
} | ||
$cache->set( $key, $input->getArgument( 'value' ), (int) $input->getArgument( 'expiration' ) ); | ||
break; | ||
case 'delete': | ||
$cache->delete( $key ); | ||
break; | ||
default: | ||
$output->writeln( 'Invalid action.' ); | ||
break; | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters