Completed
Push — master ( 131e79...afdc39 )
by Andreas
34:52 queued 12:29
created

members_to_fields()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 6
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.services
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * This is a base class which is targeted at MidCOM content object indexing. It should
11
 * be used whenever MidCOM documents are indexed, either directly or as a base class.
12
 *
13
 * It will take an arbitrary Midgard Object, for which Metadata must be available.
14
 * The document class will then load the metadata information out of the database
15
 * and populate all metadata fields of the document from there.
16
 *
17
 * If you want to index datamanager driven objects, you should instead look at
18
 * the datamanager's document class.
19
 *
20
 * The GUID of the object being referred is used as a RI.
21
 *
22
 * The documents type is "midcom".
23
 *
24
 * @package midcom.services
25
 * @see midcom_services_indexer
26
 * @see midcom_helper_metadata
27
 */
28
class midcom_services_indexer_document_midcom extends midcom_services_indexer_document
29
{
30
    /**
31
     * The metadata instance attached to the object to be indexed.
32
     *
33
     * @var midcom_helper_metadata
34
     */
35
    protected $_metadata;
36
37
    /**
38
     * The constructor initializes the content object, loads the metadata object
39
     * and populates the metadata fields accordingly.
40
     *
41
     * The source member is automatically populated with the GUID of the document,
42
     * the RI is set to it as well. The URL is set to an on-site permalink.
43
     *
44
     * @param mixed $object The content object to load, passed to the metadata constructor.
45
     * @see midcom_helper_metadata
46
     */
47 6
    public function __construct($object)
48
    {
49 6
        parent::__construct();
50
51 6
        if (!midcom::get()->config->get('indexer_backend')) {
52 6
            return;
53
        }
54
55
        $this->_set_type('midcom');
56
57
        if (is_a($object, midcom_helper_metadata::class)) {
58
            $this->_metadata = $object;
59
        } else {
60
            $this->_metadata = midcom_helper_metadata::retrieve($object);
61
            if ($this->_metadata == false) {
0 ignored issues
show
introduced by
The condition $this->_metadata == false is always false.
Loading history...
62
                debug_add('document_midcom: Failed to retrieve a Metadata object, aborting.');
63
                return;
64
            }
65
        }
66
67
        $this->source = $this->_metadata->object->guid;
68
        $this->lang = midcom::get()->i18n->get_current_language();
69
        // Add language code to RI as well so that different language versions of the object have unique identifiers
70
        $this->RI = "{$this->source}_{$this->lang}";
71
        $this->document_url = midcom::get()->permalinks->create_permalink($this->source);
72
73
        $this->_process_metadata();
74
    }
75
76
    /**
77
     * Processes the information contained in the metadata instance.
78
     */
79
    private function _process_metadata()
80
    {
81
        $this->read_metadata_from_object($this->_metadata->object);
0 ignored issues
show
Bug introduced by
$this->_metadata->object of type midcom_core_dbaobject is incompatible with the type midgard\portable\api\mgdobject expected by parameter $object of midcom_services_indexer_..._metadata_from_object(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
        $this->read_metadata_from_object(/** @scrutinizer ignore-type */ $this->_metadata->object);
Loading history...
82
        $metadata = $this->read_metadata();
83
        foreach ($metadata as $key => $value) {
84
            /**
85
             * @see parent::read_metadata_from_object()
86
             */
87
            if (!in_array($key, ['revised', 'revisor', 'created', 'creator'])) {
88
                if (in_array($key, ['keywords', 'tags'])) {
89
                    $this->content .= $value . "\n";
90
                }
91
                $this->add_text("META_{$key}", $value);
92
            }
93
        }
94
    }
95
96
    /**
97
     * Usually, documents are processed in batches, and constructing the dm for each
98
     * document is pretty wasteful, so we keep the instance around and reuse it
99
     */
100
    private function read_metadata() : array
101
    {
102
        static $meta_dm;
103
        if ($meta_dm === null) {
104
            $meta_dm = $this->_metadata->get_datamanager();
105
        }
106
        return $meta_dm->set_storage($this->_metadata->object)->get_content_html();
107
    }
108
109
    /**
110
     * This will translate all member variables into appropriate
111
     * field records, missing topic data is auto-detected
112
     */
113
    public function members_to_fields()
114
    {
115
        if (   empty($this->topic_guid)
116
            || empty($this->topic_url)
117
            || empty($this->component)) {
118
            //if one of those is missing, we override all three to ensure consistency
119
            $this->process_topic();
120
        }
121
122
        parent::members_to_fields();
123
    }
124
}
125