Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
20.83% |
5 / 24 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
DbUtils | |
20.83% |
5 / 24 |
|
0.00% |
0 / 3 |
39.75 | |
0.00% |
0 / 1 |
getDb | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
getEntityManager | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
4.84 | |||
fromEnv | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Olz\Utils; |
4 | |
5 | use Doctrine\ORM\EntityManager; |
6 | |
7 | class DbUtils { |
8 | use WithUtilsTrait; |
9 | |
10 | protected static ?\mysqli $db = null; |
11 | protected static ?EntityManager $entityManager = null; |
12 | |
13 | public function getDb(): \mysqli { |
14 | if (self::$db !== null) { |
15 | return self::$db; |
16 | } |
17 | |
18 | $db = new \mysqli( |
19 | $this->envUtils()->getMysqlServer(), |
20 | $this->envUtils()->getMysqlUsername(), |
21 | $this->envUtils()->getMysqlPassword(), |
22 | $this->envUtils()->getMysqlSchema() |
23 | ); |
24 | |
25 | if ($db->connect_error) { |
26 | throw new \Exception("MySQL Connect Error ({$db->connect_errno}) {$db->connect_error}"); |
27 | } |
28 | |
29 | $db->set_charset('utf8mb4'); |
30 | $db->query("SET NAMES utf8mb4"); |
31 | $db->query("SET time_zone = '+00:00';"); |
32 | |
33 | self::$db = $db; |
34 | return $db; |
35 | } |
36 | |
37 | public function getEntityManager(): EntityManager { |
38 | if (self::$entityManager !== null) { |
39 | return self::$entityManager; |
40 | } |
41 | |
42 | global $kernel, $entityManager; |
43 | |
44 | if ($kernel && !isset($entityManager)) { |
45 | $entityManager = $kernel->getContainer()->get('doctrine.orm.default_entity_manager'); |
46 | } |
47 | |
48 | $this->generalUtils()->checkNotNull($entityManager, "No entityManager"); |
49 | self::$entityManager = $entityManager; |
50 | return $entityManager; |
51 | } |
52 | |
53 | public static function fromEnv(): self { |
54 | return new self(); |
55 | } |
56 | } |