<commit_before><?php namespace redhotmayo\distribution;

use Exception;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use redhotmayo\api\GoogleMapsAPI;
use redhotmayo\api\SmartyStreetsAPI;
use redhotmayo\api\TexasAMAPI;
use redhotmayo\dataaccess\repository\CuisineRepository;
use redhotmayo\dataaccess\repository\RepositoryFactory;
use redhotmayo\dataaccess\repository\FoodServicesRepository;
use redhotmayo\dataaccess\repository\sql\UserRepositorySQL;
use redhotmayo\library\CuisineMapper;
use redhotmayo\library\ExcelParser;
use redhotmayo\library\FoodMap;
use redhotmayo\library\Timer;
use redhotmayo\model\Account;
use redhotmayo\notifications\GooglePushNotification;
use redhotmayo\parser\AccountParserS2;

class AccountDistribution extends Distribution {
    private $timer;

    public function __construct() {
        $this->timer = new Timer();
    }

    public function loadFromFile($filename) {
        parent::loadFromFile($filename);

        $this->leadDistribution();
    }

    /**
     * @throws \Exception
     * @internal param $filename
     */
    private function leadDistribution() {
        $filename = $this->getFileName();
        $this->timer->startTimer();

        Log::info('Parsing XLSX file...');
        $smartyStreets = new SmartyStreetsAPI();
        $tam = new TexasAMAPI();
        $google = new GoogleMapsAPI();
        $s2parser = new AccountParserS2($tam, $smartyStreets, $google);
        $function = array($s2parser, AccountParserS2::EXCEL_PROCESSOR);
        $excelParser = new ExcelParser();
        $accounts = $excelParser->parse($filename, $function);

        if (!isset($accounts) || !is_array($accounts)) {
            throw new Exception("Error with excel parsing. Results not set or not an array");
        }

        if (count($accounts) == 0) {
            App::info('No records found in XLSX file.');
            return;
        }




        $accountRepo = RepositoryFactory::GetAccountRepository();
        Log::info('Saving all records...');
        $unsaved = $accountRepo->saveAll($accounts);

        Log::info('Distributing leads to users...');
        $undistributed = $accountRepo->distributeAccountsToUsers($accounts);

        $newAccounts = $accountRepo->allAccountsDistributedToday();

        $userIds = [];

        foreach ($newAccounts as $acct) {
            $userIds[] = $acct['userId'];
        }

        $userIds = array_unique($userIds);
        $userRepo = new UserRepositorySQL();
        $users = [];
        foreach ($userIds as $uid) {
            $users[] = $userRepo->find(['id' => $uid]);
        }

        $data = ['notificationType' => 'newLeads'];

        foreach ($users as $user) {
            (new GooglePushNotification())->send($user, $data);
        }

        /** @var Account $error */
        foreach ($unsaved as $error) {
            //TODO: LOGGER: Log the accounts that were unable to save so they can be re-processed
            Log::error('Unsaved account to accounts table: ' . $error->getAccountName());
        }

        /** @var Account $error */
        foreach ($undistributed as $error) {
            //TODO: LOGGER: Log the accounts that were unable to save so they can be re-processed
            Log::error('Undistributed accounts to users: ' . $error->getAccountName());
        }
    }
}
<commit_msg>WS-33 updated AccountDistribution use statements<commit_after><?php namespace redhotmayo\distribution;

use Exception;
use Illuminate\Support\Facades\Log;
use redhotmayo\api\GoogleMapsAPI;
use redhotmayo\api\SmartyStreetsAPI;
use redhotmayo\api\TexasAMAPI;
use redhotmayo\dataaccess\repository\RepositoryFactory;
use redhotmayo\dataaccess\repository\sql\UserRepositorySQL;
use redhotmayo\library\ExcelParser;
use redhotmayo\library\Timer;
use redhotmayo\model\Account;
use redhotmayo\notifications\GooglePushNotification;
use redhotmayo\parser\AccountParserS2;

class AccountDistribution extends Distribution {
    private $timer;

    public function __construct() {
        $this->timer = new Timer();
    }

    public function loadFromFile($filename) {
        parent::loadFromFile($filename);

        $this->leadDistribution();
    }

    /**
     * @throws \Exception
     * @internal param $filename
     */
    private function leadDistribution() {
        $filename = $this->getFileName();
        $this->timer->startTimer();

        Log::info('Parsing XLSX file...');
        $smartyStreets = new SmartyStreetsAPI();
        $tam = new TexasAMAPI();
        $google = new GoogleMapsAPI();
        $s2parser = new AccountParserS2($tam, $smartyStreets, $google);
        $function = array($s2parser, AccountParserS2::EXCEL_PROCESSOR);
        $excelParser = new ExcelParser();
        $accounts = $excelParser->parse($filename, $function);

        if (!isset($accounts) || !is_array($accounts)) {
            throw new Exception("Error with excel parsing. Results not set or not an array");
        }

        if (count($accounts) == 0) {
            Log::info('No records found in XLSX file.');
            return;
        }

        //TODO: change this to an AccountDistributionService
        //AccountDistributionService::distribute($accounts)

        $accountRepo = RepositoryFactory::GetAccountRepository();
        Log::info('Saving all records...');
        $unsaved = $accountRepo->saveAll($accounts);

        Log::info('Distributing leads to users...');
        $undistributed = $accountRepo->distributeAccountsToUsers($accounts);

        $newAccounts = $accountRepo->allAccountsDistributedToday();

        $userIds = [];

        foreach ($newAccounts as $acct) {
            $userIds[] = $acct['userId'];
        }

        $userIds = array_unique($userIds);
        $userRepo = new UserRepositorySQL();
        $users = [];
        foreach ($userIds as $uid) {
            $users[] = $userRepo->find(['id' => $uid]);
        }

        $data = ['notificationType' => 'newLeads'];

        foreach ($users as $user) {
            (new GooglePushNotification())->send($user, $data);
        }

        /** @var Account $error */
        foreach ($unsaved as $error) {
            //TODO: LOGGER: Log the accounts that were unable to save so they can be re-processed
            Log::error('Unsaved account to accounts table: ' . $error->getAccountName());
        }

        /** @var Account $error */
        foreach ($undistributed as $error) {
            //TODO: LOGGER: Log the accounts that were unable to save so they can be re-processed
            Log::error('Undistributed accounts to users: ' . $error->getAccountName());
        }
    }
}

{'fraction_non_alphanumeric': 0.12836314444611455, 'fraction_numerical': 0.0021043138433789266, 'mean_word_length': 2.8241379310344827, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '1784867', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1890, 'n_words': 437}