| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/project-slim/app/Services/ |
Upload File : |
<?php
namespace App\Services;
use App\Models\Report;
use App\Repositories\Interfaces\ReportRepositoryInterface;
use App\Validators\ReportValidator;
use App\Exceptions\ValidationException;
use Illuminate\Pagination\LengthAwarePaginator;
class ReportService
{
private ReportRepositoryInterface $reportRepository;
private ReportValidator $validator;
public function __construct(ReportRepositoryInterface $reportRepository, ReportValidator $validator)
{
$this->reportRepository = $reportRepository;
$this->validator = $validator;
}
/**
* Get report by ID
*/
public function getReportById(int $id): ?Report
{
return $this->reportRepository->findById($id);
}
/**
* Get all reports with pagination and filters
*/
public function getReports(int $page = 1, int $limit = 20, array $filters = []): LengthAwarePaginator
{
return $this->reportRepository->getAll($page, $limit, $filters);
}
/**
* Create a new report
*/
public function createReport(array $data): Report
{
// Validate data
$validatedData = $this->validator->validateCreateData($data);
// Add default values
$validatedData['status'] = $validatedData['status'] ?? 'draft';
$validatedData['priority'] = $validatedData['priority'] ?? 'medium';
return $this->reportRepository->create($validatedData);
}
/**
* Update a report
*/
public function updateReport(int $id, array $data): bool
{
$report = $this->reportRepository->findById($id);
if (!$report) {
throw new \Exception("Report not found", 404);
}
// Validate data
$validatedData = $this->validator->validateUpdateData($data);
return $this->reportRepository->update($id, $validatedData);
}
/**
* Delete a report
*/
public function deleteReport(int $id): bool
{
$report = $this->reportRepository->findById($id);
if (!$report) {
throw new \Exception("Report not found", 404);
}
return $this->reportRepository->delete($id);
}
/**
* Get reports by company
*/
public function getReportsByCompany(int $companyId): array
{
$reports = $this->reportRepository->getByCompany($companyId);
return $reports->toArray();
}
/**
* Get reports by user
*/
public function getReportsByUser(int $userId): array
{
$reports = $this->reportRepository->getByUser($userId);
return $reports->toArray();
}
/**
* Search reports
*/
public function searchReports(string $query, array $filters = []): array
{
$reports = $this->reportRepository->search($query, $filters);
return $reports->toArray();
}
/**
* Generate report
*/
public function generateReport(int $id): array
{
$report = $this->reportRepository->findById($id);
if (!$report) {
throw new \Exception("Report not found", 404);
}
// Update status to generating
$this->reportRepository->update($id, ['status' => 'generating']);
try {
// Here you would implement the actual report generation logic
// This is a placeholder for the report generation process
$generatedData = $this->performReportGeneration($report);
// Update status to completed
$this->reportRepository->update($id, [
'status' => 'completed',
'file_path' => $generatedData['file_path'] ?? null,
'file_size' => $generatedData['file_size'] ?? null
]);
return $generatedData;
} catch (\Exception $e) {
// Update status to failed
$this->reportRepository->update($id, ['status' => 'failed']);
throw $e;
}
}
/**
* Get report statistics
*/
public function getStatistics(): array
{
return $this->reportRepository->getStatistics();
}
/**
* Schedule report generation
*/
public function scheduleReport(array $data): Report
{
$validatedData = $this->validator->validateCreateData($data);
$validatedData['status'] = 'scheduled';
return $this->reportRepository->create($validatedData);
}
/**
* Export report
*/
public function exportReport(int $id, string $format = 'pdf'): array
{
$report = $this->reportRepository->findById($id);
if (!$report) {
throw new \Exception("Report not found", 404);
}
if ($report->status !== 'completed') {
throw new \Exception("Report is not ready for export", 400);
}
// Here you would implement the export logic based on format
return [
'file_path' => $report->file_path,
'format' => $format,
'download_url' => "/reports/{$id}/download"
];
}
/**
* Perform the actual report generation
* This is a placeholder - implement your specific report generation logic
*/
private function performReportGeneration(Report $report): array
{
// Placeholder implementation
// You would implement specific report generation logic here
$fileName = "report_{$report->report_id}_{$report->type}_" . date('Y-m-d_H-i-s') . '.pdf';
$filePath = "storage/reports/{$fileName}";
// Simulate report generation
// In real implementation, you would:
// 1. Query the data based on report type
// 2. Generate charts/graphs if needed
// 3. Create PDF/Excel file
// 4. Save to storage
return [
'file_path' => $filePath,
'file_size' => 1024 * 50, // 50KB placeholder
'download_url' => "/reports/{$report->report_id}/download"
];
}
/**
* Get reports by status
*/
public function getReportsByStatus(string $status): array
{
$reports = $this->reportRepository->getByStatus($status);
return $reports->toArray();
}
/**
* Bulk update report status
*/
public function bulkUpdateStatus(array $reportIds, string $status): int
{
$updated = 0;
foreach ($reportIds as $id) {
if ($this->reportRepository->update($id, ['status' => $status])) {
$updated++;
}
}
return $updated;
}
/**
* Get user's report analytics
*/
public function getUserReportAnalytics(int $userId): array
{
$userReports = $this->reportRepository->getByUser($userId);
return [
'total_reports' => $userReports->count(),
'by_status' => $userReports->groupBy('status')->map->count(),
'by_type' => $userReports->groupBy('type')->map->count(),
'recent_reports' => $userReports->where('created_at', '>=', now()->subDays(30))->count()
];
}
}