GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#96)
by Ross
15:04 queued 12:23
created

EntityManagerFactory::getDbConnectionInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\EntityManager;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver;
7
use Doctrine\ORM\Configuration;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Doctrine\ORM\Tools;
11
use EdmondsCommerce\DoctrineStaticMeta\ConfigInterface;
12
use EdmondsCommerce\DoctrineStaticMeta\Exception\ConfigException;
13
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
14
15
/**
16
 * Class EntityManagerFactory
17
 *
18
 * @package EdmondsCommerce\DoctrineStaticMeta\EntityManager
19
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
20
 */
21
class EntityManagerFactory implements EntityManagerFactoryInterface
22 71
{
23
24 71
    /**
25 71
     * @var Cache
26
     */
27
    protected $cache;
28
29
    public function __construct(Cache $cache)
30
    {
31
        $this->cache = $cache;
32
    }
33
34
    /**
35
     * This is used to create a new instance of the entity manager. Each of the steps involved need to take place,
36
     * however you may wish to make modifications to individual ones. There for the method is final, but the child
37
     * steps are public and can be overwritten if you extend the class
38 71
     *
39
     * @param ConfigInterface $config
40
     *
41 71
     * @return EntityManagerInterface
42 71
     * @throws DoctrineStaticMetaException
43 71
     *
44 71
     */
45 71
    final public function getEntityManager(ConfigInterface $config): EntityManagerInterface
46 71
    {
47
        try {
48 71
            $this->validateConfig($config);
49
            $dbParams       = $this->getDbConnectionInfo($config);
50
            $doctrineConfig = $this->getDoctrineConfig($config);
51
            $this->addDsmParamsToConfig($doctrineConfig, $config);
52
            $entityManager = $this->createEntityManager($dbParams, $doctrineConfig);
53
            $this->setDebuggingInfo($config, $entityManager);
54
55
            return $entityManager;
56
        } catch (\Exception $e) {
57
            $message = 'Exception in ' . __METHOD__ . ': ' . $e->getMessage();
58
59
            throw new DoctrineStaticMetaException($message, $e->getCode(), $e);
60
        }
61
    }
62
63
    /**
64 71
     * Both the Entities path and Proxy directory need to be set and the directories exist for DSM to work. This
65
     * carries out that validation. Override this if you need any other configuration to be set.
66 71
     *
67 71
     * @param ConfigInterface $config
68
     *
69
     * @throws ConfigException
70
     */
71
    public function validateConfig(ConfigInterface $config): void
72
    {
73
        $dbEntitiesPath = $config->get(ConfigInterface::PARAM_ENTITIES_PATH);
74
        if (!is_dir($dbEntitiesPath)) {
75 71
            throw new ConfigException(
76 71
                ' ERROR  Entities path does not exist. '
77
                . 'You need to either fix the config or create the entities path directory, '
78
                . 'currently configured as: [' . $dbEntitiesPath . '] '
79
            );
80
        }
81
82
        $proxyDir = $config->get(ConfigInterface::PARAM_DOCTRINE_PROXY_DIR);
83 71
        if (!is_dir($proxyDir)) {
84
            throw new ConfigException(
85
                'ERROR  ProxyDir does not exist. '
86
                . 'You need to either fix the config or create the directory, '
87
                . 'currently configured as: [' . $proxyDir . '] '
88
            );
89
        }
90
    }
91
92
    /**
93
     * This is used to get the connection information for doctrine. By default this pulls the information out of the
94 71
     * configuration interface, however if you connection information is in a different format you can override this
95
     * method and set it
96 71
     *
97 71
     * @param ConfigInterface $config
98 71
     *
99 71
     * @return array
100
     */
101
    public function getDbConnectionInfo(ConfigInterface $config): array
102 71
    {
103 71
        $dbUser = $config->get(ConfigInterface::PARAM_DB_USER);
104 71
        $dbPass = $config->get(ConfigInterface::PARAM_DB_PASS);
105 71
        $dbHost = $config->get(ConfigInterface::PARAM_DB_HOST);
106 71
        $dbName = $config->get(ConfigInterface::PARAM_DB_NAME);
107 71
108
        return [
109
            'driver'   => 'pdo_mysql',
110
            'user'     => $dbUser,
111
            'password' => $dbPass,
112
            'dbname'   => $dbName,
113
            'host'     => $dbHost,
114
            'charset'  => 'utf8mb4',
115
        ];
116
    }
117
118
    /**
119
     * This is used to get the doctrine configuration object. By default this creates a new instance, however if you
120 71
     * already have an object configured you can override this method and inject it
121
     *
122 71
     * @param ConfigInterface $config
123 71
     *
124 71
     * @return Configuration
125
     * @SuppressWarnings(PHPMD.StaticAccess)
126 71
     */
127
    public function getDoctrineConfig(ConfigInterface $config): Configuration
128
    {
129
        $isDevMode = (bool)$config->get(ConfigInterface::PARAM_DEVMODE);
130
        $proxyDir  = $config->get(ConfigInterface::PARAM_DOCTRINE_PROXY_DIR);
131
        $cache     = $isDevMode ? null : $this->cache;
132
133
        return Tools\Setup::createConfiguration($isDevMode, $proxyDir, $cache);
134
    }
135
136 71
    /**
137
     * This is used to add the DSM specific configuration to doctrine Configuration object. You shouldn't need to
138 71
     * override this, but if you do you can
139 71
     *
140 71
     * @param Configuration   $doctrineConfig
141 71
     * @param ConfigInterface $config
142 71
     */
143 71
    public function addDsmParamsToConfig(Configuration $doctrineConfig, ConfigInterface $config): void
144
    {
145
        $paths          = $this->getPathInformation($config);
146
        $namingStrategy = $config->get(ConfigInterface::PARAM_DOCTRINE_NAMING_STRATEGY);
147
        $driver         = new StaticPHPDriver($paths);
148
        $doctrineConfig->setMetadataDriverImpl($driver);
149
        $doctrineConfig->setNamingStrategy($namingStrategy);
150
    }
151
152
    /**
153 71
     * By default we only return a single path to the entities, however if you have your entities in multiple places you
154
     * can override this method and include them all
155 71
     *
156
     * @param ConfigInterface $config
157
     *
158 71
     * @return array
159
     */
160
    public function getPathInformation(ConfigInterface $config): array
161
    {
162
        $dbEntitiesPath = $config->get(ConfigInterface::PARAM_ENTITIES_PATH);
163
164
        return [
165
            $dbEntitiesPath,
166
        ];
167
    }
168
169
    /**
170
     * This is used to create the Entity manager. You can override this if there are any calls you wish to make on it
171
     * after it has been created
172
     *
173 71
     * @param array         $dbParams
174
     * @param Configuration $doctrineConfig
175 71
     *
176
     * @return EntityManagerInterface
177
     * @throws \Doctrine\ORM\ORMException
178
     * @SuppressWarnings(PHPMD.StaticAccess)
179
     */
180
    public function createEntityManager(array $dbParams, Configuration $doctrineConfig): EntityManagerInterface
181
    {
182
        return EntityManager::create($dbParams, $doctrineConfig);
183
    }
184
185
    /**
186
     * This is used to set any debugging information, by default it enables MySql logging and clears the log table.
187 71
     * Override this method if there is anything else that you need to do
188
     *
189 71
     * @param ConfigInterface          $config
190 71
     * @param EntityManagerInterface   $entityManager
191 71
     *
192
     * @throws \Doctrine\DBAL\DBALException
193
     */
194
    public function setDebuggingInfo(ConfigInterface $config, EntityManagerInterface $entityManager): void
195
    {
196
        $isDbDebug = (bool)$config->get(ConfigInterface::PARAM_DB_DEBUG);
197
        if (false === $isDbDebug) {
198
            return;
199
        }
200
        $connection = $entityManager->getConnection();
201
        $connection->query(
202
            "
203
                set global general_log = 1;
204
                set global log_output = 'table';
205
                truncate general_log;
206
                "
207
        );
208
    }
209
}
210