<commit_before><?php

namespace App\Console\Commands;

use App\User;
use App\Contact;
use Illuminate\Console\Command;

class ImportCSV extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:csv {user} {file}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Imports CSV in Google format to user account';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $file = $this->argument('file');
        $user = User::find($this->argument('user'));
        $this->info("Importing CSV file $file to user {$user->id}");
        $row = 0;
        $imported = 0;
        if (($handle = fopen($file, "r")) !== FALSE) {
            while (($data = fgetcsv($handle)) !== FALSE) {
                $row++;

                // don't import the columns
                if ( $row == 1 ) { continue; }

                $contact = new Contact();
                $contact->account_id = $user->id;

                // if first & last name do not exist skip row
                if ( empty( $data[1] ) && empty( $data[3] ) ) {
                    continue;
                }

                if ( ! empty( $data[1] ) ) {
                    $contact->first_name = $data[1];    // Given Name
                }

                if ( ! empty( $data[2] ) ) {
                    $contact->middle_name = $data[2];   // Additional Name
                }

                if ( ! empty( $data[3] ) ) {
                    $contact->last_name = $data[3];     // Family Name
                }

                if ( ! empty( $data[14] ) ) {
                    $contact->birthdate = date('Y-m-d', strtotime($data[14]));
                }

                if ( ! empty( $data[28] ) ) {
                    $contact->email = $data[28];        // Email 1 Value
                }

                if ( ! empty( $data[42 ] ) ) {
                    $contact->phone_number = $data[42]; // Phone 1 Value
                }

                if ( ! empty( $data[49] ) ) {
                    $contact->street = $data[49];       // address 1 street
                }

                if ( ! empty( $data[50] ) ) {
                    $contact->city = $data[50];         // address 1 city
                }
                if ( ! empty( $data[52] ) ) {
                    $contact->province = $data[52];     // address 1 region (state)
                }

                if ( ! empty( $data[53] ) ) {
                    $contact->postal_code = $data[53];  // address 1 postal code (zip) 53
                }
                if ( ! empty( $data[66] ) ) {
                    $contact->job = $data[66];          // organization 1 name 66
                }
             
                // can't have empty email
                if ( empty( $contact->email ) ) {
                    $contact->email = NULL;
                }

                $contact->save();
                $contact->setAvatarColor();
                $imported++;
            }
            fclose($handle);
        }
        $this->info("Imported {$imported} Contacts");
    }
}<commit_msg>Small refactor of CSV importing (#320)<commit_after><?php

namespace App\Console\Commands;

use App\User;
use App\Contact;
use Illuminate\Console\Command;

class ImportCSV extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:csv {user : email or id of a user} {file : path to the CSV file} {--format=google}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Imports CSV in Google format to user account';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $file = $this->argument('path');

        if (is_int($this->argument('user'))) {
            $user = User::find($this->argument('user'));
        } else {
            $user = User::where('email', $this->argument('user'))->first();
        }

        if (!file_exists($file)) {
            $this->error('You need to provide a valid file path.');
            return -1;
        }

        if (!$user) {
            $this->error('You need to provide a valid User ID or email address!');
            return -1;
        }

        $this->info("Importing CSV file $file to user {$user->id}");

        $row = 0;
        $imported = 0;
        if (($handle = fopen($file, "r")) !== FALSE) {
            while (($data = fgetcsv($handle)) !== FALSE) {
                $row++;

                // don't import the columns
                if ($row == 1) {
                    continue;
                }

                $contact = new Contact();
                $contact->account_id = $user->id;

                // if first & last name do not exist skip row
                if (empty($data[1]) && empty($data[3])) {
                    continue;
                }

                if (!empty($data[1])) {
                    $contact->first_name = $data[1];    // Given Name
                }

                if (!empty($data[2])) {
                    $contact->middle_name = $data[2];   // Additional Name
                }

                if (!empty($data[3])) {
                    $contact->last_name = $data[3];     // Family Name
                }

                if (!empty($data[14])) {
                    $contact->birthdate = date('Y-m-d', strtotime($data[14]));
                }

                if (!empty($data[28])) {
                    $contact->email = $data[28];        // Email 1 Value
                }

                if (!empty($data[42])) {
                    $contact->phone_number = $data[42]; // Phone 1 Value
                }

                if (!empty($data[49])) {
                    $contact->street = $data[49];       // address 1 street
                }

                if (!empty($data[50])) {
                    $contact->city = $data[50];         // address 1 city
                }
                if (!empty($data[52])) {
                    $contact->province = $data[52];     // address 1 region (state)
                }

                if (!empty($data[53])) {
                    $contact->postal_code = $data[53];  // address 1 postal code (zip) 53
                }
                if (!empty($data[66])) {
                    $contact->job = $data[66];          // organization 1 name 66
                }

                // can't have empty email
                if (empty($contact->email)) {
                    $contact->email = NULL;
                }

                $contact->save();
                $contact->setAvatarColor();
                $imported++;
            }
            fclose($handle);
        }

        $this->info("Imported {$imported} Contacts");
    }
}
{'fraction_non_alphanumeric': 0.1450350226617223, 'fraction_numerical': 0.015519846174975965, 'mean_word_length': 1.1861302912038427, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7537790', 'n_tokens_mistral': 2116, 'n_tokens_neox': 1904, 'n_words': 478}