ImportExport::import()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 1
dl 0
loc 29
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
  
6
use Illuminate\Http\Request;
7
use App\Exports\UsersExport;
8
use App\Imports\UsersImport;
9
use Illuminate\Support\Facades\Auth;
10
use Illuminate\Support\Facades\Storage;
11
use Maatwebsite\Excel\Facades\Excel;
12
use Maatwebsite\Excel\Concerns\Importable;
13
14
15
16
class ImportExport extends Controller
17
{
18
19
    use Importable;
0 ignored issues
show
introduced by
The trait Maatwebsite\Excel\Concerns\Importable requires some properties which are not provided by App\Http\Controllers\ImportExport: $disk, $readerType, $filePath
Loading history...
20
21
    /**
22
     * Create a new controller instance.
23
     *
24
     * @return void
25
     */
26
    public function __construct()
27
    {
28
        $this->middleware('auth');
29
30
    }
31
32
     /**
33
    * @return \Illuminate\Support\Collection
34
    */
35
    public function importExportView()
36
    {
37
        if(Auth::user()->super_admin ){
0 ignored issues
show
Bug introduced by
Accessing super_admin on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
38
            return view('uploadcsv');
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('uploadcsv') returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
39
        }else{
40
            $classes = (!Auth::user()->permissions->isEmpty())  ?  Auth::user()->permissions[0]->staff_class : Auth::user()->principal[0]->security_group_institution->institution_classes;
0 ignored issues
show
Bug introduced by
Accessing principal on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing permissions on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
41
            return view('importExport')->with('classes',$classes);
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('importExpor...th('classes', $classes) returns the type Illuminate\View\View which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
42
        }
43
    }
44
45
46
   
47
    /**
48
    * @return \Illuminate\Support\Collection
49
    */
50
    public function export(Request $request) 
51
    {
52
         $request->validate([
53
                'class' => 'required'
54
            ]);
55
        return Excel::download(new UsersExport($request->input('class')), 'users.xlsx');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Maatwebsite\Excel...class')), 'users.xlsx') returns the type Symfony\Component\HttpFo...tion\BinaryFileResponse which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
56
    }
57
58
59
60
    /**
61
    * @return \Illuminate\Support\Collection
62
    */
63
    public function import(Request $request)
64
    {
65
66
            $request->validate([
67
                'import_file' => 'required',
68
                'class' => 'required'
69
            ]);
70
71
        ini_set('max_execution_time', 600);
72
        // (new UsersImport)->import(request()->file('file'), null, \Maatwebsite\Excel\Excel::XLSX);
73
74
75
            $import = new UsersImport();
0 ignored issues
show
Bug introduced by
The call to App\Imports\UsersImport::__construct() has too few arguments starting with file. ( Ignorable by Annotation )

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

75
            $import = /** @scrutinizer ignore-call */ new UsersImport();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
76
            try{
77
                $files = Storage::disk('sis-bulk-data-files')->allFiles();
0 ignored issues
show
Unused Code introduced by
The assignment to $files is dead and can be removed.
Loading history...
78
                Excel::import($import,request()->file('import_file'));
0 ignored issues
show
Bug introduced by
It seems like request()->file('import_file') can also be of type Illuminate\Http\UploadedFile[] and array; however, parameter $filePath of Maatwebsite\Excel\Facades\Excel::import() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

78
                Excel::import($import,/** @scrutinizer ignore-type */ request()->file('import_file'));
Loading history...
79
            }catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
80
                $failures = $e->failures();
81
82
                foreach ($failures as $failure) {
83
                    $failure->row(); // row that went wrong
84
                    $failure->attribute(); // either heading key (if using heading row concern) or column index
85
                    $failure->errors(); // Actual error messages from Laravel validator
86
                    $failure->values(); // The values of the row that has failed.
87
                }
88
            }
89
90
           
91
        return back();
0 ignored issues
show
Bug Best Practice introduced by
The expression return back() returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
92
    }
93
}
94