Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
33.33% |
1 / 3 |
CRAP | |
84.21% |
16 / 19 |
| DbConnection | |
0.00% |
0 / 1 |
|
33.33% |
1 / 3 |
10.39 | |
84.21% |
16 / 19 |
| create | |
0.00% |
0 / 1 |
7.18 | |
84.62% |
11 / 13 |
|||
| mergeFromFile | |
0.00% |
0 / 1 |
2.03 | |
80.00% |
4 / 5 |
|||
| getMongoDnsString | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\MicroService\DbConnection; |
| 4 | |
| 5 | use Illuminate\Contracts\Foundation\CachesConfiguration; |
| 6 | |
| 7 | class DbConnection |
| 8 | { |
| 9 | public static function create(array $configDb = []) |
| 10 | { |
| 11 | $app = app(); |
| 12 | if (! ($app instanceof CachesConfiguration && $app->configurationIsCached())) { |
| 13 | $config = $app->make('config'); |
| 14 | $db = $config->get('database'); |
| 15 | |
| 16 | if (!empty($configDb)) { |
| 17 | if (!isset($db['connections'])) { |
| 18 | $db['connections'] = []; |
| 19 | } |
| 20 | |
| 21 | foreach ($configDb as $name => $connection) { |
| 22 | if (isset( $db['connections'][$name])) { |
| 23 | throw new \Exception("Configuration already exist ! ($name)"); |
| 24 | } |
| 25 | |
| 26 | $db['connections'][$name] = $connection; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | $config->set('database', $db); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public static function mergeFromFile(string $file) |
| 35 | { |
| 36 | if (!file_exists($file)) { |
| 37 | throw new \Exception("File config connection : '$file' does not exist"); |
| 38 | } |
| 39 | $connections = require $file; |
| 40 | self::create($connections); |
| 41 | } |
| 42 | |
| 43 | public static function getMongoDnsString() |
| 44 | { |
| 45 | return 'mongodb://' . env('MONGO_HOST', 'localhost') . ':' . env('MONGO_PORT', 27017) . '/' . env('MONGO_DATABASE'); |
| 46 | } |
| 47 | } |