Skip to content

Commit

Permalink
Refactor how Cache is used to remove dependency on ManagerBackend
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc45 committed Feb 22, 2024
1 parent bc6f733 commit b428a6b
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 107 deletions.
26 changes: 13 additions & 13 deletions src/src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
namespace QIT_CLI;

class Auth {
/** @var ManagerBackend $manager_backend */
protected $manager_backend;
/** @var Cache $cache */
protected $cache;

public function __construct( ManagerBackend $manager_backend ) {
$this->manager_backend = $manager_backend;
public function __construct( Cache $cache ) {
$this->cache = $cache;
}

/**
* @return string|null base64 encoded string of user:application_password, or null if not defined.
*/
public function get_partner_auth() {
// Migrate "application_password" to "qit_token" if it exists.
if ( empty( $this->manager_backend->get_cache()->get( 'qit_token' ) ) && ! empty( $this->manager_backend->get_cache()->get( 'application_password' ) ) ) {
$this->manager_backend->get_cache()->set( 'qit_token', $this->manager_backend->get_cache()->get( 'application_password' ), - 1 );
if ( empty( $this->cache->get( 'qit_token' ) ) && ! empty( $this->cache->get( 'application_password' ) ) ) {
$this->cache->set( 'qit_token', $this->cache->get( 'application_password' ), - 1 );
}

$user = $this->manager_backend->get_cache()->get( 'user' );
$qit_token = $this->manager_backend->get_cache()->get( 'qit_token' );
$user = $this->cache->get( 'user' );
$qit_token = $this->cache->get( 'qit_token' );

if ( ! empty( $user ) && ! empty( $qit_token ) ) {
return base64_encode( sprintf( '%s:%s', $user, $qit_token ) );
Expand All @@ -33,7 +33,7 @@ public function get_partner_auth() {
* @return string|null MANAGER_SECRET, or null if not defined.
*/
public function get_manager_secret() {
return $this->manager_backend->get_cache()->get( 'manager_secret' );
return $this->cache->get( 'manager_secret' );
}

/**
Expand All @@ -43,8 +43,8 @@ public function get_manager_secret() {
* @return void
*/
public function set_partner_auth( $user, $qit_token ): void {
$this->manager_backend->get_cache()->set( 'user', $user, - 1 );
$this->manager_backend->get_cache()->set( 'qit_token', $qit_token, - 1 );
$this->cache->set( 'user', $user, - 1 );
$this->cache->set( 'qit_token', $qit_token, - 1 );
}

/**
Expand All @@ -53,10 +53,10 @@ public function set_partner_auth( $user, $qit_token ): void {
* @return void
*/
public function set_manager_secret( $manager_secret ): void {
$this->manager_backend->get_cache()->set( 'manager_secret', $manager_secret, - 1 );
$this->cache->set( 'manager_secret', $manager_secret, - 1 );
}

public function delete_manager_secret(): void {
$this->manager_backend->get_cache()->delete( 'manager_secret' );
$this->cache->delete( 'manager_secret' );
}
}
19 changes: 4 additions & 15 deletions src/src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@ class Cache {
/** @var array<scalar|array<scalar>> $cache */
protected $cache = [];

/** @var ManagerBackend $manager_backend */
protected $manager_backend;

/** @var bool */
protected $did_init = false;

/** @var string */
protected $cache_file_path;

public function __construct() {
$this->cache_file_path = $this->make_cache_path_for_manager_environment( Config::get_current_manager_backend() );
$this->init_cache();
}

public function get_cache_file_path(): string {
return $this->cache_file_path;
}

public function make_cache_path_for_manager_environment( string $manager_backend ): string {
public function make_cache_path( string $manager_backend ): string {
return Config::get_qit_dir() . ".env-$manager_backend.json";
}

Expand All @@ -38,10 +31,6 @@ public function make_cache_path_for_manager_environment( string $manager_backend
* @return void
*/
public function set( string $key, $value, int $expire ): void {
if ( ! $this->did_init ) {
throw new \LogicException( 'Cache not initialized.' );
}

if ( $expire !== - 1 ) {
$expire = time() + $expire;
}
Expand Down Expand Up @@ -107,8 +96,8 @@ public function delete( string $key ) {
*
* @throws \RuntimeException When could not read the cache file.
*/
protected function init_cache(): void {
$this->did_init = true;
public function init_cache(): void {
$this->cache_file_path = static::make_cache_path( Config::get_current_manager_backend() );

if ( ! file_exists( $this->cache_file_path ) ) {
return;
Expand All @@ -123,7 +112,7 @@ protected function init_cache(): void {
$cache = json_decode( $data, true );

if ( ! is_array( $cache ) ) {
throw new \RuntimeException( 'Could not parse cache file. Resetting environment, please remove the current Partner/ManagerBackend and add it again.' );
throw new \RuntimeException( 'Could not parse cache file. Resetting environment, please remove the current Partner/Manager backend and add it again.' );
}

$this->cache = $cache;
Expand Down
11 changes: 8 additions & 3 deletions src/src/Commands/Backend/AddBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace QIT_CLI\Commands\Backend;

use QIT_CLI\Auth;
use QIT_CLI\Cache;
use QIT_CLI\ManagerBackend;
use QIT_CLI\WooExtensionsList;
use Symfony\Component\Console\Command\Command;
Expand All @@ -25,10 +26,14 @@ class AddBackend extends Command {
/** @var ManagerBackend $manager_backend */
protected $manager_backend;

public function __construct( ManagerBackend $manager_backend, Auth $auth, WooExtensionsList $woo_extensions_list ) {
/** @var Cache $cache */
protected $cache;

public function __construct( ManagerBackend $manager_backend, Cache $cache, Auth $auth, WooExtensionsList $woo_extensions_list ) {
$this->manager_backend = $manager_backend;
$this->auth = $auth;
$this->woo_extensions_list = $woo_extensions_list;
$this->cache = $cache;
parent::__construct();
}

Expand Down Expand Up @@ -114,7 +119,7 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
}

$this->auth->set_manager_secret( $qit_secret );
$this->manager_backend->get_cache()->set( 'manager_url', $manager_url, - 1 );
$this->cache->set( 'manager_url', $manager_url, - 1 );

$output->writeln( "Validating your Manager Secret against $manager_url..." );
try {
Expand All @@ -124,7 +129,7 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
}
} catch ( \Exception $e ) {
$this->auth->delete_manager_secret();
$this->manager_backend->get_cache()->delete( 'manager_url' );
$this->cache->delete( 'manager_url' );
$output->writeln( sprintf( '<error>We could not authenticate to %s using the provided Manager Secret.</error>', escapeshellarg( $manager_url ) ) );

return Command::FAILURE;
Expand Down
16 changes: 8 additions & 8 deletions src/src/Commands/CreateMassTestCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use QIT_CLI\App;
use QIT_CLI\Auth;
use QIT_CLI\ManagerBackend;
use QIT_CLI\Cache;
use QIT_CLI\IO\Output;
use QIT_CLI\RequestBuilder;
use Symfony\Component\Console\Application;
Expand All @@ -15,19 +15,19 @@

class CreateMassTestCommands extends DynamicCommandCreator {

/** @var ManagerBackend $manager_backend */
protected $manager_backend;
/** @var Cache $cache */
protected $cache;

/** @var Auth $auth */
protected $auth;

/** @var OutputInterface $output */
protected $output;

public function __construct( ManagerBackend $manager_backend, Auth $auth ) {
$this->manager_backend = $manager_backend;
$this->auth = $auth;
$this->output = App::make( Output::class );
public function __construct( Cache $cache, Auth $auth ) {
$this->cache = $cache;
$this->auth = $auth;
$this->output = App::make( Output::class );
}

public function register_commands( Application $application ): void {
Expand Down Expand Up @@ -58,7 +58,7 @@ public function execute( InputInterface $input, OutputInterface $output ) {
->setName( 'mass-test' );

try {
$schema = $this->manager_backend->get_cache()->get_manager_sync_data( 'schemas' )['mass'] ?? null;
$schema = $this->cache->get_manager_sync_data( 'schemas' )['mass'] ?? null;

if ( ! is_array( $schema ) ) {
throw new \Exception();
Expand Down
16 changes: 8 additions & 8 deletions src/src/Commands/CreateRunCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use QIT_CLI\App;
use QIT_CLI\Auth;
use QIT_CLI\ManagerBackend;
use QIT_CLI\Cache;
use QIT_CLI\IO\Output;
use QIT_CLI\RequestBuilder;
use QIT_CLI\Upload;
Expand All @@ -20,8 +20,8 @@
use function QIT_CLI\get_manager_url;

class CreateRunCommands extends DynamicCommandCreator {
/** @var ManagerBackend $manager_backend */
protected $manager_backend;
/** @var Cache $cache */
protected $cache;

/** @var Auth $auth */
protected $auth;
Expand All @@ -35,17 +35,17 @@ class CreateRunCommands extends DynamicCommandCreator {
/** @var WooExtensionsList $woo_extensions_list */
protected $woo_extensions_list;

public function __construct( ManagerBackend $manager_backend, Auth $auth, Upload $upload, WooExtensionsList $woo_extensions_list ) {
$this->manager_backend = $manager_backend;
public function __construct( Cache $cache, Auth $auth, Upload $upload, WooExtensionsList $woo_extensions_list ) {
$this->cache = $cache;
$this->auth = $auth;
$this->output = App::make( Output::class );
$this->upload = $upload;
$this->woo_extensions_list = $woo_extensions_list;
}

public function register_commands( Application $application ): void {
foreach ( $this->manager_backend->get_cache()->get_manager_sync_data( 'test_types' ) as $test_type ) {
$this->register_command_by_schema( $application, $test_type, $this->manager_backend->get_cache()->get_manager_sync_data( 'schemas' )[ $test_type ] );
foreach ( $this->cache->get_manager_sync_data( 'test_types' ) as $test_type ) {
$this->register_command_by_schema( $application, $test_type, $this->cache->get_manager_sync_data( 'schemas' )[ $test_type ] );
}
}

Expand Down Expand Up @@ -313,7 +313,7 @@ public function execute( InputInterface $input, OutputInterface $output ) {
// This will be hidden while custom test type is in development, but kept as an alias to "woo-e2e".
if ( $test_type === 'e2e' ) {
try {
$hide_e2e = $this->manager_backend->get_cache()->get_manager_sync_data( 'hide_e2e' );
$hide_e2e = $this->cache->get_manager_sync_data( 'hide_e2e' );
} catch ( \Exception $e ) {
// If it throws it's because it's not set, so we hide it.
$hide_e2e = true;
Expand Down
9 changes: 0 additions & 9 deletions src/src/Commands/DevModeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,13 @@
namespace QIT_CLI\Commands;

use QIT_CLI\Config;
use QIT_CLI\ManagerBackend;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DevModeCommand extends Command {
protected static $defaultName = 'dev'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

/** @var ManagerBackend $manager_backend */
protected $manager_backend;

public function __construct( ManagerBackend $manager_backend ) {
$this->manager_backend = $manager_backend;
parent::__construct();
}

protected function configure() {
$this
->setDescription( 'Enabled QIT CLI development mode.' )
Expand Down
12 changes: 6 additions & 6 deletions src/src/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace QIT_CLI\Commands;

use QIT_CLI\Auth;
use QIT_CLI\ManagerBackend;
use QIT_CLI\Cache;
use QIT_CLI\RequestBuilder;
use QIT_CLI\WooExtensionsList;
use Symfony\Component\Console\Command\Command;
Expand All @@ -16,24 +16,24 @@
class ListCommand extends Command {
protected static $defaultName = 'list-tests'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

/** @var ManagerBackend $manager_backend */
protected $manager_backend;
/** @var Cache $cache */
protected $cache;

/** @var Auth $auth */
protected $auth;

/** @var WooExtensionsList $woo_extensions_list */
protected $woo_extensions_list;

public function __construct( ManagerBackend $manager_backend, Auth $auth, WooExtensionsList $woo_extensions_list ) {
$this->manager_backend = $manager_backend;
public function __construct( Cache $cache, Auth $auth, WooExtensionsList $woo_extensions_list ) {
$this->cache = $cache;
$this->auth = $auth;
$this->woo_extensions_list = $woo_extensions_list;
parent::__construct();
}

protected function configure() {
$test_types_list = implode( ', ', $this->manager_backend->get_cache()->get_manager_sync_data( 'test_types' ) );
$test_types_list = implode( ', ', $this->cache->get_manager_sync_data( 'test_types' ) );
$this
->setDescription( 'List test runs.' )
->addOption( 'extensions', 'e', InputOption::VALUE_OPTIONAL, '(Optional) Retrieve results for these extensions (Accepts slugs or IDs, comma-separated).' )
Expand Down
11 changes: 8 additions & 3 deletions src/src/Commands/Partner/AddPartner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace QIT_CLI\Commands\Partner;

use QIT_CLI\Auth;
use QIT_CLI\Cache;
use QIT_CLI\ManagerBackend;
use QIT_CLI\WooExtensionsList;
use Symfony\Component\Console\Command\Command;
Expand All @@ -27,10 +28,14 @@ class AddPartner extends Command {
/** @var ManagerBackend $manager_backend */
protected $manager_backend;

public function __construct( ManagerBackend $manager_backend, Auth $auth, WooExtensionsList $woo_extensions_list ) {
/** @var Cache $cache */
protected $cache;

public function __construct( ManagerBackend $manager_backend, Cache $cache, Auth $auth, WooExtensionsList $woo_extensions_list ) {
$this->manager_backend = $manager_backend;
$this->auth = $auth;
$this->woo_extensions_list = $woo_extensions_list;
$this->cache = $cache;
parent::__construct();
}

Expand Down Expand Up @@ -142,10 +147,10 @@ protected function execute( InputInterface $input, OutputInterface $output ): in
$this->manager_backend->add_partner( $user_environment );

$this->auth->set_partner_auth( $user, $qit_token );
$this->manager_backend->get_cache()->set( 'manager_url', $manager_url, - 1 );
$this->cache->set( 'manager_url', $manager_url, - 1 );
$this->woo_extensions_list->fetch_woo_extensions_available();

$output->writeln( sprintf( "Cache file written to: %s. Keep this file safe, as it contains your QIT Token.\n", $this->manager_backend->get_cache()->get_cache_file_path() ) );
$output->writeln( sprintf( "Cache file written to: %s. Keep this file safe, as it contains your QIT Token.\n", $this->cache->get_cache_file_path() ) );
$output->writeln( "Treat this file as you would treat your SSH keys. For more tips on hardening security, check the README of the QIT CLI.\n" );
$output->writeln( '<fg=green>Initialization complete! You can now start using the QIT CLI!</>' );

Expand Down
9 changes: 0 additions & 9 deletions src/src/Commands/SetProxyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace QIT_CLI\Commands;

use QIT_CLI\Config;
use QIT_CLI\ManagerBackend;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -14,14 +13,6 @@
class SetProxyCommand extends Command {
protected static $defaultName = 'proxy'; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase

/** @var ManagerBackend $manager_backend */
protected $manager_backend;

public function __construct( ManagerBackend $manager_backend ) {
$this->manager_backend = $manager_backend;
parent::__construct();
}

protected function configure() {
$this
->setDescription( 'Change the default URL that we use to connect to Automattic proxy locally.' )
Expand Down
3 changes: 3 additions & 0 deletions src/src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public static function is_development_mode(): bool {

public static function set_current_manager_environment( string $manager_backend ): void {
App::make( self::class )->set( 'current_environment', $manager_backend );

// Update the existing Cache singleton with the new environment.
App::make( Cache::class )->init_cache();
}

public static function get_current_manager_backend(): string {
Expand Down
Loading

0 comments on commit b428a6b

Please sign in to comment.