Skip to content

Commit

Permalink
satisfy php linter
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersoncasimir committed Jan 27, 2025
1 parent 4e87ef7 commit 8ae2722
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 96 deletions.
51 changes: 34 additions & 17 deletions modules/instrument_manager/php/instrument_data.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ use LORIS\server_processes_manager as SP;
/**
* Upload instrument data
*
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* PHP Version 8
*
* @category Main
* @package LORIS
* @author Loris Team <[email protected]>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris/
*/
class Instrument_Data extends \NDB_Page
{
Expand Down Expand Up @@ -109,27 +115,28 @@ class Instrument_Data extends \NDB_Page
// Ensure the user is allowed to upload.
if (!$request->getAttribute('user')->hasPermission(
'instrument_manager_write'
)) {
)
) {
return new \LORIS\Http\Response\JSON\Forbidden();
}

$requestBody = (array) $request->getParsedBody();
$requestBody = (array) $request->getParsedBody();

if (isset($requestBody['instrument'])) {
$instrument = $requestBody['instrument'];
if ($this->instrumentExists($instrument)) {
$dataFile = $request->getUploadedFiles()['data_file'];
$userID = $request->getAttribute('user')->getUsername();
$dataFile = $request->getUploadedFiles()['data_file'];
$userID = $request->getAttribute('user')->getUsername();

$uploadFolder = $this->loris
->getConfiguration()->getSetting('instrumentDataPath');
$filePrefix = $userID . '_' . time() . '_';
$fileLocation = $uploadFolder . "/$filePrefix"
$fileLocation = $uploadFolder . "/$filePrefix"
. $dataFile->getClientFilename();
$dataFile->moveTo($fileLocation);

$fileInfo = new SplFileInfo($fileLocation);
$this->insertCSVFileEntry($fileInfo->getFilename());
$this->_insertCSVFileEntry($fileInfo->getFilename());

if ($fileInfo->getSize() > self::MAX_FILE_BYTES) {
// Run as background task
Expand All @@ -141,9 +148,10 @@ class Instrument_Data extends \NDB_Page

$result = [
'success' => true,
'message' => "Due to the relatively large size of the uploaded file, " .
"the process has been sent to the background task queue. " .
"You will receive an email once completed."
'message' => "Due to the relatively large size of the " .
"uploaded file, the process has been sent to the " .
"background task queue. You will receive an email " .
"once completed."

];
return new \LORIS\Http\Response\JSON\Created($result);
Expand All @@ -155,8 +163,8 @@ class Instrument_Data extends \NDB_Page
$instrument,
$fileInfo,
);
$data = $dataParser->parseCSV($this->loris);
$validData = $dataParser->validateData(
$data = $dataParser->parseCSV($this->loris);
$validData = $dataParser->validateData(
$data,
[
'UserID' => $userID,
Expand Down Expand Up @@ -213,11 +221,20 @@ class Instrument_Data extends \NDB_Page
return $count > 0;
}

private function insertCSVFileEntry($filename)
/**
* Insert entry for CSV file
*
* @param string $filename The file name
*
* @return void
*/
private function _insertCSVFileEntry($filename)
{
// Create db entry for csv file
$this->loris->getDatabaseConnection()->insert('instrument_data_files', [
'FilePath' => $filename,
]);
$this->loris->getDatabaseConnection()->insert(
'instrument_data_files',
[
'FilePath' => $filename,
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -582,21 +582,23 @@ abstract class AbstractServerProcess
/**
* Gets directory where all temporary files created by this task should go
*
* @return string full path to the temporary directory
* @return string full path to the temporary directory
* @abstract
*/
abstract public function getTmpDir();

/**
* Change file permissions
*
* @return void
* @abstract
*/
abstract public function changeFilePermissions();

/**
* Perform action once process is completed
*
* @return void
* @abstract
*/
abstract public function performPostAction();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,20 @@ class MriUploadServerProcess extends AbstractServerProcess
/**
* Change file permissions
*
* @return void
*/
public function changeFilePermissions() {}
public function changeFilePermissions()
{
}

/**
* Perform action once process is completed
*
* @return void
*/
public function performPostAction() {}
public function performPostAction()
{
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess
/**
* Builds a new ParseInstrumentDataServerProcess
*
* @param string $instrument Instrument name
* @param string $instrument Instrument name
* @param string $fileLocation Location of the uploaded file
* @param ?int $id ID of the process in the database.
* @param ?int $pid PID for this process
* @param ?string $stdoutFile full path of file used to store the process's
* stdout
Expand Down Expand Up @@ -93,9 +94,11 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess
*/
public function getShellCommand()
{
$parseScript = "php " . dirname(__DIR__, 3) . "/tools/parse_instrument_data.php ";
$parseScript = "php " .
dirname(__DIR__, 3) .
"/tools/parse_instrument_data.php ";
$parseScript .= "$this->_instrument $this->_fileLocation ";
$parseScript .= "{$this->getUserId()} {$this->getUserId()}"; // TODO: Examiner?
$parseScript .= "{$this->getUserId()} {$this->getUserId()}";
return escapeshellarg($parseScript);
}

Expand Down Expand Up @@ -127,7 +130,8 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess
if (is_numeric($exitCode) && $exitCode == 0) {
return "Instrument data parse completed without errors.\n\n$exitText";
} else {
return "Instrument data parse failed with error code: $exitCode.\n\n$exitText";
return "Instrument data parse failed with error code: $exitCode." .
"\n\n$exitText";
}
}

Expand Down Expand Up @@ -173,8 +177,10 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess
/**
* Send email to user who triggered the process launch
*
* @return void
*/
public function sendNotificationEmail() {
public function sendNotificationEmail()
{
error_log("Sending email to {$this->getUserId()}");

$user = \NDB_Factory::singleton()->database()->pselectRow(
Expand All @@ -183,32 +189,43 @@ class ParseInstrumentDataServerProcess extends AbstractServerProcess
);

$msg_data = [
'name' => $user['First_name'],
'name' => $user['First_name'],
'message' => $this->getExitText(),
];
\Email::send($user['Email'], 'notifier_instrument_data_upload.tpl', $msg_data);
\Email::send(
$user['Email'],
'notifier_instrument_data_upload.tpl',
$msg_data
);
}

/**
* Change file permissions
*
* @return void
*/
public function changeFilePermissions() {
array_map(function($file){
chmod($file, 0775);
}, [
$this->getStdoutFile(),
$this->getStderrFile(),
$this->getExitCodeFile(),
]);
public function changeFilePermissions()
{
array_map(
function ($file) {
chmod($file, 0775);
},
[
$this->getStdoutFile(),
$this->getStderrFile(),
$this->getExitCodeFile(),
]
);
}


/**
* Perform action once process is completed
*
* @return void
*/
public function performPostAction() {
public function performPostAction()
{
$this->sendNotificationEmail();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ class ServerProcessesMonitor
$serverProcessFactory = new ServerProcessFactory();
$savedProcesses = [];
foreach ($rows as $row) {
$savedProcesses[$row['id']] = $serverProcessFactory->getServerProcess($row);
$savedProcesses[$row['id']]
= $serverProcessFactory->getServerProcess($row);

}

Expand All @@ -161,7 +162,6 @@ class ServerProcessesMonitor
// Find process in the database with this id (null if none)
$matchingProcess = $savedProcesses[$id] ?? null;


if (is_null($matchingProcess)) {
// No process with this id. Unknown id = unknown state
$state = 'UNKNOWN_ID';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ class ServerProcessFactory
/**
* Creates a server process based on the parameters passed as arguments.
*
* @param array $dbRow Process database row.
* @param array $dbRow Process database row.
*
* @return AbstractServerProcess the server process created
*
* @throws \InvalidArgumentException if the server process type is unknown
*/
public function getServerProcess(array $dbRow): AbstractServerProcess
Expand All @@ -50,7 +52,7 @@ class ServerProcessFactory
$dbRow['start_time'],
$dbRow['end_time'],
$dbRow['exit_text']
),
),
ParseInstrumentDataServerProcess::PROCESS_TYPE =>
new ParseInstrumentDataServerProcess(
'',
Expand All @@ -65,8 +67,10 @@ class ServerProcessFactory
$dbRow['start_time'],
$dbRow['end_time'],
$dbRow['exit_text']
),
default => throw new \InvalidArgumentException("Unknown server process type: ${dbRow['type']}")
),
default => throw new \InvalidArgumentException(
"Unknown server process type: ${dbRow['type']}"
)
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,16 @@ class ServerProcessLauncher
/**
* Launch instrument data parse process
*
* @param string $instrument Instrument name
* @param string $fileLocation File location
*
* @param string $instrument Instrument name
* @param string $fileLocation File location
*
* @return ?int ID (in the database) of the launched process or null
* if the process could not be run
*/
public function parseInstrumentData(
string $instrument,
string $fileLocation
): ?int
{
): ?int {
$parseInstrumentDataProcess = new ParseInstrumentDataServerProcess(
$instrument,
$fileLocation
Expand Down
Loading

0 comments on commit 8ae2722

Please sign in to comment.