ExportController::checkPermissions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Uccello\Core\Http\Controllers\Core;
4
5
use Illuminate\Http\Request;
6
use Uccello\Core\Exports\RecordsExport;
7
use Uccello\Core\Models\Domain;
8
use Uccello\Core\Models\Module;
9
10
class ExportController extends Controller
11
{
12
    /**
13
     * Check user permissions
14
     */
15
    protected function checkPermissions()
16
    {
17
        $this->middleware('uccello.permissions:retrieve');
18
    }
19
20
    /**
21
     * @inheritDoc
22
     */
23
    public function process(?Domain $domain, Module $module, Request $request)
24
    {
25
        // Pre-process
26
        $this->preProcess($domain, $module, $request);
27
28
        // File name
29
        $fileName = uctrans($module->name, $module).'_'.date('Ymd_His');
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Uccello\Core\Models\Module. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
30
31
        // File extension
32
        $fileExtension = $request->input('extension') ?? 'xlsx';
33
34
        // Use special format for pdf file
35
        $specialFormat = $fileExtension === 'pdf' ? \Maatwebsite\Excel\Excel::MPDF : null;
0 ignored issues
show
Bug introduced by
The type Maatwebsite\Excel\Excel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
37
        // Init export
38
        $export = (new RecordsExport)
39
                ->forDomain($domain)
40
                ->forModule($module);
41
42
        // With ID
43
        if ($request->input('with_id') === '1') {
44
            $export = $export->withId();
45
        }
46
47
        // With timestamps
48
        if ($request->input('with_timestamps') === '1') {
49
            $export = $export->withTimestamps();
50
        }
51
52
        // With descendants
53
        if ($request->input('with_descendants') === '1') {
54
            $export = $export->withDescendants();
55
        }
56
57
        // With hidden columns
58
        if ($request->input('with_hidden_columns') !== '1') {
59
            $columns = json_decode($request->input('columns'));
60
            $export = $export->withColumns($columns);
61
        }
62
63
        // With conditions
64
        if ($request->input('with_conditions') === '1') {
65
            // TODO: Build $conditions['search'] earlier in the export process to match filter format...
66
            $conditions = ['search' => json_decode($request->input('conditions'))];
67
            $export = $export->withConditions($conditions);
68
        }
69
70
        // With order
71
        if ($request->input('with_order') === '1') {
72
            $order = json_decode($request->input('order'));
73
            $export = $export->withOrder($order);
74
        }
75
76
        // Export records
77
        return $export->download($fileName.'.'.$fileExtension, $specialFormat);
78
    }
79
}
80