Added external function add service
Signed-off-by: Valentin Popov <info@valentineus.link>
This commit is contained in:
		@@ -20,6 +20,10 @@ defined('MOODLE_INTERNAL') || die();
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
use stdClass;
 | 
					use stdClass;
 | 
				
			||||||
use function defined;
 | 
					use function defined;
 | 
				
			||||||
 | 
					use function is_array;
 | 
				
			||||||
 | 
					use function is_bool;
 | 
				
			||||||
 | 
					use function is_int;
 | 
				
			||||||
 | 
					use function is_string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * It's a class description record.
 | 
					 * It's a class description record.
 | 
				
			||||||
@@ -77,4 +81,45 @@ final class record extends stdClass {
 | 
				
			|||||||
     * @var string
 | 
					     * @var string
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public $token;
 | 
					    public $token;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Classes constructor.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param array|null $conditions
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public function __construct(array $conditions = null) {
 | 
				
			||||||
 | 
					        if (isset($conditions['events']) && is_array($conditions['events'])) {
 | 
				
			||||||
 | 
					            $this->events = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            foreach ($conditions['events'] as $event) {
 | 
				
			||||||
 | 
					                if (is_string($event)) {
 | 
				
			||||||
 | 
					                    $this->events[] = $event;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (isset($conditions['header']) && is_string($conditions['header'])) {
 | 
				
			||||||
 | 
					            $this->header = $conditions['header'];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (isset($conditions['id']) && is_int($conditions['id'])) {
 | 
				
			||||||
 | 
					            $this->id = $conditions['id'];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (isset($conditions['name']) && is_string($conditions['name'])) {
 | 
				
			||||||
 | 
					            $this->name = $conditions['name'];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (isset($conditions['point']) && is_string($conditions['point'])) {
 | 
				
			||||||
 | 
					            $this->point = $conditions['point'];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (isset($conditions['status']) && is_bool($conditions['status'])) {
 | 
				
			||||||
 | 
					            $this->status = $conditions['status'];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (isset($conditions['token']) && is_string($conditions['token'])) {
 | 
				
			||||||
 | 
					            $this->token = $conditions['token'];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -31,6 +31,63 @@ use local_webhooks\local\record;
 | 
				
			|||||||
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
					 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
final class local_webhooks_external extends external_api {
 | 
					final class local_webhooks_external extends external_api {
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Add a new service.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param array $conditions
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return int
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @throws \coding_exception
 | 
				
			||||||
 | 
					     * @throws \dml_exception
 | 
				
			||||||
 | 
					     * @throws \invalid_parameter_exception
 | 
				
			||||||
 | 
					     * @throws \restricted_context_exception
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static function add_service(array $conditions): int {
 | 
				
			||||||
 | 
					        $parameters = self::validate_parameters(self::add_service_parameters(), [
 | 
				
			||||||
 | 
					            'events' => $conditions['events'],
 | 
				
			||||||
 | 
					            'header' => $conditions['header'],
 | 
				
			||||||
 | 
					            'name'   => $conditions['name'],
 | 
				
			||||||
 | 
					            'point'  => $conditions['point'],
 | 
				
			||||||
 | 
					            'status' => $conditions['status'],
 | 
				
			||||||
 | 
					            'token'  => $conditions['token'],
 | 
				
			||||||
 | 
					        ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $context = context_system::instance();
 | 
				
			||||||
 | 
					        self::validate_context($context);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $record = new record($parameters);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return api::add_service($record);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Returns description of the method parameters.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return \external_function_parameters
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static function add_service_parameters(): external_function_parameters {
 | 
				
			||||||
 | 
					        return new external_function_parameters([
 | 
				
			||||||
 | 
					            'events' => new external_multiple_structure(
 | 
				
			||||||
 | 
					                new external_value(PARAM_RAW, 'The event\'s name.'), 'The service\'s list events.'
 | 
				
			||||||
 | 
					            ),
 | 
				
			||||||
 | 
					            'header' => new external_value(PARAM_RAW, 'The request\'s header or type'),
 | 
				
			||||||
 | 
					            'name'   => new external_value(PARAM_RAW, 'The service\'s name.'),
 | 
				
			||||||
 | 
					            'point'  => new external_value(PARAM_URL, 'The service\'s endpoint.'),
 | 
				
			||||||
 | 
					            'status' => new external_value(PARAM_BOOL, 'The service\'s status.'),
 | 
				
			||||||
 | 
					            'token'  => new external_value(PARAM_RAW, 'The service\'s secret key.'),
 | 
				
			||||||
 | 
					        ], '');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Returns description of the method result value.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return \external_value
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static function add_service_returns(): external_value {
 | 
				
			||||||
 | 
					        return new external_value(PARAM_INT, 'The service\'s ID.');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Delete the existing service.
 | 
					     * Delete the existing service.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -64,6 +64,42 @@ final class local_webhooks_external_testcase extends externallib_advanced_testca
 | 
				
			|||||||
        return $record;
 | 
					        return $record;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Testing external add service.
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @throws \ReflectionException
 | 
				
			||||||
 | 
					     * @throws \coding_exception
 | 
				
			||||||
 | 
					     * @throws \dml_exception
 | 
				
			||||||
 | 
					     * @throws \invalid_parameter_exception
 | 
				
			||||||
 | 
					     * @throws \invalid_response_exception
 | 
				
			||||||
 | 
					     * @throws \restricted_context_exception
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public function test_adding() {
 | 
				
			||||||
 | 
					        $this->resetAfterTest();
 | 
				
			||||||
 | 
					        self::setAdminUser();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $record = self::get_random_record();
 | 
				
			||||||
 | 
					        $return = local_webhooks_external::add_service((array) $record);
 | 
				
			||||||
 | 
					        $return = external_api::clean_returnvalue(local_webhooks_external::add_service_returns(), $return);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self::assertInternalType('integer', $return);
 | 
				
			||||||
 | 
					        self::assertEquals(1, api::get_total_count());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $service = api::get_service($return);
 | 
				
			||||||
 | 
					        self::assertEquals($record->header, $service->header);
 | 
				
			||||||
 | 
					        self::assertEquals($record->name, $service->name);
 | 
				
			||||||
 | 
					        self::assertEquals($record->point, $service->point);
 | 
				
			||||||
 | 
					        self::assertEquals($record->status, $service->status);
 | 
				
			||||||
 | 
					        self::assertEquals($record->token, $service->token);
 | 
				
			||||||
 | 
					        self::assertEquals($return, $service->id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self::assertInternalType('array', $service->events);
 | 
				
			||||||
 | 
					        self::assertCount(count($record->events), $service->events);
 | 
				
			||||||
 | 
					        foreach ($service->events as $event) {
 | 
				
			||||||
 | 
					            self::assertContains($event, $record->events);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Testing the external delete service.
 | 
					     * Testing the external delete service.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user