Test Failed
Push — master ( 4d9b91...c71310 )
by Julien
06:31
created

ServiceProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 78.95%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 42
dl 0
loc 76
ccs 30
cts 38
cp 0.7895
rs 10
c 2
b 2
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 70 9
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Provider\Database;
13
14
use Phalcon\Db\Adapter\Pdo\AbstractPdo;
15
use Phalcon\Db\Adapter\Pdo\Mysql;
16
use Phalcon\Di\DiInterface;
17
use Phalcon\Events\ManagerInterface;
18
use Zemit\Config\ConfigInterface;
19
use Zemit\Db\Events\Logger;
20
use Zemit\Db\Events\Profiler;
21
use Zemit\Provider\AbstractServiceProvider;
22
23
class ServiceProvider extends AbstractServiceProvider
24
{
25
    protected ?string $driverName = null;
26
    protected string $serviceName = 'db';
27
    protected static bool $attachedEvents = false;
28
    
29 112
    public function register(DiInterface $di): void
30
    {
31 112
        $driverName = $this->driverName;
32 112
        $di->setShared($this->getName(), function () use ($di, $driverName) {
33 6
            $config = $di->get('config');
34 6
            assert($config instanceof ConfigInterface);
35
    
36
            // database config
37 6
            $databaseConfig = $config->pathToArray('database') ?? [];
38 6
            $driverName ??= $databaseConfig['default'] ?? 'mysql';
39
            
40
            // specified driver name
41 6
            if (isset($databaseConfig['drivers'][$driverName])) {
42 6
                if (!is_array($databaseConfig['drivers'][$driverName])) {
43
                    throw new \InvalidArgumentException('Database driver option `' . $driverName . '` must be an array');
44
                }
45
                
46 6
                $driverOptions = array_filter($databaseConfig['drivers'][$driverName], fn($value) => $value !== null);
47
                
48 6
                if (isset($driverOptions['extends'])) {
49
                    if (is_string($driverOptions['extends'])) {
50
                        $driverOptions['extends'] = explode(',', $driverOptions['extends']);
51
                    }
52
                    if (is_array($driverOptions['extends'])) {
53
                        foreach ($driverOptions['extends'] as $extend) {
54
                            $driverOptions = array_merge($databaseConfig['drivers'][trim($extend)] ?? [], $driverOptions);
55
                        }
56
                    }
57
                }
58
            }
59
            
60
            // default driver name
61
            else {
62
                $defaultDriverName = $databaseConfig['default'] ?? 'mysql';
63
                $driverOptions = $databaseConfig['drivers'][$defaultDriverName] ?? [];
64
            }
65
            
66
            // unset unsupported parameters
67 6
            unset($driverOptions['extends']);
68 6
            unset($driverOptions['enable']);
69
            
70
            // dialect
71 6
            if (!empty($driverOptions['dialectClass'])) {
72 6
                $dialectClass = $driverOptions['dialectClass'];
73 6
                assert(class_exists($dialectClass));
74 6
                $driverOptions['dialectClass'] = new $dialectClass();
75
            }
76
    
77
            // adapter
78 6
            $adapter = $driverOptions['adapter'] ?? Mysql::class;
79 6
            assert(class_exists($adapter));
80 6
            unset($driverOptions['adapter']);
81
82
            // connection
83 6
            $connection = new $adapter($driverOptions);
84 6
            assert($connection instanceof AbstractPdo);
85
            
86
            // attach events
87 6
            $eventsManager = $di->get('eventsManager');
88 6
            assert($eventsManager instanceof ManagerInterface);
89
            
90 6
            if (!self::$attachedEvents) {
91 1
                $eventsManager->attach('db', new Logger());
92 1
                $eventsManager->attach('db', new Profiler());
93 1
                self::$attachedEvents = true;
94
            }
95
            
96 6
            $connection->setEventsManager($eventsManager);
97
            
98 6
            return $connection;
99 112
        });
100
    }
101
}
102