DashboardController::updateTask()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
ccs 17
cts 17
cp 1
rs 8.9713
cc 1
eloc 15
nc 1
nop 1
crap 1
1
<?php
2
3
namespace agoalofalife\postman\Http\Controllers;
4
5
use agoalofalife\postman\Http\Requests\TaskRequest;
6
use agoalofalife\postman\Models\Email;
7
use agoalofalife\postman\Models\EmailUser;
8
use agoalofalife\postman\Models\ModePostEmail;
9
use agoalofalife\postman\Models\SheduleEmail;
10
use agoalofalife\postman\Models\Status;
11
use agoalofalife\postman\Models\User;
12
use Illuminate\Http\JsonResponse;
13
use Illuminate\Http\Request;
14
use Illuminate\Support\Collection;
15
16
class DashboardController
17
{
18
    /**
19
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|static[] | void
20
     */
21 1
    public function index() : Collection
22
    {
23 1
        return SheduleEmail::with(['email.users', 'mode', 'status'])->get();
24
    }
25
26
    /**
27
     * Get list modes
28
     * @return JsonResponse
29
     */
30 1
    public function listMode() : JsonResponse
31
    {
32 1
        return response()->json(ModePostEmail::all());
0 ignored issues
show
Bug introduced by
The method json() does not exist on Symfony\Component\HttpFoundation\Response. It seems like you code against a sub-type of Symfony\Component\HttpFoundation\Response such as Illuminate\Http\Response or Illuminate\Http\JsonResponse or Illuminate\Http\RedirectResponse. ( Ignorable by Annotation )

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

32
        return response()->/** @scrutinizer ignore-call */ json(ModePostEmail::all());
Loading history...
33
    }
34
35
    /**
36
     * @return JsonResponse
37
     */
38 1
    public function users() : JsonResponse
39
    {
40 1
        return response()->json(User::all());
41
    }
42
43
    /**
44
     * @return JsonResponse
45
     */
46 1
    public function statuses() : JsonResponse
47
    {
48 1
        return response()->json(Status::all());
49
    }
50
51
    /**
52
     * Get table column
53
     * @return \Illuminate\Http\JsonResponse
54
     */
55 1
    public function tableColumn() : JsonResponse
56
    {
57 1
        $response = [];
58
59 1
        foreach (config('postman.ui.table') as $column => $size) {
60 1
            $response['columns'][] = [
61 1
                'prop' => $column,
62 1
                'size' => $size,
63 1
                'label' => trans("postman::dashboard.{$column}")
64
            ];
65
        }
66
        
67 1
        $response['button'] = [
68 1
            'edit' => trans('postman::dashboard.button.edit'),
69 1
            'remove' => trans('postman::dashboard.button.remove'),
70
        ];
71
72 1
        return response()->json($response);
73
    }
74
75
    /**
76
     * Base this is text for form
77
     * @return \Illuminate\Http\JsonResponse
78
     */
79 1
    public function formColumn()
80
    {
81
        $forms = [
82 1
           'date' =>  [
83 1
                'label' => trans('postman::dashboard.date'),
84
                'rule' => [
85
                    'required'=> true, 'message'=> 'Please input Activity name', 'trigger'=> 'blur',
86
                ]
87
            ],
88
            'theme' => [
89 1
                'label' => trans('postman::dashboard.email.theme'),
90
            ],
91
            'text' => [
92 1
                'label' => trans('postman::dashboard.email.text'),
93
            ],
94
            'type' => [
95 1
                'label' => trans('postman::dashboard.mode.name'),
96 1
                'placeholder' => trans('postman::dashboard.mode.placeholder'),
97
            ],
98
            'users' => [
99 1
                'label' => trans('postman::dashboard.users.name'),
100 1
                'placeholder' => trans('postman::dashboard.users.placeholder'),
101
            ],
102
           'statuses' => [
103 1
               'label' => trans('postman::dashboard.statuses.name'),
104 1
               'placeholder' => trans('postman::dashboard.statuses.placeholder'),
105
           ],
106
            'button' => [
107 1
                'success' => trans('postman::dashboard.form.button.success'),
108 1
                'cancel' => trans('postman::dashboard.form.button.cancel'),
109
            ],
110
            'popup' => [
111 1
              'question' => trans('postman::dashboard.popup.question'),
112 1
              'title' => trans('postman::dashboard.popup.title'),
113 1
              'confirmButtonText' => trans('postman::dashboard.popup.confirmButtonText'),
114 1
              'cancelButtonText' => trans('postman::dashboard.popup.cancelButtonText'),
115 1
              'success.message' => trans('postman::dashboard.popup.success.message'),
116 1
              'info.message' => trans('postman::dashboard.popup.info.message'),
117
            ],
118
        ];
119
120 1
        return response()->json($forms);
121
    }
122
123
    /**
124
     * @param TaskRequest $request
125
     * @return \Illuminate\Http\JsonResponse
126
     */
127 1
    public function createTask(TaskRequest $request)
128
    {
129 1
        $request->datePostman($request);
0 ignored issues
show
Bug introduced by
The method datePostman() does not exist on agoalofalife\postman\Http\Requests\TaskRequest. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

129
        $request->/** @scrutinizer ignore-call */ 
130
                  datePostman($request);
Loading history...
130
131 1
        $email = Email::create([
132 1
            'theme' => $request->theme,
133 1
            'text' => $request->text,
134
        ]);
135
136 1
        array_map(function($user_id) use($email) {
137 1
            EmailUser::create([
138 1
                'user_id' => $user_id,
139 1
                'email_id' => $email->id,
140
            ]);
141 1
        }, $request->users);
142
143 1
        $email->tasks()->create([
144 1
            'date' => $request->date,
145 1
            'mode_id' => $request->mode,
146 1
            'status_id' => $request->statuses
147
        ]);
148
149 1
        return response()->json(['status' => true]);
150
    }
151
152
    /**
153
     * Update task
154
     * @param TaskRequest $request
155
     * @return \Illuminate\Http\JsonResponse
156
     */
157 1
    public function updateTask(TaskRequest $request)
158
    {
159 1
        $request->datePostman($request);
160 1
        $task = SheduleEmail::find($request->id);
161
162 1
        $task->update([
163 1
            'mode_id' => $request->mode,
164 1
            'date' => $request->date,
165 1
            'status_id' => $request->statuses,
166
        ]);
167
168 1
        $task->email->update([
169 1
            'theme' => $request->theme,
170 1
            'text' => $request->text,
171
        ]);
172
173 1
        $goalList = EmailUser::where('email_id', $task->email->id);
174 1
        $removal  = $goalList->get()->pluck('user_id')->diff($request->users)->values();
175 1
        $goalList->whereIn('user_id', $removal)->delete();
176
177 1
        collect($request->users)->each(function($user_id) use($task, &$usersToEmail) {
178 1
            EmailUser::firstOrCreate(['email_id' => $task->email->id, 'user_id' => $user_id]);
179 1
        });
180 1
        return response()->json(['status' => true]);
181
    }
182
    /**
183
     * @param $id
184
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
185
     */
186 1
    public function remove($id)
187
    {
188 1
        SheduleEmail::destroy($id);
189 1
        return response()->json(true);
190
    }
191
}