Completed
Push — master ( 413e7c...13a6e1 )
by Sergi Tur
09:46
created

UsersMigrationController::pendingUsersToMigrate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Acacha\Users\Http\Controllers;
4
5
use Acacha\Users\Http\Requests\StopMigrationBatchRequest;
6
use Acacha\Users\Http\Requests\UsersMigrationRequest;
7
use Acacha\Users\Jobs\MigrateUsers;
8
use Acacha\Users\Models\ProgressBatch;
9
use Acacha\Users\Models\UserMigration;
10
use Acacha\Users\Services\MigrationBatch;
11
use Illuminate\Http\Request;
12
use Redis;
13
use Scool\EbreEscoolModel\User;
14
use App\User as UserOnDestination;
15
16
/**
17
 * Class UsersMigrationController.
18
 *
19
 * @package App\Http\Controllers
20
 */
21
class UsersMigrationController extends Controller
22
{
23
    /**
24
     * Show users migration.
25
     *
26
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
27
     */
28
    public function index()
29
    {
30
        $this->authorize('migrate-users');
31
        $data = [];
32
        return view('acacha_users::users-migration',$data);
33
    }
34
35
    /**
36
     * Get total number of users in source migration database.
37
     */
38
    public function totalNumberOfUsers()
39
    {
40
        $this->authorize('migrate-users');
41
        return [ 'data' => User::all()->count() ];
42
    }
43
44
    /**
45
     * Get total number of migrated users in source migration database.
46
     *
47
     * @return array
48
     */
49
    public function totalNumberOfMigratedUsers()
50
    {
51
        $this->authorize('migrate-users');
52
        $ids = UserMigration::all()->pluck('source_user_id');
53
        return [ 'data' => ($ids->count() > 0) ? User::whereIn('id',$ids)->count() : 0 ];
54
55
    }
56
57
    /**
58
     * Get total number of users in source migration database.
59
     */
60
    public function totalNumberOfUsersOnDestination()
61
    {
62
        $this->authorize('migrate-users');
63
        return [ 'data' => UserOnDestination::all()->count() ];
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function totalNumberOfMigratedUsersOnDestination()
70
    {
71
        $this->authorize('migrate-users');
72
        return [ 'data' => UserOnDestination::migrated()->count() ];
73
    }
74
75
    /**
76
     * Check connection.
77
     *
78
     * @return array
79
     */
80
    public function checkConnection(Request $request)
81
    {
82
        $this->authorize('migrate-users');
83
        $connection = $request->has('connection')
84
            ?  $request->has('connection')
85
            : config('users.source_database_connection_name');
86
        if (check_connection($connection)) return [ 'connected' => true ];
87
        return [ 'connected' => false ];
88
    }
89
90
    /**
91
     * Migrate multiple users.
92
     *
93
     */
94
    public function migrate(UsersMigrationRequest $request, MigrationBatch $batchService)
95
    {
96
        $batch = $batchService->init();
97
        $usersToMigrate = $this->usersToMigrateByRequest($request);
98
99
        //TODO cal?
100
        $this->clearUserMigrationsJobsQueue();
101
//        dispatch((new MigrateUsers($usersToMigrate, $batch))->onQueue('users-migration'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
        dispatch((new MigrateUsers($usersToMigrate, $batch)));
103
104
        return $batch;
105
106
    }
107
108
    /**
109
     * Clear user migrations job queue.
110
     *
111
     */
112
    protected function clearUserMigrationsJobsQueue() {
113
        Redis::connection()->del('queues:users-migration');
0 ignored issues
show
Bug introduced by
The method connection() does not exist on Redis. Did you maybe mean connect()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
114
    }
115
116
    /**
117
     * Stop migration batch request.
118
     */
119
    public function stopMigration(StopMigrationBatchRequest $request)
120
    {
121
        $batch = ProgressBatch::findOrFail($request->input('batch_id'));
122
        $batch->stop();
123
        $batch->save();
124
        $this->clearUserMigrationsJobsQueue();
125
        return [ 'stopped' => true ];
126
    }
127
128
    /**
129
     * Resume migration.
130
     */
131
    public function resumeMigration()
132
    {
133
        $this->authorize('migrate-users');
134
        $this->resumeMigrationBatch();
135
    }
136
137
    /**
138
     * Resume migration batch
139
     */
140
    protected function resumeMigrationBatch()
141
    {
142
        
143
    }
144
145
    /**
146
     * Get users migration history.
147
     *
148
     * @return array
149
     */
150
    public function history()
151
    {
152
        $this->authorize('migrate-users');
153
        return UserMigration::orderBy('created_at', 'desc')->paginate();
154
    }
155
156
    /**
157
     * Get users migration batch history.
158
     *
159
     * @return array
160
     */
161
    public function batchHistory()
162
    {
163
        $this->authorize('migrate-users');
164
        return ProgressBatch::orderBy('created_at', 'desc')->get();
165
    }
166
167
    /**
168
     * Get users to migrate by request.
169
     *
170
     * @param $request
171
     * @return \Illuminate\Database\Eloquent\Collection|void|static[]
172
     */
173
    protected function usersToMigrateByRequest($request)
174
    {
175
        if ($users = $request->has('users')) return $this->usersToMigrate($users, $request);
176
        if ($filter = $request->has('filter')) return $this->usersToMigrateByFilter($filter, $request);
177
        if ($filter = $request->has('all')) return $this->allUsers($request);
0 ignored issues
show
Unused Code introduced by
$filter is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
178
        return $this->pendingUsersToMigrate($request);
179
    }
180
181
    /**
182
     * Users to migrate by user id list.
183
     *
184
     * @param $users
185
     */
186
    protected function usersToMigrate($users, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $users is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
187
    {
188
189
    }
190
191
    /**
192
     * Switch default connection.
193
     *
194
     * @param $connection
195
     * @param $env
196
     */
197
    protected function switchConnection($env,$connection)
198
    {
199
        config(['database.default' => env($env, $connection)]);
200
    }
201
202
    /**
203
     * Dump current connection.
204
     */
205
    protected function dumpCurrentConnection()
206
    {
207
        dump(config('database.default'));
208
    }
209
210
    /**
211
     * Users to migrate by filter.
212
     *
213
     * @param $filter
214
     */
215
    protected function usersToMigrateByFilter($filter, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $filter is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
216
    {
217
        //TODO
218
    }
219
220
    /**
221
     * Get all valid users.
222
     *
223
     * @param $request
224
     * @return \Illuminate\Database\Eloquent\Collection|static[]
225
     */
226
    protected function allUsers($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
227
    {
228
        return \Scool\EbreEscoolModel\User::with('person')->get();
229
    }
230
231
    /**
232
     * Get all pending users to migrate.
233
     *
234
     * @param $request
235
     * @return \Illuminate\Database\Eloquent\Collection|static[]
236
     */
237
    protected function pendingUsersToMigrate($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
238
    {
239
        $ids = UserMigration::all()->pluck('source_user_id');
240
        return ($ids->count() > 0)
241
            ? \Scool\EbreEscoolModel\User::whereNotIn('id',$ids)->with('person')->get()
242
            : [] ;
243
    }
244
245
}
246