Skip to content

Commit

Permalink
Add a trait to manage results of upgraders
Browse files Browse the repository at this point in the history
Reset results after application of a patch to avoid the same messages multiples times
  • Loading branch information
nenes25 committed Nov 11, 2023
1 parent a3a725a commit 98b85f9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/Patch/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ protected function processUpgrade(array $data): void
$upgrader->upgrade($data);
$this->errors = array_merge($this->errors, $upgrader->getErrors());
$this->success = array_merge($this->success, $upgrader->getSuccess());
$upgrader->resetResults();
} catch (\Exception $e) {
$this->errors[] = 'Unable to upgrade data for upgrader ' . get_class($upgrader) .
' error : ' . $e->getMessage();
Expand Down
21 changes: 2 additions & 19 deletions src/Upgrader/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@

class Configuration implements UpgraderInterface
{
use UpgraderResultTrait;

/** @var string Upgrader type */
public const TYPE = 'configuration';

protected array $errors = [];
protected array $success = [];

/**
* @param array $data
*/
Expand Down Expand Up @@ -68,20 +67,4 @@ public function upgrade(array $data): void
}
}
}

/**
* {@inheritDoc}
*/
public function getSuccess(): array
{
return $this->success;
}

/**
* {@inheritDoc}
*/
public function getErrors(): array
{
return $this->errors;
}
}
20 changes: 2 additions & 18 deletions src/Upgrader/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

class Module implements UpgraderInterface
{
use UpgraderResultTrait;

/** @var string Type d'upgrade */
public const TYPE = 'modules';

Expand All @@ -33,8 +35,6 @@ class Module implements UpgraderInterface

/** @var \PrestaShop\PrestaShop\Core\Addon\Module\ModuleManager */
protected $moduleManager;
protected array $errors = [];
protected array $success = [];

/**
* {@inheritDoc}
Expand Down Expand Up @@ -191,20 +191,4 @@ public function upgradeUpdate(array $data): void
}
}
}

/**
* {@inheritDoc}
*/
public function getSuccess(): array
{
return $this->success;
}

/**
* {@inheritDoc}
*/
public function getErrors(): array
{
return $this->errors;
}
}
7 changes: 7 additions & 0 deletions src/Upgrader/UpgraderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ public function getSuccess(): array;
* @return array List of the errors of the process
*/
public function getErrors(): array;

/**
* Reset the results of the upgrade
*
* @return void
*/
public function resetResults(): void;
}
41 changes: 41 additions & 0 deletions src/Upgrader/UpgraderResultTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Hhennes\ModulesManager\Upgrader;

/**
* Trait which allows to manage results upgrade process
*/
trait UpgraderResultTrait
{
protected array $errors = [];
protected array $warning = [];
protected array $success = [];

/**
* {@inheritDoc}
*/
public function getSuccess(): array
{
return $this->success;
}

/**
* {@inheritDoc}
*/
public function getErrors(): array
{
return $this->errors;
}

/**
* Reset the results of the upgrade
*
* @return void
*/
public function resetResults(): void
{
$this->success = [];
$this->errors = [];
$this->warning = [];
}
}

0 comments on commit 98b85f9

Please sign in to comment.