Completed
Push — develop ( 49f02e...6bacbf )
by Neomerx
07:17
created

DataContainerConfigurator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 50
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B configureContainer() 0 42 5
1
<?php namespace Limoncello\Application\Packages\Data;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Doctrine\DBAL\Connection;
20
use Doctrine\DBAL\DriverManager;
21
use Limoncello\Application\Data\ModelSchemaInfo;
22
use Limoncello\Contracts\Application\ContainerConfiguratorInterface;
23
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
24
use Limoncello\Contracts\Data\ModelSchemaInfoInterface;
25
use Limoncello\Contracts\Settings\SettingsProviderInterface;
26
use Psr\Container\ContainerInterface as PsrContainerInterface;
27
28
/**
29
 * @package Limoncello\Application
30
 */
31
class DataContainerConfigurator implements ContainerConfiguratorInterface
32
{
33
    /**
34
     * @inheritdoc
35
     *
36
     * @SuppressWarnings(PHPMD.StaticAccess)
37
     */
38 1
    public static function configureContainer(LimoncelloContainerInterface $container): void
39
    {
40 1
        $container[ModelSchemaInfoInterface::class] =
41 1
            function (PsrContainerInterface $container): ModelSchemaInfoInterface {
42 1
                $settings = $container->get(SettingsProviderInterface::class)->get(DataSettings::class);
43 1
                $data     = $settings[DataSettings::KEY_MODELS_SCHEMA_INFO];
44
45 1
                return (new ModelSchemaInfo())->setData($data);
46
            };
47
48 1
        $container[Connection::class] = function (PsrContainerInterface $container): Connection {
49 1
            $settings = $container->get(SettingsProviderInterface::class)->get(DoctrineSettings::class);
50 1
            $params   = array_filter([
51 1
                'driver'   => $settings[DoctrineSettings::KEY_DRIVER] ?? null,
52
                'dbname'   => $settings[DoctrineSettings::KEY_DATABASE_NAME] ?? null,
53
                'user'     => $settings[DoctrineSettings::KEY_USER_NAME] ?? null,
54
                'password' => $settings[DoctrineSettings::KEY_PASSWORD] ?? null,
55
                'host'     => $settings[DoctrineSettings::KEY_HOST] ?? null,
56
                'port'     => $settings[DoctrineSettings::KEY_PORT] ?? null,
57
                'url'      => $settings[DoctrineSettings::KEY_URL] ?? null,
58
                'memory'   => $settings[DoctrineSettings::KEY_MEMORY] ?? null,
59
                'path'     => $settings[DoctrineSettings::KEY_PATH] ?? null,
60 1
                'charset'  => $settings[DoctrineSettings::KEY_CHARSET] ?? 'UTF8',
61 1
            ], function ($value) {
62 1
                return $value !== null;
63 1
            });
64 1
            $extra    = $settings[DoctrineSettings::KEY_EXTRA] ?? [];
65
66 1
            $connection = DriverManager::getConnection($params + $extra);
67
68 1
            if (array_key_exists(DoctrineSettings::KEY_EXEC, $settings) === true &&
69 1
                is_array($toExec = $settings[DoctrineSettings::KEY_EXEC]) === true &&
70 1
                empty($toExec) === false
71
            ) {
72 1
                foreach ($toExec as $statement) {
73 1
                    $connection->exec($statement);
74
                }
75
            }
76
77 1
            return $connection;
78
        };
79
    }
80
}
81