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 (#93)
by joseph
17:38 queued 30s
created

EntityManagerFactory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Test Coverage

Coverage 81.67%

Importance

Changes 0
Metric Value
wmc 16
eloc 64
dl 0
loc 196
ccs 49
cts 60
cp 0.8167
rs 10
c 0
b 0
f 0

10 Methods

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