Completed
Push — master ( 13291d...928a65 )
by Tom
11s
created

DBALConnectionFactory::getOptionsClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 DoctrineORMModule\Service;
21
22
use Doctrine\DBAL\DriverManager;
23
use Doctrine\DBAL\Types\Type;
24
use DoctrineModule\Service\AbstractFactory;
25
use Interop\Container\ContainerInterface;
26
use Zend\ServiceManager\ServiceLocatorInterface;
27
28
/**
29
 * DBAL Connection ServiceManager factory
30
 *
31
 * @license MIT
32
 * @link    http://www.doctrine-project.org/
33
 * @author  Kyle Spraggs <[email protected]>
34
 */
35
class DBALConnectionFactory extends AbstractFactory
36
{
37
    /**
38
     * {@inheritDoc}
39
     *
40
     * @return \Doctrine\DBAL\Connection
41
     */
42 59
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
43
    {
44
        /** @var $options \DoctrineORMModule\Options\DBALConnection */
45 59
        $options = $this->getOptions($container, 'connection');
46 59
        $pdo     = $options->getPdo();
47
48 59
        if (is_string($pdo)) {
49
            $pdo = $container->get($pdo);
50
        }
51
52
        $params = array(
53 59
            'driverClass'  => $options->getDriverClass(),
54 59
            'wrapperClass' => $options->getWrapperClass(),
55 59
            'pdo'          => $pdo,
56 59
        );
57 59
        $params = array_merge($params, $options->getParams());
58
59 59
        $configuration = $container->get($options->getConfiguration());
60 59
        $eventManager  = $container->get($options->getEventManager());
61
62 59
        $connection = DriverManager::getConnection($params, $configuration, $eventManager);
63 59
        foreach ($options->getDoctrineTypeMappings() as $dbType => $doctrineType) {
64 2
            $connection->getDatabasePlatform()->registerDoctrineTypeMapping($dbType, $doctrineType);
65 59
        }
66
67 59
        foreach ($options->getDoctrineCommentedTypes() as $type) {
68 1
            $connection->getDatabasePlatform()->markDoctrineTypeCommented(Type::getType($type));
69 59
        }
70
71 59
        return $connection;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     * @return \Doctrine\DBAL\Connection
77
     */
78 59
    public function createService(ServiceLocatorInterface $container)
79
    {
80 59
        return $this($container, \Doctrine\DBAL\Connection::class);
81
    }
82
83
    /**
84
     * Get the class name of the options associated with this factory.
85
     *
86
     * @return string
87
     */
88 59
    public function getOptionsClass()
89
    {
90 59
        return 'DoctrineORMModule\Options\DBALConnection';
91
    }
92
}
93