Passed
Branch beta (1b8e35)
by Jon
07:16
created

SelectWorksheetsStep::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/** Created by PhpStorm,  User: jonphipps,  Date: 2017-05-31,  Time: 11:14 AM */
4
5
namespace App\Wizard\Import\ProjectSteps;
6
7
use App\Jobs\ParseVocabulary;
8
use App\Models\Batch;
9
use App\Models\ConceptAttribute;
10
use App\Models\ElementAttribute;
11
use App\Models\Export;
12
use App\Models\Import;
13
use Illuminate\Http\Request;
14
use Illuminate\Support\Facades\Validator;
15
use Smajti1\Laravel\Step;
16
use Smajti1\Laravel\Wizard;
17
use function dispatch;
18
use function explode;
19
20
class SelectWorksheetsStep extends Step
21
{
22
    public static $label = 'Choose the Worksheets...';
23
    public static $slug  = 'worksheets';
24
    public static $view  = 'frontend.import.project.steps.worksheets';
25
26
    public function fields(): array
27
    {
28
        return [
29
            [
30
                'name'  => 'worksheets',
31
                'label' => 'Select the worksheets to import',
32
                'type'  => 'jqxgrid_select',
33
            ],
34
        ];
35
    }
36
37
    public function PreProcess(Request $request, Wizard $wizard)
1 ignored issue
show
Unused Code introduced by
The parameter $request 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

37
    public function PreProcess(/** @scrutinizer ignore-unused */ Request $request, Wizard $wizard)

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...
38
    {
39
        $worksheets = [];
40
        $sheets     = $wizard->data()['googlesheets'];
41
        //it's already in the correct form
42
        if (is_array($sheets) && isset($sheets[0])) {
43
            return;
44
        }
45
        foreach ($sheets as $key => $worksheet) {
46
            $export               = Export::find($worksheet['id']);
47
            $sheet['worksheet']   = $export->worksheet;
48
            $sheet['languages']   = $export->languages;
49
            $sheet['exported_at'] = $export->created_at->toDayDateTimeString();
0 ignored issues
show
Bug introduced by
The method toDayDateTimeString() does not exist on DateTime. It seems like you code against a sub-type of said class. However, the method does not exist in MockeryTest_ClassThatDescendsFromInternalClass. Are you sure you never get one of those? ( Ignorable by Annotation )

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

49
            /** @scrutinizer ignore-call */ 
50
            $sheet['exported_at'] = $export->created_at->toDayDateTimeString();
Loading history...
50
            if ($export->elementset) {
51
                $id                     = $export->elementset->id;
52
                $sheet['last_edit']     = ElementAttribute::getLatestDateForElementSet($id);
53
                $sheet['elementset_id'] = $id;
54
            }
55
            if ($export->vocabulary) {
56
                $id                     = $export->vocabulary->id;
57
                $sheet['last_edit']     = ConceptAttribute::getLatestDateForVocabulary($id);
58
                $sheet['vocabulary_id'] = $id;
59
            }
60
            $sheet['last_edit']            =
61
                $sheet['last_edit'] ? $sheet['last_edit']->toDayDateTimeString() : 'Never Edited';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sheet seems to be defined later in this foreach loop on line 47. Are you sure it is defined here?
Loading history...
62
            $sheet['id']                   = $export->id . '::' . $key;
63
            $lastImport                    = $export->getLatestImport();
64
            $sheet['last_import']          =
65
                ($lastImport && $lastImport->imported_at) ? $lastImport->imported_at->toDayDateTimeString() :
66
                    'Never Imported';
67
            $sheet['last_import_batch_id'] = ($lastImport && $lastImport->batch_id) ? $lastImport->batch_id : '';
68
            $worksheets[]                  = $sheet;
69
        }
70
        $data                 = $wizard->data();
71
        $data['googlesheets'] = $worksheets;
72
        $wizard->data($data);
73
    }
74
75
    public function process(Request $request): void
76
    {
77
        $worksheets[] = json_decode($request->selected_worksheets);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$worksheets was never initialized. Although not strictly required by PHP, it is generally a good practice to add $worksheets = array(); before regardless.
Loading history...
78
        $spreadsheet  = $this->wizard->dataGet('spreadsheet');
79
        $batch_id     = $this->wizard->dataGet('batch_id');
80
        /** @var Batch $batch */
81
        $batch                = Batch::findOrFail($batch_id);
82
        $batch->total_count   = count($worksheets[0]);
83
        $unfinishedImports    = $batch->imports()->whereNull('imported_at');
84
        if ($unfinishedImports) {
85
            $unfinishedImports->delete();
86
        }
87
        $batch->save();
88
89
        // setup a job for each worksheet
90
        foreach ($worksheets[0] as $worksheet) {
91
            [ $export_id, $worksheet_id ] = explode('::', $worksheet);
92
            $export                       = Export::find($export_id);
93
            $import                       =
94
                Import::create([
95
                    'worksheet'     => $worksheet_id,
96
                    'source'        => 'Google',
97
                    'user_id'       => auth()->id(),
98
                    'batch_id'      => $batch_id,
99
                    'schema_id'     => $export->schema_id,
100
                    'vocabulary_id' => $export->vocabulary_id,
101
                ]);
102
            $export->addImports($import);
103
            // setup a job to run the worksheet processor to get the change instructions for each vocab
104
            dispatch(new ParseVocabulary($import, $worksheet_id, $spreadsheet));
105
            // each worksheet being processed will have its own checkbox table, hidden as a master->detail
106
            //it should look as if each of the worksheet selection rows had simply expanded to show the instructions
107
            // the row itself should have the master of the master-detail row just below with a summary of the changes
108
            $foo = 'bar';
0 ignored issues
show
Unused Code introduced by
The assignment to $foo is dead and can be removed.
Loading history...
109
        }
110
111
        // save progress to session
112
        $this->saveProgress($request, ['googlesheets' => $this->wizard->dataGet('googlesheets')]);
113
    }
114
115
    public function validate(Request $request)
116
    {
117
        Validator::make(['selected_worksheets' => json_decode($request->selected_worksheets)],
118
            ['selected_worksheets'          => 'required'],
119
            ['selected_worksheets.required' => 'You must select at least one worksheet before you can move to the next step.'])
120
            ->validate();
121
    }
122
123
    public function rules(Request $request = null)
124
    {
125
        return [
126
            'selected_worksheets' => 'required',
127
        ];
128
    }
129
}
130