Issues (367)

app/Console/Commands/StudentsIdGen.php (14 issues)

1
<?php
2
3
namespace App\Console\Commands;
4
5
use Lsf\UniqueUid\UniqueUid;
6
use App\Models\Security_user;
7
use App\Models\Unique_user_id;
8
use Exception;
9
use Illuminate\Console\Command;
10
use Mohamednizar\MoeUuid\MoeUuid;
0 ignored issues
show
The type Mohamednizar\MoeUuid\MoeUuid was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
class StudentsIdGen extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'students:idgen {chunk} {max}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Command description';
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct()
34
    {
35
        $this->count = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property count does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
        $this->output = new \Symfony\Component\Console\Output\ConsoleOutput();
0 ignored issues
show
Documentation Bug introduced by
It seems like new Symfony\Component\Co...\Output\ConsoleOutput() of type Symfony\Component\Console\Output\ConsoleOutput is incompatible with the declared type Illuminate\Console\OutputStyle of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->students = new Security_user();
0 ignored issues
show
Bug Best Practice introduced by
The property students does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
        $this->uniqueUId = new UniqueUid();
0 ignored issues
show
Bug Best Practice introduced by
The property uniqueUId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
        $this->max  = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property max does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
        $this->child = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property child does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
        parent::__construct();
42
    }
43
44
    /**
45
     * Execute the console command.
46
     *
47
     * @return mixed
48
     */
49
    public function handle()
50
    {
51
        $students = $this->students->query()
52
            ->where('is_student', 1)
53
            ->limit(500000)
54
            ->get()->toArray();
55
56
        $students = array_chunk($students, $this->argument('chunk'));
0 ignored issues
show
$this->argument('chunk') of type array|null|string is incompatible with the type integer expected by parameter $length of array_chunk(). ( Ignorable by Annotation )

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

56
        $students = array_chunk($students, /** @scrutinizer ignore-type */ $this->argument('chunk'));
Loading history...
57
        $this->processParallel($students, $this->argument('max'));
58
    }
59
60
    public function processParallel(array $arr, $procs = 4)
61
    {
62
        // Break array up into $procs chunks.
63
        $chunks   = array_chunk($arr, ceil((count($arr) / $procs)));
0 ignored issues
show
ceil(count($arr) / $procs) of type double is incompatible with the type integer expected by parameter $length of array_chunk(). ( Ignorable by Annotation )

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

63
        $chunks   = array_chunk($arr, /** @scrutinizer ignore-type */ ceil((count($arr) / $procs)));
Loading history...
64
        $pid      = -1;
0 ignored issues
show
The assignment to $pid is dead and can be removed.
Loading history...
65
        $children = array();
66
        foreach ($chunks as $items) {
67
            $pid = pcntl_fork();
68
            if ($pid === -1) {
69
                die('could not fork');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
70
            } else if ($pid === 0) {
71
                $this->output->writeln('started processes: ' . count($children));
72
                // We are the child process. Pass a chunk of items to process.
73
                array_walk($items, array($this, 'process'));
74
                exit(0);
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
75
            } else {
76
                // We are the parent.
77
                $children[] = $pid;
78
            }
79
        }
80
        // Wait for children to finish.
81
        foreach ($children as $pid) {
82
            // We are still the parent.
83
            pcntl_waitpid($pid, $status);
84
        }
85
    }
86
87
88
89
    public function process($students)
90
    {
91
        array_walk($students, array($this, 'updateNewUUID'));
92
    }
93
94
95
    /*
96
     * @param $student
97
     * @throws \Exception
98
     */
99
    public function updateNewUUID($student)
100
    {
101
        $this->uniqueUserId = new Unique_user_id();
0 ignored issues
show
Bug Best Practice introduced by
The property uniqueUserId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
102
        if (!$this->uniqueUId::isValidUniqueId($student['openemis_no'])) {
103
            try {
104
105
                $newId = $this->uniqueUId::getUniqueAlphanumeric();
106
                $student['openemis_no'] = $newId;
107
                $student =  $this->uniqueUserId->updateOrInsertRecord($student);
108
                // $this->output->writeln('New NSID generated for :' . $student['id']);
109
                Security_user::query()->where('id', $student['id'])
110
                    ->update(['openemis_no' => $newId, 'username' => str_replace('-', '', $newId)]);
111
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
112
            }
113
        } else {
114
            try {
115
                // $this->output->writeln('Updating student:' . $student['id']);
116
                $this->uniqueUserId->updateOrInsertRecord($student);
117
            } catch (\Exception $e) {
118
                dd($e);
119
            }
120
        }
121
    }
122
}
123