Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
43.48% covered (danger)
43.48%
10 / 23
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractBackendFactory
43.48% covered (danger)
43.48%
10 / 23
75.00% covered (warning)
75.00%
3 / 4
19.56
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setup
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createHttpClient
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 createConnectorCache
13.33% covered (danger)
13.33%
2 / 15
0.00% covered (danger)
0.00%
0 / 1
14.42
1<?php
2
3/**
4 * Abstract factory for backends.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2021.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Search
25 * @author   Ere Maijala <ere.maijala@helsinki.fi>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\Search\Factory;
31
32use Laminas\Cache\Storage\StorageInterface;
33use Laminas\Config\Config;
34use Laminas\ServiceManager\Factory\FactoryInterface;
35use Psr\Container\ContainerInterface;
36
37/**
38 * Abstract factory for backends.
39 *
40 * @category VuFind
41 * @package  Search
42 * @author   David Maus <maus@hab.de>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org Main Site
45 */
46abstract class AbstractBackendFactory implements FactoryInterface
47{
48    /**
49     * Service container.
50     *
51     * @var ContainerInterface
52     */
53    protected $serviceLocator;
54
55    /**
56     * Constructor
57     */
58    public function __construct()
59    {
60    }
61
62    /**
63     * Initialize the factory
64     *
65     * @param ContainerInterface $sm Service manager
66     *
67     * @return void
68     */
69    public function setup(ContainerInterface $sm)
70    {
71        $this->serviceLocator = $sm;
72    }
73
74    /**
75     * Create HTTP Client
76     *
77     * @param int    $timeout Request timeout
78     * @param array  $options Other options
79     * @param string $url     Request URL (needed for proper local address check when
80     * the client is being proxified)
81     *
82     * @return \Laminas\Http\Client
83     */
84    protected function createHttpClient(
85        ?int $timeout = null,
86        array $options = [],
87        string $url = null
88    ): \Laminas\Http\Client {
89        $client = $this->serviceLocator->get(\VuFindHttp\HttpService::class)
90            ->createClient($url);
91        if (null !== $timeout) {
92            $options['timeout'] = $timeout;
93        }
94        $client->setOptions($options);
95        return $client;
96    }
97
98    /**
99     * Create cache for the connector if enabled in configuration
100     *
101     * @param Config $searchConfig Search configuration
102     *
103     * @return ?StorageInterface
104     */
105    protected function createConnectorCache(Config $searchConfig): ?StorageInterface
106    {
107        if (empty($searchConfig->SearchCache->adapter)) {
108            return null;
109        }
110        $cacheConfig = $searchConfig->SearchCache->toArray();
111        $options = $cacheConfig['options'] ?? [];
112        if (empty($options['namespace'])) {
113            $options['namespace'] = 'Index';
114        }
115        if (empty($options['ttl'])) {
116            $options['ttl'] = 300;
117        }
118        $settings = [
119            'adapter' => $cacheConfig['adapter'],
120            'options' => $options,
121        ];
122        return $this->serviceLocator
123            ->get(\Laminas\Cache\Service\StorageAdapterFactory::class)
124            ->createFromArrayConfiguration($settings);
125    }
126}