MigrateAPIIDAReports
extends MigrateReports
in package
MigrateAPIIDAReports class.
Table of Contents
Properties
- $db : object
- The database instance for report data.
- $instance : object
- The single instance of the class.
- $upload_dir_path : string
- The path to the upload directory.
Methods
- init() : MigrateAPIIDAReports
- Gets the single instance of the class.
- migrate_data() : array<int, mixed>
- Migrates data.
- __construct() : mixed
- Constructor.
- clean_files() : array<string|int, string>
- Cleans up the given files.
- db_entry_exists() : bool
- Checks if a db entry exists, given the first name, last name, and email.
- get_files() : array<string|int, string>
- Gets all the files in the bc-apiida/results directory.
- insert_into_db() : int|WP_Error
- Inserts the given data into the database.
- maybe_format_keys() : array<string, mixed>
- Converts the keys of the given associative array from camel case to snake case.
- migrate_reports() : array<int, string>|array<int, int>|false
- Migrates the APIIDA reports from the provided JSON files to the database.
- prepare_item_for_db() : array<string, mixed>
- Prepares a report item from a JSON file for database insertion.
- prepare_report_for_db() : array<string|int, string>
- Prepares a report item from a JSON file for database insertion.
Properties
$db
The database instance for report data.
protected
object
$db
$instance
The single instance of the class.
protected
static object
$instance
$upload_dir_path
The path to the upload directory.
protected
string
$upload_dir_path
Methods
init()
Gets the single instance of the class.
public
static init() : MigrateAPIIDAReports
Return values
MigrateAPIIDAReports —Single instance of the class.
migrate_data()
Migrates data.
public
migrate_data() : array<int, mixed>
Finds all the files in the bc-apiida directory and migrates them to the database.
Return values
array<int, mixed> —An array of migrated data.
__construct()
Constructor.
protected
__construct() : mixed
Initializes the MigrateReports class by setting up the upload directory path and database instance for report data.
Tags
clean_files()
Cleans up the given files.
protected
clean_files(array<string|int, string> $files) : array<string|int, string>
Iterates over the given array of files and removes any files that do not contain the required keys 'FirstName', 'LastName', and 'Email'. It then adds a report_url to the data in the file by prepending the site URL and '#report-' to the filename. The modified data is then encoded back to JSON and saved to the file.
Parameters
- $files : array<string|int, string>
-
The files to clean up.
Return values
array<string|int, string> —The cleaned up files.
db_entry_exists()
Checks if a db entry exists, given the first name, last name, and email.
protected
db_entry_exists(array{first_name: string, last_name: string, email: string} $data) : bool
If the data is not valid (e.g. empty or not set), this function will return true to indicate that the data should be skipped.
Parameters
- $data : array{first_name: string, last_name: string, email: string}
-
The data to check for.
Return values
bool —True if the database entry exists, false otherwise.
get_files()
Gets all the files in the bc-apiida/results directory.
protected
get_files() : array<string|int, string>
Finds all the files in the bc-apiida/results directory and returns them as an array. If the directory does not exist, or if there are no files with the name 'data-*', an empty array is returned.
Return values
array<string|int, string> —Array of file paths.
insert_into_db()
Inserts the given data into the database.
protected
insert_into_db(array<string, mixed> $data) : int|WP_Error
Attempts to insert the provided array of data into the database. If the insertion fails, a WP_Error object is returned indicating the failure. If successful, the ID of the inserted row is returned.
Parameters
- $data : array<string, mixed>
-
The data to insert.
Return values
int|WP_Error —The ID of the inserted row, or a WP_Error object on failure.
maybe_format_keys()
Converts the keys of the given associative array from camel case to snake case.
protected
maybe_format_keys(array<string, mixed> $data) : array<string, mixed>
Utilizes the camel_to_snake
function to transform each key in the array.
Parameters
- $data : array<string, mixed>
-
The associative array with camel case keys to be transformed.
Return values
array<string, mixed> —The array with keys converted to snake case.
migrate_reports()
Migrates the APIIDA reports from the provided JSON files to the database.
protected
migrate_reports(array<int, string> $files) : array<int, string>|array<int, int>|false
Iterates over each file, prepares the data for database insertion, and inserts it if it does not already exist in the database. If an error occurs during insertion, the file is skipped. Only successfully inserted report IDs are returned.
Parameters
- $files : array<int, string>
-
Array of JSON files to be migrated.
Return values
array<int, string>|array<int, int>|false —Array of IDs of migrated reports, or false if no files are provided.
prepare_item_for_db()
Prepares a report item from a JSON file for database insertion.
private
prepare_item_for_db(array<string, mixed> $item) : array<string, mixed>
Sets the created date to the current time if not provided. Converts the integrationCampaign2 key to report_url. Serializes the data if it isn't already.
Parameters
- $item : array<string, mixed>
-
The report item to prepare.
Return values
array<string, mixed> —The prepared report item.
prepare_report_for_db()
Prepares a report item from a JSON file for database insertion.
private
prepare_report_for_db(string $file, string $app) : array<string|int, string>
This method reads a JSON file, converts its contents to an array, formats the keys, and prepares the data for insertion into the database. It sets the report title using the 'job' key if available, assigns the application name to the 'app' key, and removes the 'job' key after use.
Parameters
- $file : string
-
The path to the JSON file to read.
- $app : string
-
The application name to associate with the report.
Return values
array<string|int, string> —The prepared report data ready for database insertion.