Conditions | 4 |
Paths | 4 |
Total Lines | 56 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
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 |
||
32 | public static function getEntityManager(): ?EntityManagerInterface |
||
33 | { |
||
34 | if (self::$instance instanceof EntityManager) { |
||
35 | return self::$instance; |
||
36 | } |
||
37 | |||
38 | if ( |
||
39 | !isset( |
||
40 | $_ENV['DATABASE_NAME'], |
||
41 | $_ENV['DATABASE_USER'], |
||
42 | $_ENV['DATABASE_PASSWD'], |
||
43 | $_ENV['ENTITY_DIR'] |
||
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 | 'charset' => $_ENV['DATABASE_CHARSET'] ?? 'UTF8', |
||
59 | ]; |
||
60 | |||
61 | $entityDir = dirname(__DIR__, 2) . '/' . $_ENV['ENTITY_DIR']; |
||
62 | // $debug = $_ENV['DEBUG'] ?? false; |
||
63 | $queryCache = new PhpFilesAdapter('doctrine_queries'); |
||
64 | // $metadataCache = new PhpFilesAdapter('doctrine_metadata'); |
||
65 | $resultsCache = new PhpFilesAdapter('doctrine_results'); |
||
66 | $config = ORMSetup::createAttributeMetadataConfiguration( |
||
67 | [ $entityDir ], // paths to mapped entities |
||
68 | true, // developper mode |
||
69 | ini_get('sys_temp_dir') // Proxy dir |
||
70 | ); |
||
71 | $config->setQueryCache($queryCache); |
||
72 | // $config->setMetadataCache($metadataCache); |
||
73 | $config->setResultCache($resultsCache); |
||
74 | $config->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED); |
||
75 | // if ($debug) { |
||
76 | // $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); |
||
77 | // } |
||
78 | |||
79 | try { |
||
80 | $entityManager = EntityManager::create($dbParams, $config); |
||
81 | } catch (Throwable $e) { |
||
82 | $msg = sprintf('ERROR (%d): %s', $e->getCode(), $e->getMessage()); |
||
83 | fwrite(STDERR, $msg . PHP_EOL); |
||
84 | exit(1); |
||
85 | } |
||
86 | |||
87 | return $entityManager; |
||
88 | } |
||
106 |