Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
adapter_factory
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 get_with_options
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\storage;
15
16use phpbb\config\config;
17use phpbb\di\service_collection;
18use phpbb\storage\exception\storage_exception;
19
20class adapter_factory
21{
22    /**
23     * @var config
24     */
25    protected $config;
26
27    /**
28     * @var service_collection
29     */
30    protected $adapters;
31
32    /**
33     * @var service_collection
34     */
35    protected $providers;
36
37    /**
38     * Constructor
39     *
40     * @param config                $config
41     * @param service_collection    $adapters
42     * @param service_collection    $providers
43     */
44    public function __construct(config $config, service_collection $adapters, service_collection $providers)
45    {
46        $this->config = $config;
47        $this->adapters = $adapters;
48        $this->providers = $providers;
49    }
50
51    /**
52     * Obtains a configured adapters for a given storage
53     *
54     * @param string    $storage_name
55     *
56     * @return mixed
57     */
58    public function get(string $storage_name): mixed
59    {
60        $provider_class = $this->config['storage\\' . $storage_name . '\\provider'];
61        $provider = $this->providers->get_by_class($provider_class);
62
63        $options = [];
64        foreach (array_keys($provider->get_options()) as $definition)
65        {
66            /** @psalm-suppress InvalidArrayOffset */
67            $options[$definition] = $this->config['storage\\' . $storage_name . '\\config\\' . $definition];
68        }
69
70        return $this->get_with_options($storage_name, $options);
71    }
72
73    /**
74     * Obtains a configured adapters for a given storage with custom options
75     *
76     * @param string    $storage_name
77     * @param array        $options
78     *
79     * @return mixed
80     */
81    public function get_with_options(string $storage_name, array $options): mixed
82    {
83        $provider_class = $this->config['storage\\' . $storage_name . '\\provider'];
84        $provider = $this->providers->get_by_class($provider_class);
85
86        if (!$provider->is_available())
87        {
88            throw new storage_exception('STORAGE_ADAPTER_NOT_AVAILABLE');
89        }
90
91        $adapter = $this->adapters->get_by_class($provider->get_adapter_class());
92        $adapter->configure($options);
93
94        return $adapter;
95    }
96}