Passed
Push — master ( eec1ac...56c06a )
by Andreas
26:07
created

midcom_services_indexer_client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
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
     * @var array
27
     */
28
    protected $_node;
29
30
    /**
31
     * The L10n DB for the topic's component
32
     *
33
     * @var midcom_services_i18n_l10n
34
     */
35
    protected $_l10n;
36
37
    /**
38
     * The indexer service
39
     *
40
     * @var midcom_services_indexer
41
     */
42
    private $_indexer;
43
44
    /**
45
     * The queries we will work on. Each entry consists of a querybuilder
46
     * instance and a datamanager to render the results, and is indexed by name
47
     *
48
     * @var array
49
     */
50
    private $_queries = [];
51
52
    /**
53
     * Constructor
54
     *
55
     * @param midcom_db_topic $topic The current topic
56
     */
57 4
    public function __construct($topic, midcom_services_indexer $indexer = null)
58
    {
59 4
        $this->_topic = $topic;
60 4
        $this->_l10n = midcom::get()->i18n->get_l10n($topic->component);
61 4
        $this->_indexer = $indexer ?? midcom::get()->indexer;
62
63 4
        $nav = new midcom_helper_nav();
64 4
        $this->_node = $nav->get_node($topic->id);
65 4
    }
66
67
    /**
68
     * Index a single object from DM
69
     *
70
     * @param mixed $object The object instance to use
71
     */
72 4
    public function index($object) : bool
73
    {
74 4
        return $this->_indexer->index($this->new_document($object));
75
    }
76
77
    /**
78
     * @param midcom\datamanager\datamanager $dm datamanager (or schemadb in dm2)
79
     */
80
    public function add_query(string $name, midcom_core_querybuilder $qb, $dm)
81
    {
82
        $this->_queries[$name] = [$qb, $dm];
83
    }
84
85
    public function reindex()
86
    {
87
        foreach ($this->_queries as $name => [$qb, $dm]) {
88
            $results = $qb->execute();
89
            if (!empty($results)) {
90
                $documents = $this->process_results($name, $results, $dm);
91
                if (!empty($documents)) {
92
                    $this->_indexer->index($documents);
93
                }
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param mixed $object
100
     */
101 4
    public function new_document($object) : midcom_services_indexer_document
102
    {
103 4
        $document = $this->create_document($object);
104 4
        $document->topic_guid = $this->_topic->guid;
105 4
        $document->component = $this->_topic->component;
106 4
        $document->topic_url = $this->_node[MIDCOM_NAV_FULLURL];
107 4
        return $document;
108
    }
109
110
    /**
111
     *
112
     * @param midcom_core_dbaobject[] $results
113
     * @param midcom\datamanager\datamanager $dm datamanager (or schemadb in dm2)
114
     * @return midcom_services_indexer_document[]
115
     */
116
    abstract public function process_results(string $name, array $results, $dm) : array;
117
118
    /**
119
     *
120
     * @param mixed $object
121
     * @return midcom_services_indexer_document
122
     */
123
    abstract public function create_document($object) : midcom_services_indexer_document;
124
}
125