Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DriverConfigurationFeeder
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 feed
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 feedConfigWithService
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2020 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 */
20
21declare(strict_types=1);
22
23namespace oat\generis\persistence;
24
25use oat\oatbox\service\ConfigurableService;
26
27class DriverConfigurationFeeder extends ConfigurableService
28{
29    public const SERVICE_ID = 'generis/DriverConfigurationFeeder';
30    public const OPTION_DRIVER_OPTIONS = 'driverOptions';
31
32    public function feed(array $config): array
33    {
34        if (empty($config['connection']['driverClass'])) {
35            return $config;
36        }
37
38        $driverClass = $config['connection']['driverClass'];
39
40        $options = $this->getOption(self::OPTION_DRIVER_OPTIONS, []);
41
42        if (empty($options[$driverClass]) || empty($config['connection']['driverOptions'])) {
43            return $config;
44        }
45
46        foreach ($options[$driverClass]['driverOptions'] as $option) {
47            $config = $this->feedConfigWithService($config, $option);
48        }
49
50        return $config;
51    }
52
53    private function feedConfigWithService(array $config, string $option): array
54    {
55        if (empty($config['connection']['driverOptions'][$option])) {
56            return $config;
57        }
58
59        if (is_object($config['connection']['driverOptions'][$option])) {
60            return $config;
61        }
62
63        $config['connection']['driverOptions'][$option] = $this->getServiceLocator()
64            ->get($config['connection']['driverOptions'][$option]);
65
66        return $config;
67    }
68}