Passed
Push — master ( 598445...fef947 )
by Andreas
11:12
created

midcom_services_indexer_client   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 93
ccs 14
cts 21
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A add_query() 0 3 1
A index() 0 3 1
A new_document() 0 7 1
A reindex() 0 6 4
1
<?php
2
/**
3
 * @package midcom.services
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Indexer client base class
11
 *
12
 * @package midcom.services
13
 */
14
abstract class midcom_services_indexer_client
15
{
16
    /**
17
     * The topic we're working on
18
     *
19
     * @var midcom_db_topic
20
     */
21
    protected $_topic;
22
23
    /**
24
     * The NAP node corresponding to the topic
25
     */
26
    protected array $_node;
27
28
    /**
29
     * The L10n DB for the topic's component
30
     */
31
    protected midcom_services_i18n_l10n $_l10n;
32
33
    private midcom_services_indexer $_indexer;
34
35
    /**
36
     * The queries we will work on. Each entry consists of a querybuilder
37
     * instance and a datamanager to render the results, and is indexed by name
38
     */
39
    private array $_queries = [];
40
41
    /**
42
     * @param midcom_db_topic $topic The current topic
43
     */
44 4
    public function __construct($topic, midcom_services_indexer $indexer = null)
45
    {
46 4
        $this->_topic = $topic;
47 4
        $this->_l10n = midcom::get()->i18n->get_l10n($topic->component);
48 4
        $this->_indexer = $indexer ?? midcom::get()->indexer;
49
50 4
        $nav = new midcom_helper_nav();
51 4
        $this->_node = $nav->get_node($topic->id);
52
    }
53
54
    /**
55
     * Index a single object from DM
56
     *
57
     * @param mixed $object The object instance to use
58
     */
59 4
    public function index($object) : bool
60
    {
61 4
        return $this->_indexer->index($this->new_document($object));
62
    }
63
64
    /**
65
     * @param midcom\datamanager\datamanager $dm datamanager (or schemadb in dm2)
66
     */
67
    public function add_query(string $name, midcom_core_querybuilder $qb, $dm)
68
    {
69
        $this->_queries[$name] = [$qb, $dm];
70
    }
71
72
    public function reindex()
73
    {
74
        foreach ($this->_queries as $name => [$qb, $dm]) {
75
            if ($results = $qb->execute()) {
76
                if ($documents = $this->process_results($name, $results, $dm)) {
77
                    $this->_indexer->index($documents);
78
                }
79
            }
80
        }
81
    }
82
83
    /**
84
     * @param mixed $object
85
     */
86 4
    public function new_document($object) : midcom_services_indexer_document
87
    {
88 4
        $document = $this->create_document($object);
89 4
        $document->topic_guid = $this->_topic->guid;
90 4
        $document->component = $this->_topic->component;
91 4
        $document->topic_url = $this->_node[MIDCOM_NAV_FULLURL];
92 4
        return $document;
93
    }
94
95
    /**
96
     * @param midcom_core_dbaobject[] $results
97
     * @param midcom\datamanager\datamanager $dm datamanager (or schemadb in dm2)
98
     * @return midcom_services_indexer_document[]
99
     */
100
    abstract public function process_results(string $name, array $results, $dm) : array;
101
102
    /**
103
     * @param mixed $object
104
     * @return midcom_services_indexer_document
105
     */
106
    abstract public function create_document($object) : midcom_services_indexer_document;
107
}
108