Odyssey
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
LoanDataGenerator Class Reference

Public Member Functions

 __construct (CsvToArray $csv_obj, DbLoanCreator $db_obj, LoanGeneratorMapper $mapper)
 
 WaitForInput ()
 

Protected Member Functions

 Set ($prop, $object)
 
 SetInputLine ()
 
 SetConsoleMessage ()
 
 ProcessInput ()
 
 IsInputEmpty ()
 
 UserHasSelectedVolume ()
 
 UserHasConfirmed ()
 
 GenerateLoanRecords ()
 
 ParseCsv ()
 
 ContinueProcessing ()
 
 SetDetailTemplate ()
 
 MapCsvToData ()
 
 CreateSchemaRecords ($row)
 
 CreateMasterSchema ($row)
 
 CreateDetailSchema ($loan_id)
 
 InsertUserLoan ($loan_id, $user_id, $row)
 
 InsertChallengeResponses ($user_id)
 
 InsertUser ($row)
 
 GetLoanAppUserId ()
 
 ReturnResponse ()
 
 SuccessResponses ()
 
 ErrorResponses ()
 
 ResetInputValues ()
 
 UpdateResponses ($response=[])
 
 UpdateSuccessCounts ($response=[])
 
 UpdateResponseErrors ($response=[])
 
 AddNewErrors ($table, $error_arr)
 
 IsErrors ()
 

Protected Attributes

 $Mapper
 
 $CsvToArray
 
 $DbLoanCreator
 
 $test_credentials = '1234'
 
 $confirmed = false
 
 $num_records = 0
 
 $csv_line_count = 0
 
 $line = ''
 
 $schema_template = 8
 
 $detail_template = []
 
 $csv_data = []
 
 $success_counts = []
 
 $errors = []
 

Detailed Description

Generates sample loans with random for local testing. Run via command line in PHP.

Usage: $obj = new LoanDataGenerator(CsvObject $csv_obj, DbLoanCreator $db_obj, LoanGeneratorMapper $mapper); $obj->execute();

You will be prompted only with "how many loans to create" and asked to confirm. When complete, it will give you the test user passwords to log in with (see DB lnappuser for login info.)

Change the random data by modifying sample-loans-05-19.csv.

Definition at line 17 of file LoanDataGenerator.php.

Constructor & Destructor Documentation

◆ __construct()

LoanDataGenerator::__construct ( CsvToArray  $csv_obj,
DbLoanCreator  $db_obj,
LoanGeneratorMapper  $mapper 
)

LoanDataGenerator constructor.

Parameters
CsvToArray$csv_objinstance
DbLoanCreator$db_objinstance
LoanGeneratorMapper$mapperinstance
Returns
void

Definition at line 66 of file LoanDataGenerator.php.

66  {
67 
68  $this
69  ->Set('Mapper', $mapper)
70  ->Set('CsvToArray', $csv_obj)
71  ->Set('DbLoanCreator', $db_obj);
72  }

Member Function Documentation

◆ AddNewErrors()

LoanDataGenerator::AddNewErrors (   $table,
  $error_arr 
)
protected

Helper for above to simplify code. Don't have to test values, wouldn't be here is there wasn't at least one member in $error_arr

Parameters
string$table
array$error_arr
Returns
$this

Definition at line 581 of file LoanDataGenerator.php.

581  {
582 
583  if (! isset($this->errors[$table])) {
584  $this->errors[$table] = [];
585  }
586 
587  foreach ($error_arr as $err) {
588  $this->errors[$table][] = $err;
589  }
590 
591  return $this;
592  }

◆ ContinueProcessing()

LoanDataGenerator::ContinueProcessing ( )
protected

If we have reached number of lines or end of file, stop processing; if exceeded num_records, close the CSV, handle will still be open.

Returns
bool

Definition at line 246 of file LoanDataGenerator.php.

246  {
247 
248  if ($this->csv_line_count > $this->num_records - 1) {
249  $this->CsvToArray->CloseFile();
250  return false;
251  }
252 
253  if (! $this->CsvToArray->IsLinesToProcess()) {
254  return false;
255  }
256 
257  return true;
258 
259  }
IsLinesToProcess()
Definition: CsvToArray.php:91

◆ CreateDetailSchema()

LoanDataGenerator::CreateDetailSchema (   $loan_id)
protected

Insert the schema detail records for our current loan.

Parameters
int$loan_id
Returns
$this

Definition at line 355 of file LoanDataGenerator.php.

355  {
356 
357  $map = $this->Mapper->CreateDetailTemplateMap($loan_id, $this->detail_template);
358 
359  $response = $this->DbLoanCreator->AddSampleData($map);
360 
361  $this->UpdateResponses($response);
362 
363  return $this;
364  }
AddSampleData($data=[])
UpdateResponses($response=[])

◆ CreateMasterSchema()

LoanDataGenerator::CreateMasterSchema (   $row)
protected

Create the master schema and return the ID of the inserted record. In later PHP/SQL versions, this will automatically return the record ID inserted, but can't do that here so we are doing two things in one method (which is bad.)

Parameters
array$row
Exceptions
Exception
Returns
int

Definition at line 339 of file LoanDataGenerator.php.

339  {
340 
341  $map = $this->Mapper->MasterSchemaMap($row);
342  $response = $this->DbLoanCreator->AddSampleData($map);
343  $this->UpdateResponses($response);
344 
345  $data = $this->DbLoanCreator->GetLastRecord('lnappschemamaster', 'loanid');
346 
347  return $data['data']['loanid'];
348  }
AddSampleData($data=[])
UpdateResponses($response=[])
GetLastRecord($table, $keyfield)

◆ CreateSchemaRecords()

LoanDataGenerator::CreateSchemaRecords (   $row)
protected

Set the master schema and get the id (see comments below.) Set the detail schema.

Parameters
array$rowa row of data from the CSV.
Exceptions
Exception
Returns
int

Definition at line 323 of file LoanDataGenerator.php.

323  {
324 
325  $loan_id = $this->CreateMasterSchema($row);
326  $this->CreateDetailSchema($loan_id);
327 
328  return $loan_id;
329  }

◆ ErrorResponses()

LoanDataGenerator::ErrorResponses ( )
protected

Ditto, helper for ReturnResponse().

Returns
string|null

Definition at line 488 of file LoanDataGenerator.php.

488  {
489 
490  $msg = null;
491 
492  if ($this->IsErrors()) {
493  foreach ($this->errors as $table => $error_arr) {
494  foreach ($error_arr as $str) {
495 
496  $msg .= "Error for table $table : $str" . PHP_EOL;
497  }
498  }
499  }
500 
501  return $msg;
502  }

◆ GenerateLoanRecords()

LoanDataGenerator::GenerateLoanRecords ( )
protected

Call up the actual generator class and get busy.

Exceptions
Exception
Returns
string

Definition at line 207 of file LoanDataGenerator.php.

207  {
208 
209  return $this
210  ->ParseCsv()
211  ->SetDetailTemplate()
212  ->MapCsvToData()
213  ->ReturnResponse();
214  }

◆ GetLoanAppUserId()

LoanDataGenerator::GetLoanAppUserId ( )
protected

Get the user id from the record we just inserted (@TODO since we are now using native pg_methods, we should change our inserts to ' ... returning userid' so we don't need this exrta query)

Returns
int

Definition at line 425 of file LoanDataGenerator.php.

425  {
426 
427  $data = $this->DbLoanCreator->GetLastRecord('lnappuser', 'userid');
428 
429  if (isset($data['data']) && isset($data['data']['userid'])) {
430  return $data['data']['userid'];
431  }
432 
433  return 0;
434  }
GetLastRecord($table, $keyfield)

◆ InsertChallengeResponses()

LoanDataGenerator::InsertChallengeResponses (   $user_id)
protected

Insert three random challenge responses so we can test login process.

Parameters
int$user_id
Exceptions
Exception
Returns
$this

Definition at line 393 of file LoanDataGenerator.php.

393  {
394 
395  $rand_resp = $this->Mapper->MapRandomQuestionResponses($user_id, $this->test_credentials);
396  $response = $this->DbLoanCreator->AddSampleData($rand_resp);
397  $this->UpdateResponses($response);
398 
399  return $this;
400  }
AddSampleData($data=[])
UpdateResponses($response=[])

◆ InsertUser()

LoanDataGenerator::InsertUser (   $row)
protected

Insert the user data for the loan. Do first so we can get a user id. (@TODO since we are now using native pg_methods, we should change our inserts to ' ... returning userid'

Parameters
$row
Returns
$this

Definition at line 409 of file LoanDataGenerator.php.

409  {
410 
411  $user_map = $this->Mapper->MapUserData($row, $this->test_credentials);
412  $response = $this->DbLoanCreator->AddSampleData($user_map);
413 
414  $this->UpdateResponses($response);
415 
416  return $this;
417  }
AddSampleData($data=[])
UpdateResponses($response=[])

◆ InsertUserLoan()

LoanDataGenerator::InsertUserLoan (   $loan_id,
  $user_id,
  $row 
)
protected

Insert the loan data. (@TODO since we are now using native pg_methods, we should change our inserts to ' ... returning userid' so we don't need this exrta query)

Parameters
int$loan_id
int$user_id
array$row
Exceptions
Exception
Returns
$this

Definition at line 376 of file LoanDataGenerator.php.

376  {
377 
378  // Create a json map [csv key] => [formfield_[this schema field id]]
379  $data = $this->DbLoanCreator->GetList('lnappschemadetail', 'loanid', $loan_id);
380  $map_user = $this->Mapper->MapUserLoan($user_id, $loan_id, $row, $data);
381  $response = $this->DbLoanCreator->AddSampleData($map_user);
382  $this->UpdateResponses($response);
383 
384  return $this;
385  }
AddSampleData($data=[])
UpdateResponses($response=[])
GetList($table, $keyfield, $value)

◆ IsErrors()

LoanDataGenerator::IsErrors ( )
protected

Got errors?

Returns
bool

Definition at line 598 of file LoanDataGenerator.php.

598  {
599 
600  return count($this->errors) > 0;
601  }

◆ IsInputEmpty()

LoanDataGenerator::IsInputEmpty ( )
protected

Is $this->line property empty? (or zero)

Returns
bool

Definition at line 179 of file LoanDataGenerator.php.

179  {
180 
181  return empty($this->line);
182  }

◆ MapCsvToData()

LoanDataGenerator::MapCsvToData ( )
protected

For each line of the CSV, map the data to the tables, which is an unavoidable several steps. We must first get the user insert so we can have a user ID for the question records. Then we can move on to the loan, but there is no clear way to associate our CSV fields with loan data fields to create formname_[schema record id] for the JSON. This form key pattern is what makes the data load properly in the end user loan app.

Exceptions
Exception
Returns
$this

Definition at line 289 of file LoanDataGenerator.php.

289  {
290 
291  if ($this->IsErrors()) {
292  return $this;
293  }
294 
295  $count = 0;
296 
297  foreach ($this->csv_data as $index => $row) {
298 
299  $count++;
300  $loan_id = $this->CreateSchemaRecords($row);
301  $this->InsertUser($row);
302  $user_id = $this->GetLoanAppUserId();
303 
304  $this
305  ->InsertUserLoan($loan_id, $user_id, $row)
306  ->InsertChallengeResponses($user_id);
307 
308  // Should already be done at continueProcessing, double guard.
309  if ($count >= $this->num_records) {
310  break;
311  }
312  }
313 
314  return $this;
315  }

◆ ParseCsv()

LoanDataGenerator::ParseCsv ( )
protected

Use CsvToArray to parse the CSV file into an array.

Returns
$this

Definition at line 221 of file LoanDataGenerator.php.

221  {
222 
223  while ($this->ContinueProcessing()) {
224 
225  $this->csv_line_count++;
226 
227  $data = $this->CsvToArray->ParseData();
228 
229  if (isset($data['errors']) && (count($data['errors']) > 0)) {
230  // Mock a table handle to canonicalize error output.
231  $this->AddNewErrors('csv', $data['errors']);
232  return $this;
233  }
234 
235  $this->csv_data[] = $data['data'];
236  }
237 
238  return $this;
239  }
AddNewErrors($table, $error_arr)

◆ ProcessInput()

LoanDataGenerator::ProcessInput ( )
protected

Process the command line input.

Returns
string

Definition at line 151 of file LoanDataGenerator.php.

151  {
152 
153  if ($this->line === 'q') {
154  return "Exiting program . . ." . PHP_EOL;
155  exit;
156  }
157 
158  if (! $this->UserHasSelectedVolume()) {
159  $this->num_records = $this->line;
160 
161 
162  } elseif (! $this->UserHasConfirmed()) {
163 
164  if ($this->line === 'n') {
165  $this->num_records = 0;
166  return '';
167  }
168 
169  $this->confirmed = $this->line === 'y';
170  }
171 
172  return '';
173  }

◆ ResetInputValues()

LoanDataGenerator::ResetInputValues ( )
protected

When done, flush these out to return to the prompt screen.

Returns
$this

Definition at line 508 of file LoanDataGenerator.php.

508  {
509 
510  $this->num_records = 0;
511  $this->confirmed = false;
512 
513  return $this;
514  }

◆ ReturnResponse()

LoanDataGenerator::ReturnResponse ( )
protected

Compose a response that should be everything that went right. Or wrong.

Returns
string

Definition at line 440 of file LoanDataGenerator.php.

440  {
441 
442  $plural = ($this->num_records == 1)? 'record' : 'records';
443 
444  $msg = "
445  Done creating {$this->num_records} loan $plural." .
446  PHP_EOL .
447  "
448  To test users, select returning non member at the loan app
449  screen and log in. All passwords and security questions are
450  answered with '{$this->test_credentials}.'" .
451  PHP_EOL .
452  $this->SuccessResponses() .
453  $this->ErrorResponses() .
454  PHP_EOL .
455  "
456  When you are done testing, run 'php cleanup-test-records.php'
457  to remove all test records. Type 'q' to exit." .
458  PHP_EOL;
459 
460  $this->ResetInputValues();
461 
462  return $msg;
463  }

◆ Set()

LoanDataGenerator::Set (   $prop,
  $object 
)
protected

A typical setter, sets internal properties (CsvToArray, DbLoanCreator, LoanGeneratorMapper) Usually setters are public, don't think we'll need it

Parameters
stringprop property identifier
mixedproperty to set
Returns
$this

Definition at line 99 of file LoanDataGenerator.php.

99  {
100 
101  $this->{$prop} = $object;
102 
103  return $this;
104  }

◆ SetConsoleMessage()

LoanDataGenerator::SetConsoleMessage ( )
protected

Set prompt messages. These prompts should come in this order.

Exceptions
Exception
Returns
string

Definition at line 124 of file LoanDataGenerator.php.

124  {
125 
126  if ($this->UserHasConfirmed()) {
127  // Set loose the Kraken.
128  echo "One moment, generating loan data . . . " . PHP_EOL;
129  return $this->GenerateLoanRecords();
130  }
131 
132  if ($this->UserHasSelectedVolume()) {
133  return "
134  This program will now generate {$this->num_records} loan records.
135  Enter y to continue, n to cancel:" . PHP_EOL;
136  }
137 
138  if ($this->IsInputEmpty() || ! $this->UserHasSelectedVolume()) {
139  return "
140  Enter q to quit at any time.
141  Enter the number of loan records you would like to generate:" . PHP_EOL;
142  }
143 
144  return null;
145  }

◆ SetDetailTemplate()

LoanDataGenerator::SetDetailTemplate ( )
protected

Get detail record ID 8 to be applied to every loan we insert. All we will need to do is swap out loan id and unset detail id for each record, see Mapper->createDetailTemplateMap()

Returns
$this

Definition at line 267 of file LoanDataGenerator.php.

267  {
268 
269  if ($this->IsErrors()) {
270  return $this;
271  }
272 
273  $data = $this->DbLoanCreator->getList('lnappschemadetail', 'loanid', $this->schema_template);
274 
275  $this->detail_template = $data['data'];
276 
277  return $this;
278  }

◆ SetInputLine()

LoanDataGenerator::SetInputLine ( )
protected

Set the line entered from input, if there is one.

Returns
$this

Definition at line 110 of file LoanDataGenerator.php.

110  {
111 
112  $handle = fopen ("php://stdin","r");
113  $this->line = trim(fgets($handle));
114  fclose($handle);
115 
116  return $this;
117  }

◆ SuccessResponses()

LoanDataGenerator::SuccessResponses ( )
protected

Helper for returnResponse(). walk the successful insert counts and create sensible message strings of them.

Returns
string|null

Definition at line 470 of file LoanDataGenerator.php.

470  {
471 
472  $msg = null;
473  if (count($this->success_counts) > 0) {
474  foreach ($this->success_counts as $table => $count) {
475 
476  $plural = ($count == 1)? 'record' : 'records';
477  $msg .= "$count $plural inserted for table $table." . PHP_EOL;
478  }
479  }
480 
481  return $msg;
482  }

◆ UpdateResponseErrors()

LoanDataGenerator::UpdateResponseErrors (   $response = [])
protected

Set any response errors in our response array. Walking through and adding in the event other errors are added before or after.

Parameters
array$response
Returns
$this

Definition at line 562 of file LoanDataGenerator.php.

562  {
563 
564  if ((isset($response['errors']) && count($response['errors']) > 0)) {
565  foreach ($response['errors'] as $table => $error_arr) {
566 
567  $this->AddNewErrors($table, $error_arr);
568  }
569  }
570 
571  return $this;
572  }
AddNewErrors($table, $error_arr)

◆ UpdateResponses()

LoanDataGenerator::UpdateResponses (   $response = [])
protected

Update the error array and success counts.

Parameters
array$response
Returns
$this

Definition at line 521 of file LoanDataGenerator.php.

521  {
522 
523  if (count($response) == 0) {
524  return $this;
525  }
526 
527  $this
528  ->UpdateSuccessCounts($response)
529  ->UpdateResponseErrors($response);
530 
531  return $this;
532  }

◆ UpdateSuccessCounts()

LoanDataGenerator::UpdateSuccessCounts (   $response = [])
protected

Update the success counts, to be used in a plain-English message in ReturnResponse().

Parameters
array$response
Returns
$this

Definition at line 539 of file LoanDataGenerator.php.

539  {
540 
541  if ((isset($response['success']) && count($response['success']) > 0)) {
542  foreach ($response['success'] as $table => $count) {
543 
544  if (isset($this->success_counts[$table])) {
545  $this->success_counts[$table] += $response['success'][$table];
546  continue;
547  }
548 
549  $this->success_counts[$table] = $response['success'][$table];
550  }
551  }
552 
553  return $this;
554  }

◆ UserHasConfirmed()

LoanDataGenerator::UserHasConfirmed ( )
protected

Has the user confirmed their choice?

Returns
bool

Definition at line 197 of file LoanDataGenerator.php.

197  {
198 
199  return $this->confirmed === true;
200  }

◆ UserHasSelectedVolume()

LoanDataGenerator::UserHasSelectedVolume ( )
protected

Has the user selected how many records to create?

Returns
bool

Definition at line 188 of file LoanDataGenerator.php.

188  {
189 
190  return $this->num_records > 0;
191  }

◆ WaitForInput()

LoanDataGenerator::WaitForInput ( )

Entry point. Wait for user input once started.

Exceptions
Exception
Returns
string

Definition at line 79 of file LoanDataGenerator.php.

79  {
80 
81  while ($this->line !== 'q') {
82 
83  echo $this->SetConsoleMessage();
84  echo $this
85  ->SetInputLine()
86  ->ProcessInput();
87  }
88 
89  return '';
90  }

The documentation for this class was generated from the following file: