| Total Complexity | 9 |
| Total Lines | 62 |
| Duplicated Lines | 0 % |
| Coverage | 90.48% |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | trait HasDatabaseRecordsTrait |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var Connection |
||
| 17 | */ |
||
| 18 | protected $connection = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @return Connection |
||
| 22 | */ |
||
| 23 | 7 | public function getDB() |
|
| 24 | { |
||
| 25 | 7 | if ($this->connection == null) { |
|
| 26 | 3 | $this->initDB(); |
|
| 27 | } |
||
| 28 | |||
| 29 | 7 | $this->checkDB(); |
|
| 30 | |||
| 31 | 7 | return $this->connection; |
|
| 32 | } |
||
| 33 | |||
| 34 | 3 | protected function initDB() |
|
| 35 | { |
||
| 36 | 3 | $this->setDB($this->newDbConnection()); |
|
| 37 | 3 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @param Connection $connection |
||
| 41 | * @return $this |
||
| 42 | */ |
||
| 43 | 24 | public function setDB($connection) |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return Connection |
||
| 52 | */ |
||
| 53 | 3 | protected function newDbConnection() |
|
| 54 | { |
||
| 55 | 3 | if (function_exists('app')) { |
|
| 56 | return app('db.connection'); |
||
| 57 | } |
||
| 58 | 3 | return Container::getInstance()->get('db.connection'); |
|
| 59 | } |
||
| 60 | |||
| 61 | 7 | public function checkDB() |
|
| 62 | { |
||
| 63 | 7 | if (!$this->hasDB()) { |
|
| 64 | trigger_error("Database connection missing for [" . get_class($this) . "]", E_USER_ERROR); |
||
| 65 | } |
||
| 66 | 7 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @return bool |
||
| 70 | */ |
||
| 71 | 7 | public function hasDB() |
|
| 74 | } |
||
| 75 | } |
||
| 76 |