2017-04-30 21:18:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Eloquent;
|
2017-05-02 20:55:36 +02:00
|
|
|
use Cache;
|
2017-04-30 21:18:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ExpenseCategory.
|
|
|
|
*/
|
|
|
|
class LookupModel extends Eloquent
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public $timestamps = false;
|
|
|
|
|
2017-04-30 21:52:05 +02:00
|
|
|
public function lookupAccount()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\LookupAccount');
|
|
|
|
}
|
2017-04-30 21:18:17 +02:00
|
|
|
|
|
|
|
public static function createNew($accountKey, $data)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
|
|
|
$lookupAccount = LookupAccount::whereAccountKey($accountKey)->first();
|
|
|
|
|
|
|
|
if ($lookupAccount) {
|
|
|
|
$data['lookup_account_id'] = $lookupAccount->id;
|
|
|
|
} else {
|
|
|
|
abort('Lookup account not found for ' . $accountKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static::create($data);
|
|
|
|
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
}
|
2017-04-30 21:52:05 +02:00
|
|
|
|
2017-05-01 11:29:45 +02:00
|
|
|
public static function deleteWhere($where)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
|
|
|
static::where($where)->delete();
|
|
|
|
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:50:10 +02:00
|
|
|
public static function setServerByField($field, $value)
|
|
|
|
{
|
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = get_called_class();
|
|
|
|
$className = str_replace('Lookup', '', $className);
|
|
|
|
$key = sprintf('server:%s:%s:%s', $className, $field, $value);
|
|
|
|
|
|
|
|
// check if we've cached this lookup
|
2017-05-02 20:55:36 +02:00
|
|
|
if (env('MULTI_DB_CACHE_ENABLED') && $server = Cache::get($key)) {
|
2017-05-10 10:38:21 +02:00
|
|
|
static::setDbServer($server);
|
2017-05-01 08:50:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current = config('database.default');
|
|
|
|
config(['database.default' => DB_NINJA_LOOKUP]);
|
|
|
|
|
2017-05-01 15:06:34 +02:00
|
|
|
if ($value && $lookupModel = static::where($field, '=', $value)->first()) {
|
2017-05-01 10:13:15 +02:00
|
|
|
$entity = new $className();
|
2017-05-01 15:06:34 +02:00
|
|
|
$server = $lookupModel->getDbServer();
|
|
|
|
|
2017-05-10 10:38:21 +02:00
|
|
|
static::setDbServer($server);
|
2017-05-01 08:50:10 +02:00
|
|
|
|
2017-05-01 10:13:15 +02:00
|
|
|
// check entity is found on the server
|
2017-05-10 17:18:48 +02:00
|
|
|
if ($field === 'oauth_user_key') {
|
|
|
|
list($providerId, $oauthId) = explode('-', $value);
|
|
|
|
$isFound = $entity::where('oauth_provider_id', '=', $providerId)
|
|
|
|
->where('oauth_user_id', '=', $oauthId)->first();
|
|
|
|
} else {
|
|
|
|
$isFound = $entity::where($field, '=', $value)->first();
|
|
|
|
}
|
|
|
|
if (! $isFound) {
|
2017-05-01 08:50:10 +02:00
|
|
|
abort("Looked up {$className} not found: {$field} => {$value}");
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:55:36 +02:00
|
|
|
Cache::put($key, $server, 120);
|
2017-05-01 08:50:10 +02:00
|
|
|
} else {
|
|
|
|
config(['database.default' => $current]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 10:38:21 +02:00
|
|
|
protected static function setDbServer($server)
|
2017-05-01 08:50:10 +02:00
|
|
|
{
|
2017-05-02 11:09:01 +02:00
|
|
|
if (! env('MULTI_DB_ENABLED')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:50:10 +02:00
|
|
|
config(['database.default' => $server]);
|
|
|
|
}
|
|
|
|
|
2017-04-30 21:52:05 +02:00
|
|
|
public function getDbServer()
|
|
|
|
{
|
|
|
|
return $this->lookupAccount->lookupCompany->dbServer->name;
|
|
|
|
}
|
2017-04-30 21:18:17 +02:00
|
|
|
}
|