Failed Conditions
Pull Request — master (#6777)
by Yannick
24:02
created

Setup::createConfiguration()   F

Complexity

Conditions 16
Paths 296

Size

Total Lines 48
Code Lines 33

Duplication

Lines 16
Ratio 33.33 %

Code Coverage

Tests 15
CRAP Score 57.555

Importance

Changes 0
Metric Value
dl 16
loc 48
ccs 15
cts 33
cp 0.4545
rs 3.7109
c 0
b 0
f 0
cc 16
eloc 33
nc 296
nop 3
crap 57.555

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools;
21
22
use Doctrine\Common\ClassLoader;
23
use Doctrine\Common\Cache\Cache;
24
use Doctrine\Common\Cache\CacheProvider;
25
use Doctrine\Common\Cache\ArrayCache;
26
use Doctrine\ORM\Configuration;
27
use Doctrine\ORM\Mapping\Driver\XmlDriver;
28
use Doctrine\ORM\Mapping\Driver\YamlDriver;
29
30
/**
31
 * Convenience class for setting up Doctrine from different installations and configurations.
32
 *
33
 * @author Benjamin Eberlei <[email protected]>
34
 */
35
class Setup
36
{
37
    /**
38
     * Use this method to register all autoloads for a downloaded Doctrine library.
39
     * Pick the directory the library was uncompressed into.
40
     *
41
     * @param string $directory
42
     *
43
     * @return void
44
     */
45 1
    public static function registerAutoloadDirectory($directory)
46
    {
47 1
        if (!class_exists('Doctrine\Common\ClassLoader', false)) {
48
            require_once $directory . "/Doctrine/Common/ClassLoader.php";
49
        }
50
51 1
        $loader = new ClassLoader("Doctrine", $directory);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\ClassLoader has been deprecated with message: the ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
52 1
        $loader->register();
53
54 1
        $loader = new ClassLoader("Symfony\Component", $directory . "/Doctrine");
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\ClassLoader has been deprecated with message: the ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
55 1
        $loader->register();
56 1
    }
57
58
    /**
59
     * Creates a configuration with an annotation metadata driver.
60
     *
61
     * @param array   $paths
62
     * @param boolean $isDevMode
63
     * @param string  $proxyDir
64
     * @param Cache   $cache
65
     * @param bool    $useSimpleAnnotationReader
66
     *
67
     * @return Configuration
68
     */
69 3 View Code Duplication
    public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null, $useSimpleAnnotationReader = true)
70
    {
71 3
        $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
72 3
        $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));
73
74 3
        return $config;
75
    }
76
77
    /**
78
     * Creates a configuration with a xml metadata driver.
79
     *
80
     * @param array   $paths
81
     * @param boolean $isDevMode
82
     * @param string  $proxyDir
83
     * @param Cache   $cache
84
     *
85
     * @return Configuration
86
     */
87 1 View Code Duplication
    public static function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
88
    {
89 1
        $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
90 1
        $config->setMetadataDriverImpl(new XmlDriver($paths));
91
92 1
        return $config;
93
    }
94
95
    /**
96
     * Creates a configuration with a yaml metadata driver.
97
     *
98
     * @param array   $paths
99
     * @param boolean $isDevMode
100
     * @param string  $proxyDir
101
     * @param Cache   $cache
102
     *
103
     * @return Configuration
104
     */
105 1 View Code Duplication
    public static function createYAMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, Cache $cache = null)
106
    {
107 1
        $config = self::createConfiguration($isDevMode, $proxyDir, $cache);
108 1
        $config->setMetadataDriverImpl(new YamlDriver($paths));
109
110 1
        return $config;
111
    }
112
113
    /**
114
     * Creates a configuration without a metadata driver.
115
     *
116
     * @param bool   $isDevMode
117
     * @param string $proxyDir
118
     * @param Cache  $cache
119
     *
120
     * @return Configuration
121
     */
122 6
    public static function createConfiguration($isDevMode = false, $proxyDir = null, Cache $cache = null)
123
    {
124 6
        $proxyDir = $proxyDir ?: sys_get_temp_dir();
125
126 6
        if ($isDevMode === false && $cache === null) {
127
            if (extension_loaded('apc')) {
128
                $cache = new \Doctrine\Common\Cache\ApcCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ApcCache has been deprecated with message: since version 1.6, use ApcuCache instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
129
            }
130
            if ($cache === null && ini_get('xcache.cacher')) {
131
                $cache = new \Doctrine\Common\Cache\XcacheCache();
132
            }
133 View Code Duplication
            if ($cache === null && extension_loaded('memcache')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
                $memcache = new \Memcache();
135
                $r = @$memcache->connect('127.0.0.1');
136
                if ($r !== false) {
137
                    $cache = new \Doctrine\Common\Cache\MemcacheCache();
138
                    $cache->setMemcache($memcache);
139
                }
140
            }
141 View Code Duplication
            if ($cache === null && extension_loaded('redis')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
                $redis = new \Redis();
143
                $r = @$redis->connect('127.0.0.1');
144
                if ($r !== false) {
145
                    $cache = new \Doctrine\Common\Cache\RedisCache();
146
                    $cache->setRedis($redis);
147
                }
148
            }
149
            if ($cache === null) {
150
                $cache = new ArrayCache();
151
            }
152 6
        } elseif ($cache === null) {
153 4
            $cache = new ArrayCache();
154
        }
155
156 6
        if ($cache instanceof CacheProvider) {
157 5
            $cache->setNamespace("dc2_" . md5($proxyDir) . "_"); // to avoid collisions
158
        }
159
160 6
        $config = new Configuration();
161 6
        $config->setMetadataCacheImpl($cache);
162 6
        $config->setQueryCacheImpl($cache);
163 6
        $config->setResultCacheImpl($cache);
164 6
        $config->setProxyDir($proxyDir);
165 6
        $config->setProxyNamespace('DoctrineProxies');
166 6
        $config->setAutoGenerateProxyClasses($isDevMode);
167
168 6
        return $config;
169
    }
170
}
171