1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace agoalofalife\Reports\Console; |
5
|
|
|
|
6
|
|
|
use agoalofalife\Reports\Report; |
7
|
|
|
use agoalofalife\Reports\Models\Report as ReportModel; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Illuminate\Support\Facades\Storage; |
10
|
|
|
use Maatwebsite\Excel\Facades\Excel; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class HandleReportCommand |
14
|
|
|
* @package agoalofalife\Reports\Console |
15
|
|
|
*/ |
16
|
|
|
class HandleReportCommand extends Command |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The name and signature of the console command. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $signature = 'reports:handle {classReport}'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The console command description. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = 'Handle Report class and save file'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Execute the console command. |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
4 |
|
public function handle() : void |
39
|
|
|
{ |
40
|
4 |
|
$classReport = $this->argument('classReport'); |
41
|
|
|
|
42
|
4 |
|
if (class_exists($classReport) === false) { |
43
|
1 |
|
throw new \Exception($classReport . ' class not exist'); |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
if (is_subclass_of($classReport, Report::class) === false) { |
|
|
|
|
47
|
1 |
|
throw new \Exception($classReport . ' class is not subclass ' . Report::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
$report = app()->make($classReport); |
51
|
2 |
|
$excelWriter = Excel::create($report->getFileName(), $resultHandler = [$report, 'handler']); |
|
|
|
|
52
|
|
|
// if method handler return true, meaning 'success' result |
53
|
2 |
|
if ($resultHandler) { |
|
|
|
|
54
|
2 |
|
$excelWriter->store($report->extension, Storage::disk($report->disk) |
55
|
2 |
|
->getDriver()->getAdapter()->getPathPrefix()); |
56
|
|
|
|
57
|
2 |
|
$report->getReportModel()->update([ |
58
|
2 |
|
'status' => ReportModel::STATUS_COMPLETED, |
59
|
|
|
'is_completed' => true, |
60
|
|
|
]); |
61
|
|
|
// send notification |
62
|
2 |
|
$report->send(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |