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 (#74)
by Ross
11:11
created

EntityManagerFactory::setDebuggingInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

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