Conditions | 4 |
Paths | 4 |
Total Lines | 58 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 10 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | public static function getEntityManager(): EntityManagerInterface |
||
32 | { |
||
33 | if (self::$instance instanceof EntityManager) { |
||
34 | return self::$instance; |
||
35 | } |
||
36 | |||
37 | if ( |
||
38 | !isset( |
||
39 | $_ENV['DATABASE_NAME'], |
||
40 | $_ENV['DATABASE_USER'], |
||
41 | $_ENV['DATABASE_PASSWD'], |
||
42 | $_ENV['ENTITY_DIR'], |
||
43 | $_ENV['SERVER_VERSION'], |
||
44 | ) |
||
45 | ) { |
||
46 | fwrite(STDERR, 'Faltan variables de entorno por definir' . PHP_EOL); |
||
47 | exit(1); |
||
48 | } |
||
49 | |||
50 | // Cargar configuración de la conexión. |
||
51 | $dbParams = [ |
||
52 | 'host' => $_ENV['DATABASE_HOST'] ?? '127.0.0.1', |
||
53 | 'port' => $_ENV['DATABASE_PORT'] ?? 3306, |
||
54 | 'dbname' => $_ENV['DATABASE_NAME'], |
||
55 | 'user' => $_ENV['DATABASE_USER'], |
||
56 | 'password' => $_ENV['DATABASE_PASSWD'], |
||
57 | 'driver' => $_ENV['DATABASE_DRIVER'] ?? 'pdo_mysql', |
||
58 | 'serverVersion' => $_ENV['SERVER_VERSION'], |
||
59 | 'charset' => $_ENV['DATABASE_CHARSET'] ?? 'UTF8', |
||
60 | ]; |
||
61 | |||
62 | $entityDir = dirname(__DIR__, 2) . '/' . $_ENV['ENTITY_DIR']; |
||
63 | // $queryCache = new PhpFilesAdapter('doctrine_queries'); |
||
64 | // $metadataCache = new PhpFilesAdapter('doctrine_metadata'); |
||
65 | // $resultsCache = new PhpFilesAdapter('doctrine_results'); |
||
66 | $config = ORMSetup::createAttributeMetadataConfiguration( |
||
67 | paths: [ $entityDir ], // paths to mapped entities |
||
68 | isDevMode: true, // developper mode |
||
69 | proxyDir: (string) ini_get('sys_temp_dir') // Proxy dir |
||
70 | ); |
||
71 | // $config->setQueryCache($queryCache); |
||
72 | // $config->setMetadataCache($metadataCache); |
||
73 | // $config->setResultCache($resultsCache); |
||
74 | $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED); |
||
75 | |||
76 | // configuring the database connection |
||
77 | /** @var Connection $connection */ |
||
78 | $connection = DriverManager::getConnection($dbParams, $config); |
||
79 | |||
80 | try { |
||
81 | self::$instance = new EntityManager($connection, $config); |
||
82 | } catch (Throwable $e) { |
||
83 | $msg = sprintf('ERROR (%d): %s', $e->getCode(), $e->getMessage()); |
||
84 | fwrite(STDERR, $msg . PHP_EOL); |
||
85 | exit(1); |
||
86 | } |
||
87 | |||
88 | return self::$instance; |
||
89 | } |
||
107 |