SmartCane/laravel_app/app/Livewire/Reports/ReportManager.php
Martin Folkerts f60fb7e46d wip
2023-12-05 13:55:11 +01:00

140 lines
3.1 KiB
PHP

<?php
namespace App\Livewire\Reports;
use App\Models\Report;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Laravel\Jetstream\Jetstream;
use Livewire\Component;
use Symfony\Component\Process\Process;
class ReportManager extends Component
{
/**
* The create API token form state.
*
* @var array
*/
public $createReportForm = [
'name' => '',
'path' => '',
];
public $processOutput = '';
public $confirmingReportDeletion = false;
/**
* The ID of the API token being deleted.
*
* @var int
*/
public $reportIdBeingDeleted;
/**
* Mount the component.
*
* @return void
*/
public function mount()
{
}
/**
* Create a new API token.
*
* @return void
*/
public function createReport()
{
$this->resetErrorBag();
Validator::make([
'name' => $this->createReportForm['name'],
], [
'name' => ['required', 'string', 'max:255'],
])->validateWithBag('createReport');
$projectFolder = base_path('../');
$command = [
sprintf('%sbuild_report.sh', $projectFolder),
sprintf('--filename=%s', $this->createReportForm['name']),
];
// Convert commands array to a single string
$process = new Process($command);
$currentPath = '/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin/Users/mfolkerts/anaconda3/bin:/Library/Apple/usr/bin';
$process->setEnv(['PATH' => $currentPath.':/usr/local/Cellar/pandoc/3.1.8/bin/pandoc']);
$process->setTimeout(36000); // stel een geschikte timeout in
$process->start();
try {
$myOutput = [];
$process->wait(function ($type, $buffer) use (&$myOutput) {
$this->stream(to: 'processOutput', content: $buffer);
$myOutput[] = $buffer;
logger($buffer);
});
$this->processOutput = collect($myOutput)->join('\n');
} catch (ProcessFailedException $exception) {
echo $exception->getMessage();
}
Report::create(
$this->createReportForm
);
$this->createReportForm['name'] = '';
$this->dispatch('created');
}
/**
* Confirm that the given API token should be deleted.
*
* @param int $tokenId
* @return void
*/
public function confirmReportDeletion($reportId)
{
$this->confirmingReportDeletion = true;
$this->reportIdBeingDeleted = $reportId;
}
/**
* Delete the API token.
*
* @return void
*/
public function deleteReport()
{
Report::whereId($this->reportIdBeingDeleted)->first()->delete();
$this->confirmingReportDeletion = false;
}
/**
* Get the current user of the application.
*
* @return mixed
*/
public function getReportsProperty()
{
return Report::all();
}
public function render()
{
return view('livewire.reports.report-manager');
}
}