<?php

class UserTableSSNEncrypter extends Seeder {

    public function run()
    {
        // Disable query logging to avoid memory exhaustion
        DB::disableQueryLog();

        $this->command->info('Users SSN Encryption started');

        // Initiates time
        $time_start = microtime(true);



        try
        {
            $users = User::all();

            foreach ($users as $user)
            {

                if(strlen($user->ssn)>9)
                    continue;

                DB::table('users')
                    ->where('id', $user->id)
                    ->update(array(
                        'ssn' => Crypt::encrypt($user->ssn),
                        // 'program_id' => $program_id, //should change when I know how this info will be given
                        // 'updated_at' => date("Y-m-d H:i:s", time()),
                        )
                    );

                // Uncomment line below to see output
                // $this->command->info('Encrypted ssn for '.$user->email.': '.$user->surnames.', '.$user->first_name);

            }

        }

        // If an exception is raised, show the message and add to error
        catch(Exception $e)
        {
            $this->command->info($e->getMessage());
        };


        // Stop time
        $time_end = microtime(true);

        // Display results
        $this->command->info('------------------------------------------------------------');
    }

}
