Passed
Pull Request — master (#2144)
by Alan
03:14
created

DoctrineMongoDbOdmSetup   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createAnnotationMetadataConfiguration() 0 6 1
A createCacheConfiguration() 0 17 3
A createYAMLMetadataConfiguration() 0 6 1
A createXMLMetadataConfiguration() 0 6 1
A createConfiguration() 0 16 3
A createCacheInstance() 0 35 6
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Test;
15
16
use Doctrine\Common\Cache\ArrayCache;
17
use Doctrine\Common\Cache\Cache;
18
use Doctrine\Common\Cache\CacheProvider;
19
use Doctrine\ODM\MongoDB\Configuration;
20
use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver;
21
use Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver;
22
23
/**
24
 * Convenience class for setting up Doctrine from different installations and configurations.
25
 *
26
 * @author Benjamin Eberlei <[email protected]>
27
 * @author Alan Poulain <[email protected]>
28
 */
29
class DoctrineMongoDbOdmSetup
30
{
31
    /**
32
     * Creates a configuration with an annotation metadata driver.
33
     */
34
    public static function createAnnotationMetadataConfiguration(array $paths, bool $isDevMode = false, string $proxyDir = null, string $hydratorDir = null, Cache $cache = null): Configuration
35
    {
36
        $config = self::createConfiguration($isDevMode, $proxyDir, $hydratorDir, $cache);
37
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths));
38
39
        return $config;
40
    }
41
42
    /**
43
     * Creates a configuration with a xml metadata driver.
44
     */
45
    public static function createXMLMetadataConfiguration(array $paths, bool $isDevMode = false, string $proxyDir = null, string $hydratorDir = null, Cache $cache = null): Configuration
46
    {
47
        $config = self::createConfiguration($isDevMode, $proxyDir, $hydratorDir, $cache);
48
        $config->setMetadataDriverImpl(new XmlDriver($paths));
49
50
        return $config;
51
    }
52
53
    /**
54
     * Creates a configuration with a yaml metadata driver.
55
     */
56
    public static function createYAMLMetadataConfiguration(array $paths, bool $isDevMode = false, string $proxyDir = null, string $hydratorDir = null, Cache $cache = null): Configuration
57
    {
58
        $config = self::createConfiguration($isDevMode, $proxyDir, $hydratorDir, $cache);
59
        $config->setMetadataDriverImpl(new YamlDriver($paths));
60
61
        return $config;
62
    }
63
64
    /**
65
     * Creates a configuration without a metadata driver.
66
     */
67
    public static function createConfiguration(bool $isDevMode = false, string $proxyDir = null, string $hydratorDir = null, Cache $cache = null): Configuration
68
    {
69
        $proxyDir = $proxyDir ?: sys_get_temp_dir();
70
        $hydratorDir = $hydratorDir ?: sys_get_temp_dir();
71
72
        $cache = self::createCacheConfiguration($isDevMode, $proxyDir, $hydratorDir, $cache);
73
74
        $config = new Configuration();
75
        $config->setMetadataCacheImpl($cache);
76
        $config->setProxyDir($proxyDir);
77
        $config->setHydratorDir($hydratorDir);
78
        $config->setProxyNamespace('DoctrineProxies');
79
        $config->setHydratorNamespace('DoctrineHydrators');
80
        $config->setAutoGenerateProxyClasses($isDevMode);
81
82
        return $config;
83
    }
84
85
    private static function createCacheConfiguration(bool $isDevMode, string $proxyDir, string $hydratorDir, ?Cache $cache): Cache
86
    {
87
        $cache = self::createCacheInstance($isDevMode, $cache);
88
89
        if (!$cache instanceof CacheProvider) {
90
            return $cache;
91
        }
92
93
        $namespace = $cache->getNamespace();
94
95
        if ('' !== $namespace) {
96
            $namespace .= ':';
97
        }
98
99
        $cache->setNamespace($namespace.'dc2_'.md5($proxyDir.$hydratorDir).'_'); // to avoid collisions
100
101
        return $cache;
102
    }
103
104
    private static function createCacheInstance(bool $isDevMode, ?Cache $cache): Cache
105
    {
106
        if (null !== $cache) {
107
            return $cache;
108
        }
109
110
        if (true === $isDevMode) {
111
            return new ArrayCache();
112
        }
113
114
        if (\extension_loaded('apcu')) {
115
            return new \Doctrine\Common\Cache\ApcuCache();
116
        }
117
118
        if (\extension_loaded('memcached')) {
119
            $memcached = new \Memcached();
120
            $memcached->addServer('127.0.0.1', 11211);
121
122
            $cache = new \Doctrine\Common\Cache\MemcachedCache();
123
            $cache->setMemcached($memcached);
124
125
            return $cache;
126
        }
127
128
        if (\extension_loaded('redis')) {
129
            $redis = new \Redis();
130
            $redis->connect('127.0.0.1');
131
132
            $cache = new \Doctrine\Common\Cache\RedisCache();
133
            $cache->setRedis($redis);
134
135
            return $cache;
136
        }
137
138
        return new ArrayCache();
139
    }
140
}
141