Skip to content

Commit

Permalink
Apply required changes for vendor proxies (#69)
Browse files Browse the repository at this point in the history
* use logging proxy classes

* use new method to ask for confirmation

* use proxy methods for console helpers

* adjust console commands

* use proxy methods for adding command arguments
  • Loading branch information
sgiehl authored May 2, 2023
1 parent 425f6ae commit ea8b749
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 52 deletions.
7 changes: 3 additions & 4 deletions Commands/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use Piwik\Plugin\ConsoleCommand;
use Piwik\Plugins\CustomVariables\CustomVariables;
use Piwik\Plugins\CustomVariables\Model;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
*/
Expand All @@ -27,12 +25,13 @@ protected function configure()
$this->setDescription('Get info about configured custom variables');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function doExecute(): int
{
$output = $this->getOutput();
$maxVars = CustomVariables::getNumUsableCustomVariables();

if ($this->hasEverywhereSameAmountOfVariables()) {
$this->writeSuccessMessage($output, [
$this->writeSuccessMessage([
'Your Matomo is configured for ' . $maxVars . ' custom variables.'
]);
return self::SUCCESS;
Expand Down
76 changes: 30 additions & 46 deletions Commands/SetNumberOfCustomVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,11 @@
use Piwik\Plugin\ConsoleCommand;
use Piwik\Plugins\CustomVariables\Model;
use Piwik\Tracker\Cache;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

/**
*/
class SetNumberOfCustomVariables extends ConsoleCommand
{
/**
* @var \Symfony\Component\Console\Helper\ProgressBar
*/
private $progress;

protected function configure()
{
$this->setName('customvariables:set-max-custom-variables');
Expand All @@ -36,16 +26,18 @@ protected function configure()
./console customvariables:set-max-custom-variables 10
=> 10 custom variables will be available in total
");
$this->addArgument('maxCustomVars', InputArgument::REQUIRED, 'Set the number of max available custom variables');
$this->addRequiredArgument('maxCustomVars', 'Set the number of max available custom variables');
}

protected function execute(InputInterface $input, OutputInterface $output)
protected function doExecute(): int
{
$numVarsToSet = $this->getNumVariablesToSet($input);
$input = $this->getInput();
$output = $this->getOutput();
$numVarsToSet = $this->getNumVariablesToSet();
$numChangesToPerform = $this->getNumberOfChangesToPerform($numVarsToSet);

if (0 === $numChangesToPerform) {
$this->writeSuccessMessage($output, [
$this->writeSuccessMessage([
'Your Matomo is already configured for ' . $numVarsToSet . ' custom variables.'
]);
return 0;
Expand All @@ -55,55 +47,50 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(sprintf('Configuring Matomo for %d custom variables', $numVarsToSet));

foreach (Model::getScopes() as $scope) {
$this->printChanges($scope, $numVarsToSet, $output);
$this->printChanges($scope, $numVarsToSet);
}

if ($input->isInteractive() && !$this->confirmChange($input, $output)) {
if ($input->isInteractive() && !$this->confirmChange()) {
return 0;
}

$output->writeln('');
$output->writeln('Starting to apply changes');
$output->writeln('');

$this->progress = $this->initProgress($numChangesToPerform, $output);
$this->initProgressBar($numChangesToPerform);

foreach (Model::getScopes() as $scope) {
$this->performChange($scope, $numVarsToSet, $output);
$this->performChange($scope, $numVarsToSet);
}

Cache::clearCacheGeneral();
$this->progress->finish();
$this->finishProgressBar();

$this->writeSuccessMessage($output, [
$this->writeSuccessMessage([
'Your Matomo is now configured for ' . $numVarsToSet . ' custom variables.'
]);

return 0;
}

private function initProgress($numChangesToPerform, OutputInterface $output)
{
return new ProgressBar($output, $numChangesToPerform);
}

private function performChange($scope, $numVarsToSet, OutputInterface $output)
private function performChange($scope, $numVarsToSet)
{
$model = new Model($scope);
$numCurrentVars = $model->getCurrentNumCustomVars();
$numDifference = $this->getAbsoluteDifference($numCurrentVars, $numVarsToSet);

if ($numVarsToSet > $numCurrentVars) {
$this->addCustomVariables($model, $numDifference, $output);
$this->addCustomVariables($model, $numDifference);
return;
}

$this->removeCustomVariables($model, $numDifference, $output);
$this->removeCustomVariables($model, $numDifference);
}

private function getNumVariablesToSet(InputInterface $input)
private function getNumVariablesToSet(): int
{
$maxCustomVars = $input->getArgument('maxCustomVars');
$maxCustomVars = $this->getInput()->getArgument('maxCustomVars');

if (!is_numeric($maxCustomVars)) {
throw new \InvalidArgumentException('The number of available custom variables has to be a number');
Expand All @@ -118,21 +105,18 @@ private function getNumVariablesToSet(InputInterface $input)
return $maxCustomVars;
}

private function confirmChange(InputInterface $input, OutputInterface $output)
private function confirmChange()
{
$output->writeln('');

$helper = $this->getHelper('question');
$question = new ConfirmationQuestion(
$this->getOutput()->writeln('');
return $this->askForConfirmation(
'<question>Are you sure you want to perform these actions? (y/N)</question>',
false
);

return $helper->ask($input, $output, $question);
}

private function printChanges($scope, $numVarsToSet, OutputInterface $output)
private function printChanges($scope, $numVarsToSet)
{
$output = $this->getOutput();
$model = new Model($scope);
$scopeName = $model->getScopeName();
$highestIndex = $model->getHighestCustomVarIndex();
Expand Down Expand Up @@ -165,30 +149,30 @@ private function printChanges($scope, $numVarsToSet, OutputInterface $output)
}
}

private function getAbsoluteDifference($currentNumber, $numberToSet)
private function getAbsoluteDifference(int $currentNumber, int $numberToSet): int
{
return abs($numberToSet - $currentNumber);
}

private function removeCustomVariables(Model $model, $numberOfVarsToRemove, OutputInterface $output)
private function removeCustomVariables(Model $model, $numberOfVarsToRemove)
{
for ($index = 0; $index < $numberOfVarsToRemove; $index++) {
$indexRemoved = $model->removeCustomVariable();
$this->progress->advance();
$output->writeln(' <info>Removed a variable in scope "' . $model->getScopeName() . '" having the index ' . $indexRemoved . '</info>');
$this->advanceProgressBar();
$this->getOutput()->writeln(' <info>Removed a variable in scope "' . $model->getScopeName() . '" having the index ' . $indexRemoved . '</info>');
}
}

private function addCustomVariables(Model $model, $numberOfVarsToAdd, OutputInterface $output)
private function addCustomVariables(Model $model, $numberOfVarsToAdd)
{
for ($index = 0; $index < $numberOfVarsToAdd; $index++) {
$indexAdded = $model->addCustomVariable();
$this->progress->advance();
$output->writeln(' <info>Added a variable in scope "' . $model->getScopeName() . '" having the index ' . $indexAdded . '</info>');
$this->advanceProgressBar();
$this->getOutput()->writeln(' <info>Added a variable in scope "' . $model->getScopeName() . '" having the index ' . $indexAdded . '</info>');
}
}

private function getNumberOfChangesToPerform($numVarsToSet)
private function getNumberOfChangesToPerform(int $numVarsToSet): int
{
$numChangesToPerform = 0;

Expand Down
4 changes: 2 additions & 2 deletions Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Piwik\DataAccess\TableMetadata;
use Piwik\Db;
use Piwik\Piwik;
use Psr\Log\LoggerInterface;
use Piwik\Log\LoggerInterface;

class Model
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public function getScopeDescription()
* @see getHighestCustomVarIndex()
* @return int
*/
public function getCurrentNumCustomVars()
public function getCurrentNumCustomVars(): int
{
$indexes = $this->getCustomVarIndexes();

Expand Down

0 comments on commit ea8b749

Please sign in to comment.