Test Failed
Push — master ( e27e96...62a61a )
by Chubarov
05:17
created

DashboardController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
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
    public function index() : Collection
22 1
    {
23 1
        return SheduleEmail::with(['email.users', 'mode', 'status'])->get();
24 1
    }
25 1
26
    /**
27 1
     * Get list modes
28
     * @return JsonResponse
29
     */
30
    public function listMode() : JsonResponse
31
    {
32
        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 1
35
    /**
36 1
     * @return JsonResponse
37
     */
38
    public function users() : JsonResponse
39
    {
40
        return response()->json(User::all());
41
    }
42 1
43
    /**
44 1
     * @return JsonResponse
45
     */
46
    public function statuses() : JsonResponse
47
    {
48
        return response()->json(Status::all());
49
    }
50
51 1
    /**
52
     * Get table column
53 1
     * @return \Illuminate\Http\JsonResponse
54
     */
55 1
    public function tableColumn() : JsonResponse
56 1
    {
57 1
        $response = [];
58 1
59 1
        foreach (config('postman.ui.table') as $column => $size) {
60
            $response['columns'][] = [
61
                'prop' => $column,
62
                'size' => $size,
63 1
                'label' => trans("postman::dashboard.{$column}")
64 1
            ];
65 1
        }
66
        
67
        $response['button'] = [
68 1
            'edit' => trans('postman::dashboard.button.edit'),
69
            'remove' => trans('postman::dashboard.button.remove'),
70
        ];
71
72
        return response()->json($response);
73
    }
74
75 1
    /**
76
     * Base this is text for form
77
     * @return \Illuminate\Http\JsonResponse
78 1
     */
79 1
    public function formColumn()
80
    {
81
        $forms = [
82
           'date' =>  [
83
                'label' => trans('postman::dashboard.date'),
84
                'rule' => [
85 1
                    'required'=> true, 'message'=> 'Please input Activity name', 'trigger'=> 'blur',
86
                ]
87
            ],
88 1
            'theme' => [
89
                'label' => trans('postman::dashboard.email.theme'),
90
            ],
91 1
            '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 1
           ],
106 1
            'button' => [
107 1
                'success' => trans('postman::dashboard.form.button.success'),
108 1
                'cancel' => trans('postman::dashboard.form.button.cancel'),
109
            ],
110
            'popup' => [
111
              'question' => trans('postman::dashboard.popup.question'),
112 1
              'title' => trans('postman::dashboard.popup.title'),
113
              'confirmButtonText' => trans('postman::dashboard.popup.confirmButtonText'),
114
              'cancelButtonText' => trans('postman::dashboard.popup.cancelButtonText'),
115
              'success.message' => trans('postman::dashboard.popup.success.message'),
116
              'info.message' => trans('postman::dashboard.popup.info.message'),
117
            ],
118
        ];
119 1
120
        return response()->json($forms);
121 1
    }
122
123 1
    /**
124 1
     * @param TaskRequest $request
125 1
     * @return \Illuminate\Http\JsonResponse
126
     */
127
    public function createTask(TaskRequest $request)
128 1
    {
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 1
131 1
        $email = Email::create([
132
            'theme' => $request->theme,
133 1
            'text' => $request->text,
134
        ]);
135 1
136 1
        array_map(function($user_id) use($email) {
137 1
            EmailUser::create([
138 1
                'user_id' => $user_id,
139
                'email_id' => $email->id,
140 1
            ]);
141
        }, $request->users);
142
143
        $email->tasks()->create([
144
            'date' => $request->date,
145
            'mode_id' => $request->mode,
146
            'status_id' => $request->statuses
147
        ]);
148 1
149
        return response()->json(['status' => true]);
150 1
    }
151 1
152 1
    /**
153 1
     * Update task
154 1
     * @param TaskRequest $request
155
     * @return \Illuminate\Http\JsonResponse
156 1
     */
157 1
    public function updateTask(TaskRequest $request)
158 1
    {
159
        $request->datePostman($request);
160
        $task = SheduleEmail::find($request->id);
161 1
162 1
        $task->update([
163 1
            'mode_id' => $request->mode,
164
            'date' => $request->date,
165 1
            'status_id' => $request->statuses,
166 1
        ]);
167 1
168 1
        $task->email->update([
169
            'theme' => $request->theme,
170
            'text' => $request->text,
171
        ]);
172
173
        $goalList = EmailUser::where('email_id', $task->email->id);
174 1
        $removal  = $goalList->get()->pluck('user_id')->diff($request->users)->values();
175
        $goalList->whereIn('user_id', $removal)->delete();
176 1
177 1
        collect($request->users)->each(function($user_id) use($task, &$usersToEmail) {
178
            EmailUser::firstOrCreate(['email_id' => $task->email->id, 'user_id' => $user_id]);
179
        });
180
        return response()->json(['status' => true]);
181
    }
182
    /**
183
     * @param $id
184
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
185
     */
186
    public function remove($id)
187
    {
188
        SheduleEmail::destroy($id);
189
        return response()->json(true);
190
    }
191
}