SmartCane/laravel_app/app/Livewire/Reports/ReportManager.php
Martin Folkerts 4c730c0167 wip
2023-10-24 17:08:04 +02:00

136 lines
2.8 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);
$process->setTimeout(3600); // stel een geschikte timeout in
$process->start();
try {
$myOutput = [];
$process->wait(function ($type, $buffer) use (&$myOutput){
$this->stream(to: 'processOutput', content: $buffer);
$myOutput[] = $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');
}
}