HandleReportCommand::handle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 15
cts 15
cp 1
rs 9.488
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
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) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \agoalofalife\Reports\Report::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
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']);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Maatwebsite\Excel\Facades\Excel. Did you maybe mean createFreshMockInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
52
        // if method handler return true, meaning 'success' result
53 2
        if ($resultHandler) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $resultHandler of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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
}