Skip to main content

PHP Class Reference

Complete reference for all PHP classes in MIH. Includes method signatures, parameter descriptions, return types, and usage examples.


\local_integrationhub\mih

File: classes/mih.php Pattern: Facade / Singleton

The main entry point. All outbound integrations go through this class.

Methods

request(string $servicename, string $endpoint, array $payload, string $method): mih_response

Static convenience method for making a request.

$response = \local_integrationhub\mih::request('my-service', '/api', ['id'=>1], 'POST');

send(string $servicename): mih_request

Starts a fluent request builder.

$response = \local_integrationhub\mih::send('my-service')
->to('/api')
->with(['id'=>1])
->dispatch();

instance(): self

Returns the singleton instance (internal use).

$mih = \local_integrationhub\mih::instance();

request(string $servicename, string $endpoint, array $payload, string $method): mih_response

Makes a request to a registered external service.

ParameterTypeDefaultDescription
$servicenamestringService slug (the name field in the dashboard)
$endpointstring/Path appended to base URL. For AMQP: routing key. For SOAP: method name.
$payloadarray[]Data to send. JSON-encoded for HTTP/AMQP.
$methodstringPOSTHTTP method. Ignored for AMQP and SOAP.

request(...)

(See static method above)

Returns: mih_response

Throws: \moodle_exception with keys:

  • service_not_found — service slug not in database
  • service_disabled — service exists but enabled = 0
  • circuit_open — circuit breaker is OPEN and cooldown has not expired
try {
$response = mih::request('my-api', '/users', ['id' => 5], 'POST');
} catch (\moodle_exception $e) {
// Handle configuration/circuit errors
}

\local_integrationhub\mih_response

File: classes/mih_response.php Pattern: Immutable Value Object

Wraps the result of every MIH request.

Properties

PropertyTypeDescription
$successbooltrue if the request completed successfully
$httpstatusint|nullHTTP response code. null for AMQP.
$bodystring|nullRaw response body
$errorstring|nullError message. null on success.
$latencymsintTotal time in milliseconds (including retries)
$attemptsintTotal attempts made

Methods

is_ok(): bool

Returns true if $success === true.

if ($response->is_ok()) { ... }

json(bool $assoc = true): mixed

Decodes $body as JSON.

ParameterTypeDefaultDescription
$assocbooltruetrue = array, false = stdClass

Returns: array, stdClass, scalar, or null

$data = $response->json();        // array
$obj = $response->json(false); // stdClass

\local_integrationhub\service\registry

File: classes/service/registry.php Pattern: Static utility class

Handles all database operations for the local_integrationhub_svc table.

Methods

get_service(string $name): \stdClass|false

Retrieves a service by its slug name.

$service = registry::get_service('my-api');
if (!$service) {
// Service not found
}

get_service_by_id(int $id): \stdClass

Retrieves a service by its database ID. Throws \dml_exception if not found.

$service = registry::get_service_by_id(3);

get_all_services(): array

Returns all services as an associative array keyed by id.

$services = registry::get_all_services();
foreach ($services as $id => $service) {
echo $service->name;
}

create_service(\stdClass $data): int

Creates a new service record and initializes its circuit breaker state.

Returns: The new service's id.

$data = new stdClass();
$data->name = 'new-api';
$data->type = 'rest';
$data->base_url = 'https://api.example.com';
$data->enabled = 1;
// ... other fields

$id = registry::create_service($data);

update_service(int $id, \stdClass $data): bool

Updates an existing service record.

$data = new stdClass();
$data->timeout = 10;
registry::update_service(3, $data);

delete_service(int $id): bool

Deletes a service and all associated records (circuit breaker, logs, rules, DLQ entries).

registry::delete_service(3);

\local_integrationhub\service\circuit_breaker

File: classes/service/circuit_breaker.php

Manages the circuit breaker state for a service.

Methods

from_service(\stdClass $service): self

Factory method. Creates a circuit breaker instance from a service record.

$cb = circuit_breaker::from_service($service);

is_available(): bool

Returns true if requests should be allowed through.

  • CLOSED → true
  • OPEN + cooldown not expired → false
  • OPEN + cooldown expired → transitions to HALFOPEN, returns true
  • HALFOPEN → true
if (!$cb->is_available()) {
throw new \moodle_exception('circuit_open');
}

record_success(): void

Records a successful request. Resets failure_count to 0. If state was HALFOPEN, transitions to CLOSED.

$cb->record_success();

record_failure(): void

Records a failed request. Increments failure_count. If failure_count >= threshold, transitions to OPEN.

$cb->record_failure();

get_state(): \stdClass

Returns the raw circuit breaker DB record.

$state = $cb->get_state();
echo $state->state; // 'closed', 'open', 'halfopen'
echo $state->failure_count; // int
echo $state->last_failure; // unix timestamp

get_state_label(): string

Returns a human-readable state label: 'CLOSED', 'OPEN', or 'HALFOPEN'.

echo $cb->get_state_label(); // 'OPEN'

reset(): void

Forces the circuit to CLOSED and resets the failure counter. Used by the dashboard "Reset Circuit" button.

$cb->reset();

\local_integrationhub\service\retry_policy

File: classes/service/retry_policy.php

Executes a callable with configurable retry attempts and exponential backoff.

Methods

from_service(\stdClass $service): self

Factory method. Creates a retry policy from a service record.

$policy = retry_policy::from_service($service);

execute(callable $operation): mixed

Executes $operation with retries.

ParameterTypeDescription
$operationcallableFunction to execute. Receives the attempt number (1-based) as its argument.

Returns: The return value of $operation on success.

Throws: The last exception if all attempts fail.

$result = $policy->execute(function(int $attempt) use ($transport, $service, $endpoint, $payload, $method) {
return $transport->execute($service, $endpoint, $payload, $method);
});

get_total_attempts(): int

Returns the total number of attempts made in the last execute() call.

$policy->execute($fn);
echo $policy->get_total_attempts(); // 3 (if it took 3 attempts)

\local_integrationhub\transport\contract

File: classes/transport/contract.php Pattern: Interface

All transport drivers must implement this interface.

Methods

execute(\stdClass $service, string $endpoint, array $payload, string $method): array

Executes the transport request.

Returns: Array with keys:

  • success (bool)
  • body (string|null)
  • httpstatus (int|null)
  • error (string|null)
  • latencyms (int)
  • attempts (int)

\local_integrationhub\event\observer

File: classes/event/observer.php

Methods

handle_event(\core\event\base $event): void

Static method. Called by Moodle's event system for every event. Performs rule lookup, deduplication, and task queuing.

// Registered in db/events.php — not called directly

\local_integrationhub\task\dispatch_event_task

File: classes/task/dispatch_event_task.php Extends: \core\task\adhoc_task

Methods

execute(): void

Processes one queued event. Loads rule and service, interpolates template, calls Gateway, handles DLQ on permanent failure.


move_to_dlq(\stdClass $rule, array $payload, string $error): void

Protected. Writes a failed event to the DLQ table.


\local_integrationhub\task\consume_responses_task

File: classes/task/consume_responses_task.php Extends: \core\task\scheduled_task

Methods

get_name(): string

Returns 'Consume AMQP Responses'.


execute(): void

Connects to each AMQP service that has a response_queue configured and consumes pending messages.


\local_integrationhub\transport\amqp_helper

File: classes/transport/amqp_helper.php

Methods

create_connection(string $url, int $timeout = 5): AMQPStreamConnection

Parses an AMQP URL and creates a connection. Supports both amqp:// and amqps://.

$connection = amqp_helper::create_connection('amqp://guest:guest@localhost:5672/', 5);

ensure_queue($channel, string $queue): void

Declares a durable queue if it does not already exist.

amqp_helper::ensure_queue($channel, 'my_queue');