MigrationMoodle: Improve migration messages - refs BT#15992

pull/3127/head
Angel Fernando Quiroz Campos 6 years ago
parent f591f7f2fd
commit 3081d9b2fe
  1. 12
      plugin/migrationmoodle/admin.php
  2. 23
      plugin/migrationmoodle/src/Exceptions/Exception.php
  3. 26
      plugin/migrationmoodle/src/Exceptions/ExtractException.php
  4. 37
      plugin/migrationmoodle/src/Exceptions/LoadException.php
  5. 43
      plugin/migrationmoodle/src/Exceptions/TransformException.php
  6. 36
      plugin/migrationmoodle/src/Task/BaseTask.php

@ -1,6 +1,7 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\Exception as MigrationMoodleException;
use Chamilo\PluginBundle\MigrationMoodle\Task\BaseTask;
require_once __DIR__.'/../../main/inc/global.inc.php';
@ -93,15 +94,20 @@ if (!empty($action) && isAllowedAction($action, $menu)) {
echo Display::page_subheader(
$plugin->get_lang($taskName)
);
echo '<pre>';
$taskName = 'Chamilo\\PluginBundle\\MigrationMoodle\\Task\\'.$taskName;
/** @var BaseTask $task */
$task = new $taskName();
echo '<div style="overflow: auto;height: 820px;">';
$task->execute();
echo '</div>';
try {
$task->execute();
} catch (MigrationMoodleException $exception) {
$exception->displayAsString();
}
echo '</pre>';
}
echo '</div>';

@ -0,0 +1,23 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Exceptions;
/**
* Class Exception.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Exceptions
*/
abstract class Exception extends \Exception
{
public function displayAsString()
{
$pieces = [$this->getMessage()];
if ($this->getPrevious()) {
$pieces[] = "\t".$this->getPrevious()->getMessage();
}
echo implode(PHP_EOL, $pieces);
}
}

@ -0,0 +1,26 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Exceptions;
use Throwable;
/**
* Class ExtractException.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Exceptions
*/
class ExtractException extends Exception
{
/**
* ExtractException constructor.
*
* @param Throwable|null $previous
*/
public function __construct(Throwable $previous = null)
{
$message = 'Error while extracting data.';
parent::__construct($message, 0, $previous);
}
}

@ -0,0 +1,37 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Exceptions;
use Throwable;
/**
* Class LoadException.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Exceptions
*/
class LoadException extends Exception
{
/**
* @var array
*/
private $incomingData;
public function __construct($incomingData, Throwable $previous = null)
{
$message = 'Error while loading transformed data.';
$this->incomingData = $incomingData;
parent::__construct($message, 0, $previous);
}
public function displayAsString()
{
$pieces = [
parent::displayAsString(),
"\t".print_r($this->incomingData, true),
];
echo implode(PHP_EOL, $pieces);
}
}

@ -0,0 +1,43 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Exceptions;
use Throwable;
/**
* Class TransformException.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Exceptions
*/
class TransformException extends Exception
{
/**
* @var array
*/
private $extractedData;
/**
* TransformException constructor.
*
* @param array $extractedData
* @param Throwable|null $previous
*/
public function __construct(array $extractedData, Throwable $previous = null)
{
$message = 'Error while transforming extracted data.';
$this->extractedData = $extractedData;
parent::__construct($message, 0, $previous);
}
public function displayAsString()
{
$pieces = [
parent::displayAsString(),
"\t".print_r($this->extractedData, true),
];
echo implode(PHP_EOL, $pieces);
}
}

@ -3,6 +3,10 @@
namespace Chamilo\PluginBundle\MigrationMoodle\Task;
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\Exception;
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\ExtractException;
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\LoadException;
use Chamilo\PluginBundle\MigrationMoodle\Exceptions\TransformException;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\ExtractorInterface;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\LoaderInterface;
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\TransformerInterface;
@ -63,45 +67,30 @@ abstract class BaseTask
*/
abstract public function getLoadConfiguration();
/**
* @throws Exception
*/
public function execute()
{
foreach ($this->extractFiltered() as $extractedData) {
try {
$incomingData = $this->transformer->transform($extractedData);
} catch (\Exception $exception) {
$this->showMessage('Error while transforming extracted data.', $exception->getMessage(), $extractedData);
continue;
throw new TransformException($extractedData, $exception);
}
try {
$loadedId = $this->loader->load($incomingData);
} catch (\Exception $exception) {
$this->showMessage('Error while loading transformed data.', $exception->getMessage(), $incomingData);
continue;
throw new LoadException($incomingData, $exception);
}
$hash = $this->saveMapLog($extractedData['id'], $loadedId);
$this->showMessage('Data migrated.', "{$extractedData['id']} -> $loadedId", $hash);
echo "Data migrated. $hash".PHP_EOL;
}
}
/**
* @param string $first
* @param string $second
* @param string $data
*/
private function showMessage($first, $second, $data)
{
echo '<p>'
."$first "
."<em>$second</em><br>"
.'<code>'.print_r($data, true).'</code>'
.'</p>';
}
/**
* @throws \Exception
*/
@ -122,6 +111,8 @@ abstract class BaseTask
}
/**
* @throws ExtractException
*
* @return \Generator
*/
private function extractFiltered()
@ -135,8 +126,7 @@ abstract class BaseTask
yield $extractedData;
}
} catch (\Exception $exception) {
echo 'Error while extracting data. ';
echo $exception->getMessage();
throw new ExtractException($exception);
}
}

Loading…
Cancel
Save