UsersExport::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace App\Exports;
4
5
use App\Models\Security_user;
6
use Maatwebsite\Excel\Concerns\FromCollection;
7
use Maatwebsite\Excel\Concerns\WithHeadings;
8
use Maatwebsite\Excel\Concerns\Exportable;
9
use App\Models\Import_mapping;
10
use Maatwebsite\Excel\Concerns\WithEvents;
11
use Maatwebsite\Excel\Concerns\WithStartRow;
12
use Maatwebsite\Excel\Events\BeforeWriting;
13
use Maatwebsite\Excel\Events\BeforeExport;
14
use Illuminate\Support\Facades\Auth;
15
use Maatwebsite\Excel\Excel;
16
use Maatwebsite\Excel\Files\LocalTemporaryFile;
17
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
18
use Maatwebsite\Excel\Concerns\RegistersEventListeners;
19
use Maatwebsite\Excel\Events\BeforeSheet;
20
21
class UsersExport implements FromCollection, WithEvents, WithStartRow, WithMultipleSheets {
22
23
    /**
24
     * @return \Illuminate\Support\Collection
25
     */
26
    use Exportable,
0 ignored issues
show
introduced by
The trait Maatwebsite\Excel\Concerns\Exportable requires some properties which are not provided by App\Exports\UsersExport: $disk, $diskOptions, $fileName, $filePath, $writerType, $headers
Loading history...
27
        RegistersEventListeners;
28
29
    public $_file_name = 'users.xls';
30
31
    public function __construct($class) {
32
33
        $this->class = $class;
0 ignored issues
show
Bug Best Practice introduced by
The property class does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
        $this->_file_name = time() . '_' . Auth::user()->id . '_' . 'student_upload.xls';
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
35
    }
36
37
    public function registerEvents(): array {
38
        return [
39
        BeforeSheet::class => function(BeforeSheet $event){
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

39
        BeforeSheet::class => function(/** @scrutinizer ignore-unused */ BeforeSheet $event){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
//          dd($event->crea)  
41
        },
42
            BeforeWriting::class => function(BeforeWriting $event) {
43
                $file_path = 'app/censusNo_className_sis_students_bulk_upload_v1.xlsx';
44
                $event->writer->reopen(new LocalTemporaryFile(storage_path($file_path)), Excel::XLSX);
45
                $event->writer->getDelegate()->setActiveSheetIndex(1);
46
                return $event->getWriter()->getDelegate()->getActiveSheet();
47
            }
48
        ];
49
    }
50
51
    public function sheets(): array {
52
        return [
53
            1 => $this
54
        ];
55
    }
56
57
    public function startRow(): int {
58
        return 3;
59
    }
60
61
    public function collection() {
62
63
        $class = $this->class;
64
        return Security_user::select('openemis_no', 'first_name', 'gender_id', 'date_of_birth', 'address', 'birthplace_area_id')
65
                        ->with(['class', 'special_needs'])
66
                        ->whereHas('class', function ($query) use ($class) {
67
                            $query->where('institution_class_id', '=', $class);
68
                        })
69
                        ->get();
70
    }
71
72
}
73