Conditions | 1 |
Paths | 1 |
Total Lines | 2 |
Code Lines | 0 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Metric | Value |
---|---|
cc | 1 |
eloc | 0 |
c | 1 |
b | 0 |
f | 0 |
nc | 1 |
nop | 0 |
dl | 0 |
loc | 2 |
rs | 10 |
1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | |||
3 | /** |
||
4 | * src/Utility/DoctrineConnector.php |
||
0 ignored issues
–
show
|
|||
5 | * |
||
6 | * @license https://opensource.org/licenses/MIT MIT License |
||
0 ignored issues
–
show
|
|||
7 | * @link https://miw.etsisi.upm.es/ ETS de Ingeniería de Sistemas Informáticos |
||
0 ignored issues
–
show
|
|||
8 | */ |
||
0 ignored issues
–
show
|
|||
9 | |||
10 | namespace MiW\DemoDoctrine\Utility; |
||
11 | |||
12 | use Doctrine\Common\Proxy\AbstractProxyFactory; |
||
13 | use Doctrine\ORM\EntityManager; |
||
14 | use Doctrine\ORM\EntityManagerInterface; |
||
15 | use Doctrine\ORM\ORMSetup; |
||
16 | use Exception; |
||
17 | use Symfony\Component\Cache\Adapter\PhpFilesAdapter; |
||
18 | use Throwable; |
||
19 | |||
20 | /** |
||
21 | * Class DoctrineConnector |
||
22 | */ |
||
0 ignored issues
–
show
|
|||
23 | final class DoctrineConnector |
||
24 | { |
||
0 ignored issues
–
show
|
|||
25 | private static EntityManager|null $instance = null; |
||
0 ignored issues
–
show
|
|||
26 | |||
27 | /** |
||
28 | * Generate the Entity Manager |
||
29 | * |
||
30 | * @return EntityManagerInterface|null |
||
31 | */ |
||
32 | public static function getEntityManager(): ?EntityManagerInterface |
||
0 ignored issues
–
show
|
|||
33 | { |
||
0 ignored issues
–
show
|
|||
34 | if (self::$instance instanceof EntityManager) { |
||
35 | return self::$instance; |
||
36 | } |
||
37 | |||
38 | if ( |
||
0 ignored issues
–
show
|
|||
39 | !isset( |
||
0 ignored issues
–
show
|
|||
40 | $_ENV['DATABASE_NAME'], |
||
0 ignored issues
–
show
|
|||
41 | $_ENV['DATABASE_USER'], |
||
0 ignored issues
–
show
|
|||
42 | $_ENV['DATABASE_PASSWD'], |
||
0 ignored issues
–
show
|
|||
43 | $_ENV['ENTITY_DIR'] |
||
0 ignored issues
–
show
|
|||
44 | ) |
||
0 ignored issues
–
show
|
|||
45 | ) { |
||
46 | fwrite(STDERR, 'Faltan variables de entorno por definir' . PHP_EOL); |
||
0 ignored issues
–
show
|
|||
47 | exit(1); |
||
0 ignored issues
–
show
|
|||
48 | } |
||
49 | |||
50 | // Cargar configuración de la conexión. |
||
51 | $dbParams = [ |
||
0 ignored issues
–
show
|
|||
52 | 'host' => $_ENV['DATABASE_HOST'] ?? '127.0.0.1', |
||
0 ignored issues
–
show
|
|||
53 | 'port' => $_ENV['DATABASE_PORT'] ?? 3306, |
||
0 ignored issues
–
show
|
|||
54 | 'dbname' => $_ENV['DATABASE_NAME'], |
||
0 ignored issues
–
show
|
|||
55 | 'user' => $_ENV['DATABASE_USER'], |
||
0 ignored issues
–
show
|
|||
56 | 'password' => $_ENV['DATABASE_PASSWD'], |
||
0 ignored issues
–
show
|
|||
57 | 'driver' => $_ENV['DATABASE_DRIVER'] ?? 'pdo_mysql', |
||
0 ignored issues
–
show
|
|||
58 | 'charset' => $_ENV['DATABASE_CHARSET'] ?? 'UTF8', |
||
0 ignored issues
–
show
|
|||
59 | ]; |
||
0 ignored issues
–
show
|
|||
60 | |||
61 | $entityDir = dirname(__DIR__, 2) . '/' . $_ENV['ENTITY_DIR']; |
||
0 ignored issues
–
show
|
|||
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( |
||
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. ![]() |
|||
67 | [ $entityDir ], // paths to mapped entities |
||
0 ignored issues
–
show
|
|||
68 | true, // developper mode |
||
0 ignored issues
–
show
|
|||
69 | ini_get('sys_temp_dir') // Proxy dir |
||
0 ignored issues
–
show
|
|||
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) { |
||
0 ignored issues
–
show
|
|||
76 | // $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); |
||
0 ignored issues
–
show
|
|||
77 | // } |
||
0 ignored issues
–
show
|
|||
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); |
||
0 ignored issues
–
show
|
|||
84 | exit(1); |
||
0 ignored issues
–
show
|
|||
85 | } |
||
86 | |||
87 | return $entityManager; |
||
88 | } |
||
0 ignored issues
–
show
|
|||
89 | |||
90 | protected function __construct() |
||
0 ignored issues
–
show
|
|||
91 | { |
||
0 ignored issues
–
show
|
|||
92 | } |
||
0 ignored issues
–
show
|
|||
93 | |||
94 | protected function __clone() |
||
0 ignored issues
–
show
|
|||
95 | { |
||
0 ignored issues
–
show
|
|||
96 | } |
||
0 ignored issues
–
show
|
|||
97 | |||
98 | /** |
||
0 ignored issues
–
show
|
|||
99 | * @throws Exception |
||
0 ignored issues
–
show
|
|||
100 | */ |
||
0 ignored issues
–
show
|
|||
101 | public function __wakeup() |
||
102 | { |
||
0 ignored issues
–
show
|
|||
103 | throw new Exception("Cannot unserialize a Singleton."); |
||
0 ignored issues
–
show
The string literal
Cannot unserialize a Singleton. does not require double quotes, as per coding-style, please use single quotes.
PHP provides two ways to mark string literals. Either with single quotes String literals in single quotes on the other hand are evaluated very literally and the only two
characters that needs escaping in the literal are the single quote itself ( Double quoted string literals may contain other variables or more complex escape sequences. <?php
$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";
print $doubleQuoted;
will print an indented: If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear. For more information on PHP string literals and available escape sequences see the PHP core documentation. ![]() |
|||
104 | } |
||
0 ignored issues
–
show
|
|||
105 | } |
||
0 ignored issues
–
show
|
|||
106 |