Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlenderBackendFactory
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __invoke
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
20
 attachEvents
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Factory for Blender backend.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) The National Library of Finland 2022.
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     http://vufind.org   Main Site
28 */
29
30namespace VuFind\Search\Factory;
31
32use Laminas\EventManager\EventManager;
33use Laminas\ServiceManager\Factory\FactoryInterface;
34use Psr\Container\ContainerInterface;
35use VuFindSearch\Backend\Blender\Backend;
36
37/**
38 * Factory for Blender backend.
39 *
40 * @category VuFind
41 * @package  Search
42 * @author   Ere Maijala <ere.maijala@helsinki.fi>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     http://vufind.org   Main Site
45 */
46class BlenderBackendFactory implements FactoryInterface
47{
48    /**
49     * Service manager.
50     *
51     * @var ContainerInterface
52     */
53    protected $container;
54
55    /**
56     * VuFind configuration reader
57     *
58     * @var \VuFind\Config\PluginManager
59     */
60    protected $config;
61
62    /**
63     * Search configuration file identifier.
64     *
65     * @var string
66     */
67    protected $searchConfig = 'Blender';
68
69    /**
70     * Facet configuration file identifier.
71     *
72     * @var string
73     */
74    protected $facetConfig = 'Blender';
75
76    /**
77     * Mappings YAML configuration file identifier.
78     *
79     * @var string
80     */
81    protected $mappingsConfig = 'BlenderMappings.yaml';
82
83    /**
84     * Create service
85     *
86     * @param ContainerInterface $sm      Service manager
87     * @param string             $name    Requested service name (unused)
88     * @param array              $options Extra options (unused)
89     *
90     * @return Backend
91     *
92     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
93     */
94    public function __invoke(ContainerInterface $sm, $name, array $options = null)
95    {
96        $this->container = $sm;
97        $this->config = $sm->get(\VuFind\Config\PluginManager::class);
98        $yamlReader = $sm->get(\VuFind\Config\YamlReader::class);
99        $blenderConfig = $this->config->get($this->searchConfig);
100        $backendConfig = $blenderConfig->Backends
101            ? $blenderConfig->Backends->toArray() : [];
102        if (!$backendConfig) {
103            throw new \Exception("No backends enabled in {$this->searchConfig}.ini");
104        }
105        $backends = [];
106        $backendManager = $sm->get(\VuFind\Search\BackendManager::class);
107        foreach (array_keys($backendConfig) as $backendId) {
108            $backends[$backendId] = $backendManager->get($backendId);
109        }
110        $blenderMappings = $yamlReader->get($this->mappingsConfig);
111        $backend = new Backend(
112            $backends,
113            $blenderConfig,
114            $blenderMappings,
115            new EventManager($sm->get('SharedEventManager'))
116        );
117        $this->attachEvents($backend);
118        return $backend;
119    }
120
121    /**
122     * Create Blender listeners.
123     *
124     * @param Backend $backend Backend
125     *
126     * @return void
127     */
128    protected function attachEvents(Backend $backend)
129    {
130        $manager = $this->container->get('SharedEventManager');
131
132        $manager->attach(
133            \VuFindSearch\Service::class,
134            \VuFindSearch\Service::EVENT_PRE,
135            [$backend, 'onSearchPre']
136        );
137        $manager->attach(
138            \VuFindSearch\Service::class,
139            \VuFindSearch\Service::EVENT_POST,
140            [$backend, 'onSearchPost']
141        );
142    }
143}