Completed
Push — master ( 2ff197...58043b )
by Sergi Tur
06:57
created

UsersMigrationController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A totalNumberOfUsers() 0 5 1
A migrate() 0 13 2
A usersToMigrateByRequest() 0 6 3
A usersToMigrate() 0 4 1
A usersToMigrateByFilter() 0 4 1
A allUsers() 0 4 1
1
<?php
2
3
namespace Acacha\Users\Http\Controllers;
4
5
use Acacha\Users\Events\UserMigrationStatusUpdated;
6
use Acacha\Users\Http\Requests\UsersMigrationRequest;
7
8
/**
9
 * Class UsersMigrationController.
10
 *
11
 * @package App\Http\Controllers
12
 */
13
class UsersMigrationController extends Controller
14
{
15
    /**
16
     * Show users migration.
17
     *
18
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
19
     */
20
    public function index()
21
    {
22
        $this->authorize('migrate-users');
23
        $data = [];
24
        return view('acacha_users::users-migration',$data);
25
    }
26
27
    /**
28
     * Get total number of users in source migration database.
29
     */
30
    public function totalNumberOfUsers()
31
    {
32
        $this->authorize('migrate-users');
33
        return [ 'data' => 68 ];
34
    }
35
36
    /**
37
     * Migrate multiple users.
38
     *
39
     */
40
    public function migrate(UsersMigrationRequest $request)
41
    {
42
        $usersToMigrate = $this->usersToMigrateByRequest($request);
43
        dd($usersToMigrate);
44
        $progress= 0;
45
        foreach ($usersToMigrate as $user ) {
0 ignored issues
show
Bug introduced by
The expression $usersToMigrate of type null|object<Illuminate\D...rsMigrationController>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46
            event(new UserMigrationStatusUpdated($user, $progress));
47
            sleep(1);
48
            dump ('User ' . $user . ' migrated!');
49
            $progress = $progress + 50;
50
        }
51
        event(new UserMigrationStatusUpdated('Finish!', $progress));
52
    }
53
54
55
    /**
56
     * Get users to migrate.
57
     *
58
     * @param $request
59
     */
60
    protected function usersToMigrateByRequest($request)
61
    {
62
        if ($users = $request->has('users')) return $this->usersToMigrate($users, $request);
63
        if ($filter = $request->has('filter')) return $this->usersToMigrateByFilter($filter, $request);
64
        return $this->allUsers($request);
65
    }
66
67
    /**
68
     * Users to migrate by user id list.
69
     *
70
     * @param $users
71
     */
72
    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...
73
    {
74
75
    }
76
77
    /**
78
     * Users to migrate by filter.
79
     *
80
     * @param $filter
81
     */
82
    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...
83
    {
84
85
    }
86
87
    /**
88
     * Get all valid users.
89
     *
90
     * @param $request
91
     * @return \Illuminate\Database\Eloquent\Collection|static[]
92
     */
93
    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...
94
    {
95
        return \Scool\EbreEscoolModel\User::all();
96
    }
97
}
98