Passed
Push — master ( 3a819d...c9b8a4 )
by Andreas
19:18
created

net_nemein_rss_parser_item::get_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package net.nemein.rss
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
use SimplePie\Item;
10
11
/**
12
 * Helper class for custom RSS handling
13
 *
14
 * @package net.nemein.rss
15
 */
16
class net_nemein_rss_parser_item extends Item
17
{
18
    private bool $_id_missing = true;
19
20
    private ?string $_local_guid = null;
21
22
    /**
23
     * @inheritdoc
24
     */
25 4
    public function get_title()
26
    {
27 4
        $title = parent::get_title();
28 4
        if (empty($title)) {
29 3
            $l10n = midcom::get()->i18n->get_l10n('net.nemein.rss');
30 3
            $title = $l10n->get('untitled');
31 3
            if ($description = $this->get_description()) {
32
                // Use 20 first characters from the description as title
33 2
                $title = mb_substr(strip_tags($this->_decode($description)), 0, 20) . '...';
34 1
            } elseif ($date = $this->get_date('U')) {
35
                // Use publication date as title
36
                $title = $l10n->get_formatter()->date($date);
37
            }
38
        }
39 4
        return $title;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 4
    public function get_description($description_only = false)
46
    {
47 4
        return $this->_decode(parent::get_description($description_only));
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 2
    public function get_content($content_only = false)
54
    {
55 2
        return $this->_decode(parent::get_content($content_only));
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 4
    public function get_link($key = 0, $rel = 'alternate')
62
    {
63 4
        $link = parent::get_link($key, $rel);
64 4
        if (   $rel !== 'alternate'
65 4
            || $key !== 0) {
66 1
            return $link;
67
        }
68
69 4
        if (empty($link)) {
70 4
            if (!$this->_id_missing) {
71
                $link = $this->get_id();
72
            } else {
73
                // No link or GUID defined
74
                // TODO: Generate a "link" using channel URL
75 4
                $link = '';
76
            }
77
        }
78 4
        return $link;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84 4
    public function get_id($hash = false, $fn = 'md5')
85
    {
86 4
        $guid = parent::get_id($hash, false);
87
88 4
        if (empty($guid)) {
89 2
            $this->_id_missing = true;
90 2
            $guid = parent::get_link();
91
        }
92
93 4
        return $guid;
94
    }
95
96
    public function get_local_guid() : ?string
97
    {
98
        return $this->_local_guid;
99
    }
100
101
    public function set_local_guid(string $guid)
102
    {
103
        $this->_local_guid = $guid;
104
    }
105
106 4
    private function _decode($string) : string
107
    {
108 4
        return html_entity_decode((string) $string, ENT_QUOTES, midcom::get()->i18n->get_current_charset());
109
    }
110
}
111