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
Push — master ( 749633...2fce5c )
by joseph
17:28 queued 14:47
created

EntityManagerFactory::validateConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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