Skip to content

Instantly share code, notes, and snippets.

@rreynier
Last active August 29, 2015 14:25
Show Gist options
  • Save rreynier/1801df267a9bd391c6ea to your computer and use it in GitHub Desktop.
Save rreynier/1801df267a9bd391c6ea to your computer and use it in GitHub Desktop.
<?php
// API
$newCompanySettings = new NewCompanySettings();
$newCompanySettings->populateFromApiRequest($request);
$company = new Company($db);
$result = $company->create(newCompanySettings);
// GUI Controller
$newCompanySettings = new NewCompanySettings();
$newCompanySettings->populateFromForm($post);
$company = new Company($db);
$result = $company->create($newCompanySettings);
// Object representation of Company at a high level
class Company() {
$adminDb;
$companyRecord;
$companyDatabase;
construct($databaseConnection , Integer $companyId = false)
$this->adminDb = $databaseConnection;
if ($companyId)
$this->companyRecord = retrieveCompanyRecord($companyId);
$this->companyDatabase = new CompanyDatabase($companyRecord);
create(NewCompanySettings $newCompanySettings)
$this->companyRecord = new CompanyRecord();
$this->companyRecord->setName($newCompanySettings->getName())
...
// persist $companyRecord
$this->companyDatabase = new CompanyDatabase();
$this->companyDatabase->create($this->companyRecord, $newCompanySettings);
$this->copmanyRecord->setCompanyId($companyDatabase->getCompanyId());
// persit $companyRecord once again
retrieveCompanyRecord
...
return CompanyRecord
resetDates(Date);
$this->companyDatabase->resetDates(Date);
// add a log somewhere that dates were reset
// update company record updatedAt timestamp
// etc.
return true|false
}
// Object representation of the database_location record
class CompanyRecord {
// getters and setters
}
// Object representation of the company database
class CompanyDatabase {
$databaseConnection;
construct(CompanyRecord $companyRecord)
$this->companyDatabaseConnection = getCompanyDatabaseConnection($companyRecord)
create(NewCompanySettings $newCompanySettings) // private method
// identify id range and actual id for company
$companyId = $this->getNextAvailabeCompanyId($region);
// identify the database name for company
$dbName = $this->getNextAvailableDatabaseName($newCompanySettings);
// create database with base sql
$this->createBaseDatabase();
// customize our database
$this->customize($newCompanySettings);
// do some things if this is a trial instance
if($newCompanySettings->getIsTrial())
$seedDate = $this->getTrialSeedData();
$this->seed($seedData);
if($newCompanySettings->getResetDates())
$this->resetDates($newCopmanySettings->getRestDate());
getCompanyDatabaseConnection($companyId)
return $companyDatabaseConnection;
customize(NewCompanySettings $newCompanySettings)
...
return true|false;
seed($seedData)
...
return true|false;
getNextAvailabeCompanyId() // private method
...
return int;
getNextAvailableDatabaseName() // private method
...
return string;
createBaseDatabase() // private method
...
return true|false;
...
}
class NewCompanySettings {
$isTrial = true|false;
$trialStartDate = null|DateTime;
$resetDates = true|false;
$resetDate = null|DateTime;
$users = [User, User, ...];
populateFromForm($post);
// parse post
return isValid();
populateFromApiRequest($apiRequest);
// parse api request
return isValid();
isValid();
getters/setters();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment