Skip to content

Commit

Permalink
fix: change abort_if to abort, add comments and simplify
Browse files Browse the repository at this point in the history
- abort step, condition not required (defaults to true)
- abort step, simplify conditions when to abort
- add more logging to the abort step
  • Loading branch information
keevan committed Sep 13, 2022
1 parent 5b772ad commit b04564c
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
79 changes: 79 additions & 0 deletions classes/local/step/abort_trait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Abort Step ..Trait
*
* This trait allows both flow/connector implementations to share core
* functionality. This should be moved to the "main" step type in an ideal
* world, and live there directly instead, but will need to be done as such
* until support for dual steps are fully supported.
*
* @package tool_dataflows
* @author Kevin Pham <[email protected]>
* @copyright Catalyst IT, 2022
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_dataflows\local\step;

trait abort_trait {

/**
* Executes the step, aborting the whole dataflow.
*
* @param mixed|null $input
* @return mixed
*/
public function execute($input = null) {
$config = $this->get_config();

// If a condition was set, it should not abort if the result is false. be evaluated and abort if 'true'.
if ($config->condition === false) {
return $input;
}

// If the condition was set, print out the results of the expression so it's obvious what it had evaluated to.
if ($config->condition !== '') {
$rawconfig = $this->get_raw_config();
$strresult = var_export($config->condition, true);
$this->log("Aborting dataflow due to the expression returning '{$strresult}': {$rawconfig->condition}");
}

// By default, it should abort the step.
throw new \Exception('Aborting');
}

/**
* Return the definition of the fields available in this form.
*
* @return array
*/
public static function form_define_fields(): array {
return [
'condition' => ['type' => PARAM_TEXT, 'required' => false],
];
}

/**
* Custom form inputs
*
* @param \MoodleQuickForm $mform
*/
public function form_add_custom_inputs(\MoodleQuickForm &$mform) {
$mform->addElement('text', 'config_condition', get_string('connector_abort:condition', 'tool_dataflows'));
}
}
11 changes: 11 additions & 0 deletions classes/local/step/base_step.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,17 @@ protected function get_config(): \stdClass {
return $this->stepdef->config;
}

/**
* Get the step's (definition) raw config
*
* Helper method to reduce the complexity when authoring step types.
*
* @return \stdClass configuration object
*/
protected function get_raw_config(): \stdClass {
return $this->stepdef->get_raw_config();
}

/**
* Returns whether the engine's run is dry
*
Expand Down
35 changes: 35 additions & 0 deletions classes/local/step/connector_abort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace tool_dataflows\local\step;

/**
* 'Abort if' step
*
* @package tool_dataflows
* @author Kevin Pham <[email protected]>
* @copyright Catalyst IT, 2022
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class connector_abort extends connector_step {
use abort_trait;

/** @var int[] number of output flows (min, max). */
protected $outputflows = [0, 1];

/** @var int[] number of output connectors (min, max) */
protected $outputconnectors = [0, 1];
}
4 changes: 4 additions & 0 deletions lang/en/tool_dataflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
$string['strftimedatetimeaccurate'] = '%d %B %Y, %I:%M:%S %p';

// Step names.
$string['step_name_connector_abort'] = 'Abort connector';
$string['step_name_connector_curl'] = 'Curl connector';
$string['step_name_connector_debugging'] = 'Debugging connector';
$string['step_name_connector_email'] = 'Email notification';
Expand Down Expand Up @@ -383,6 +384,9 @@
$string['connector_wait:timesec'] = 'Time in seconds';
$string['connector_wait:not_integer'] = 'Wait time value must evaluate to a positive integer (had "{$a}").';

// Abort If.
$string['connector_abort:condition'] = 'Condition';

// Flow Web service.
$string['flow_web_service:webservice'] = 'Web service';
$string['flow_web_service:webservice_help'] = 'Web service name to call ie: <code>core_user_create_users</code>';
Expand Down
1 change: 1 addition & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function tool_dataflows_after_config() {
*/
function tool_dataflows_step_types() {
return [
new step\connector_abort,
new step\connector_curl,
new step\connector_debugging,
new step\connector_debug_file_display,
Expand Down

0 comments on commit b04564c

Please sign in to comment.