Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
search_backend_factory
85.71% covered (warning)
85.71%
6 / 7
66.67% covered (warning)
66.67%
2 / 3
4.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get_active
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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\search;
15
16use phpbb\config\config;
17use phpbb\di\exception\service_not_found_exception;
18use phpbb\di\service_collection;
19use phpbb\search\backend\search_backend_interface;
20use phpbb\search\exception\no_search_backend_found_exception;
21
22class search_backend_factory
23{
24    /**
25     * @var config
26     */
27    protected $config;
28
29    /**
30     * @var service_collection
31     */
32    protected $search_backends;
33
34    /**
35     * Constructor
36     *
37     * @param config $config
38     * @param service_collection $search_backends
39     */
40    public function __construct(config $config, service_collection $search_backends)
41    {
42        $this->config = $config;
43        $this->search_backends = $search_backends;
44    }
45
46    /**
47     * Obtains a specified search backend
48     *
49     * @param string $class
50     *
51     * @throws no_search_backend_found_exception
52     *
53     * @return search_backend_interface
54     */
55    public function get(string $class): search_backend_interface
56    {
57        try
58        {
59            $search = $this->search_backends->get_by_class($class);
60        }
61        catch (service_not_found_exception $e)
62        {
63            throw new no_search_backend_found_exception('SEARCH_BACKEND_NOT_FOUND', [], $e);
64        }
65
66        return $search;
67    }
68
69    /**
70     * Obtains active search backend
71     *
72     * @throws no_search_backend_found_exception
73     *
74     * @return search_backend_interface
75     */
76    public function get_active(): search_backend_interface
77    {
78        return $this->get($this->config['search_type']);
79    }
80}